コード例 #1
0
        public async Task <TextTranslationResult[]> GetTranslationResultAsync(
            ClassiiiUser classiiiUser,
            string queueId)
        {
            string url = baseUrl + "/translate/async/" + queueId;

            Dictionary <string, object> headers = HttpUtils.BuildHeaders(classiiiUser, url);

            var content = await HttpUtils.SendAsync(HttpMethod.Get, url, headers, null);

            var byteArray = await content.ReadAsByteArrayAsync();

            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

            var definition = new { status = "", data = new { taskId = "", translationResult = new TextTranslationResult[] { } } };

            var serverResp = JsonConvert.DeserializeAnonymousType(responseString, definition);

            if (serverResp.status != ResponseStatus.Success)
            {
                throw new Exception("text translation failed");
            }

            if (serverResp.data.taskId == null)
            {
                return(new TextTranslationResult[] { });
            }

            return(serverResp.data.translationResult);
        }
コード例 #2
0
        public async Task <TextTranslationResult[]> TranslateFlowAsync(ClassiiiUser classiiiUser, TextTranslationOption option, string[] text)
        {
            this.classiiiUser = classiiiUser;

            this.queueId = await client.TranslateTextByAsyncModeAsync(classiiiUser, option, text);

            if (aTimer != null)
            {
                StopTimer(aTimer);
            }

            aTimer = new System.Timers.Timer();

            aTimer.Interval = Interval;

            aTimer.Elapsed += OnTimedEvent;

            aTimer.AutoReset = true;

            aTimer.Enabled = true;

            while (!done)
            {
                Thread.Sleep(1000);
            }

            return(result);
        }
コード例 #3
0
        public async Task <string> SpeechToTextAsync(
            ClassiiiUser classiiiUser,
            string sourceLang,
            string filePath)
        {
            string url = baseUrl + "/translate/stt";

            Dictionary <string, object> headers = HttpUtils.BuildHeaders(classiiiUser, url);

            Dictionary <string, string> body = new Dictionary <string, string>();

            body.Add("sourceLang", sourceLang);
            body.Add("audioFile", Path.GetFileName(filePath));

            Dictionary <string, string> fileDict = new Dictionary <string, string>();

            fileDict.Add("audioFile", filePath);

            var content = await HttpUtils.PostFileAsync(url, headers, body, fileDict);

            var byteArray = await content.ReadAsByteArrayAsync();

            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

            var definition = new { status = "", data = new { result = "" } };

            var serverResp = JsonConvert.DeserializeAnonymousType(responseString, definition);

            if (serverResp.status != ResponseStatus.Success)
            {
                throw new Exception("speech to text failed");
            }

            return(serverResp.data.result);
        }
コード例 #4
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            //Console.WriteLine("Hello World!");
            UserInfo.init("config.json");
            FileTranslateFlow flow = new FileTranslateFlow("https://translate.classiii.info/api/v1");
            //FileTranslateFlow flow = new FileTranslateFlow("http://*****:*****@"C:\mydocuments\morning.docx" };
            string[] langs   = { "en" };
            int      fieldId = 1;
            bool     done    = false;
            //done = await flow.T4ooFlowAsync(t4ooUser, files, langs, fieldId);
            //Debug.Assert(done);
            ClassiiiUser classiiiUser = new ClassiiiUser {
                AccessKey  = UserInfo.CLASSIII_ACCESS_KEY,
                SecretKey  = UserInfo.CLASSIII_SECRET_KEY,
                ContractId = UserInfo.FILE_CONTRACT_ID
            };

            done = await flow.ClassiiiFlowAsync(classiiiUser, files, langs, fieldId);

            Debug.Assert(done);
        }
コード例 #5
0
        public async Task <bool> AddUserDictionaryItemAsync(
            ClassiiiUser classiiiUser, UserDictionaryItem item)
        {
            string url = baseUrl + "/dictionary";

            Dictionary <string, object> headers = HttpUtils.BuildHeaders(classiiiUser, url);

            Dictionary <string, object> body = new Dictionary <string, object>()
            {
                { "fromLang", item.fromLang },
                { "fromText", item.fromText },
                { "toLang", item.toLang },
                { "toText", item.toText }
            };

            var content = await HttpUtils.SendAsync(HttpMethod.Post, url, headers, body);

            var byteArray = await content.ReadAsByteArrayAsync();

            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

            var definition = new { status = "" };

            var serverResp = JsonConvert.DeserializeAnonymousType(responseString, definition);

            if (serverResp.status != ResponseStatus.Success)
            {
                throw new Exception("add user dictionary item failed");
            }

            return(true);
        }
コード例 #6
0
        public async Task <string> TranslateTextByAsyncModeAsync(
            ClassiiiUser classiiiUser,
            TextTranslationOption option,
            string[] text)
        {
            string url = baseUrl + "/translate/async";

            Dictionary <string, object> headers = HttpUtils.BuildHeaders(classiiiUser, url);

            Dictionary <string, object> body = BuildBody(option, classiiiUser.ContractId, text);

            var content = await HttpUtils.SendAsync(HttpMethod.Post, url, headers, body);

            var byteArray = await content.ReadAsByteArrayAsync();

            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

            var definition = new { status = "", data = new { queueId = "" } };

            var serverResp = JsonConvert.DeserializeAnonymousType(responseString, definition);

            if (serverResp.status != ResponseStatus.Success)
            {
                throw new Exception("text translation failed");
            }

            return(serverResp.data.queueId);
        }
コード例 #7
0
        public async Task <bool> T4ooFlowAsync(T4ooUser t4ooUser, string[] files, string[] langs, int fieldId)
        {
            string authCode = await client.GetAuthCodeAsync(t4ooUser);

            ClassiiiUser classiiiUser = await client.AuthenticateAsync(t4ooUser, authCode);

            return(await ClassiiiFlowAsync(classiiiUser, files, langs, fieldId));
        }
コード例 #8
0
        private static async Task TestTextToSpeech(string baseUrl,
                                                   ClassiiiUser classiiiUser,
                                                   string targetLang,
                                                   string text,
                                                   string saveFilePath)
        {
            TextToSpeechClient textTranslationClient = new TextToSpeechClient(baseUrl);
            //  音声合成
            bool result = await textTranslationClient.TextToSpeechtAsync(classiiiUser, targetLang, text, saveFilePath);

            Debug.Assert(result);
        }
コード例 #9
0
        static async Task Main(string[] args)
        {
            UserInfo.init("config.json");

            string baseUrl = "https://translate.classiii.info/api/v1";

            ClassiiiUser classiiiUser = new ClassiiiUser {
                AccessKey = UserInfo.CLASSIII_ACCESS_KEY, SecretKey = UserInfo.CLASSIII_SECRET_KEY
            };

            // 音声合成を試す
            await TestTextToSpeech(baseUrl, classiiiUser, "ja", "こんにちは", "hello.wav");
        }
コード例 #10
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            UserInfo.init("config.json");

            string baseUrl = "https://translate.classiii.info/api/v1";

            ClassiiiUser classiiiUser = new ClassiiiUser {
                AccessKey = UserInfo.CLASSIII_ACCESS_KEY, SecretKey = UserInfo.CLASSIII_SECRET_KEY
            };

            // 音声認識を試す
            await TestSpeechToText(baseUrl, classiiiUser);
        }
コード例 #11
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            UserInfo.init("config.json");
            string       baseUrl      = "https://translate.classiii.info/api/v1";
            ClassiiiUser classiiiUser = new ClassiiiUser {
                AccessKey  = UserInfo.CLASSIII_ACCESS_KEY,
                SecretKey  = UserInfo.CLASSIII_SECRET_KEY,
                ContractId = UserInfo.TEXT_CONTRACT_ID
            };

            // テキスト翻訳を試す
            await TestTextTranslation(baseUrl, classiiiUser);

            // ユーザー辞書を試す
            await TestUserDictionary(baseUrl, classiiiUser);
        }
コード例 #12
0
        public async Task <bool> ClassiiiFlowAsync(ClassiiiUser classiiiUser, string[] files, string[] langs, int fieldId)
        {
            this.classiiiUser = classiiiUser;

            long timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();

            FileTranslationOption option = new FileTranslationOption
            {
                Langs     = langs,
                AccessKey = classiiiUser.AccessKey,
                SecretKey = classiiiUser.SecretKey,
                Nonce     = timeStamp.ToString(),
                FieldId   = fieldId
            };

            translateId = await client.TranslateAsync(files, classiiiUser.ContractId, option);

            if (aTimer != null)
            {
                StopTimer(aTimer);
            }

            aTimer = new System.Timers.Timer();

            aTimer.Interval = Interval;

            aTimer.Elapsed += OnTimedEvent;

            aTimer.AutoReset = true;

            aTimer.Enabled = true;

            while (!done)
            {
                Thread.Sleep(1000);
            }

            return(true);
        }
コード例 #13
0
        public async Task <bool> TextToSpeechtAsync(
            ClassiiiUser classiiiUser,
            string targetLang,
            string text,
            string saveFilePath)
        {
            string url = baseUrl + "/translate/tts";

            Dictionary <string, object> headers = HttpUtils.BuildHeaders(classiiiUser, url);

            Dictionary <string, object> body = new Dictionary <string, object>();

            body.Add("targetLang", targetLang);
            body.Add("text", text);

            var content = await HttpUtils.SendAsync(HttpMethod.Post, url, headers, body);

            var byteArray = await content.ReadAsByteArrayAsync();

            await File.WriteAllBytesAsync(saveFilePath, byteArray);

            return(true);
        }
コード例 #14
0
        public async Task <bool> DownloadZipAsync(ClassiiiUser classiiiUser, string ids, string savePath)
        {
            string url = baseUrl + "/downloads?ids=" + HttpUtility.UrlEncode(ids);

            string apiPath   = HttpUtility.UrlDecode(new Uri(url).PathAndQuery);
            string nonce     = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
            string signature = Utils.GetSignature(nonce, apiPath, classiiiUser.SecretKey);

            Dictionary <string, object> headers = new Dictionary <string, object> {
                { "accessKey", classiiiUser.AccessKey },
                { "nonce", nonce },
                { "signature", signature }
            };

            var content = await HttpUtils.SendAsync(HttpMethod.Get, url, headers, null);

            MemoryStream stream = new MemoryStream();

            await content.CopyToAsync(stream);

            Utils.UnzipStream(stream, savePath);

            return(true);
        }
コード例 #15
0
        public async Task <bool> DeleteUserDictionaryItemAsync(
            ClassiiiUser classiiiUser, int id)
        {
            string url = baseUrl + "/dictionary/" + id;

            Dictionary <string, object> headers = HttpUtils.BuildHeaders(classiiiUser, url);

            var content = await HttpUtils.SendAsync(HttpMethod.Delete, url, headers, null);

            var byteArray = await content.ReadAsByteArrayAsync();

            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

            var definition = new { status = "" };

            var serverResp = JsonConvert.DeserializeAnonymousType(responseString, definition);

            if (serverResp.status != ResponseStatus.Success)
            {
                throw new Exception("delete user dictionary item failed");
            }

            return(true);
        }
コード例 #16
0
        public async Task <UserDictionaryItem[]> GetUserDictionaryAsync(
            ClassiiiUser classiiiUser)
        {
            string url = baseUrl + "/dictionary";

            Dictionary <string, object> headers = HttpUtils.BuildHeaders(classiiiUser, url);

            var content = await HttpUtils.SendAsync(HttpMethod.Get, url, headers, null);

            var byteArray = await content.ReadAsByteArrayAsync();

            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

            var definition = new { status = "", data = new { entries = new UserDictionaryItem[] { } } };

            var serverResp = JsonConvert.DeserializeAnonymousType(responseString, definition);

            if (serverResp.status != ResponseStatus.Success)
            {
                throw new Exception("get user dictionary failed");
            }

            return(serverResp.data.entries);
        }
コード例 #17
0
        public async Task <Translate> GetOneHistoryAsync(ClassiiiUser classiiiUser, string translateId)
        {
            string url = baseUrl + "/translate-result/" + translateId;

            Dictionary <string, object> headers = HttpUtils.BuildHeaders(classiiiUser, url);

            var content = await HttpUtils.SendAsync(HttpMethod.Get, url, headers, null);

            var byteArray = await content.ReadAsByteArrayAsync();

            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

            var definition = new { status = "", data = new Translate {
                                   } };

            var serverResp = JsonConvert.DeserializeAnonymousType(responseString, definition);

            if (serverResp.status != ResponseStatus.Success)
            {
                return(null);
            }

            return(serverResp.data);
        }
コード例 #18
0
 public Translate[] GetAllHistories(ClassiiiUser classiiiUser)
 {
     Translate[] ret = { };
     return(ret);
 }
コード例 #19
0
        private static async System.Threading.Tasks.Task TestSpeechToText(string baseUrl, ClassiiiUser classiiiUser)
        {
            SpeechToTextClient textTranslationClient = new SpeechToTextClient(baseUrl);
            //  音声認識
            string result = await textTranslationClient.SpeechToTextAsync(classiiiUser, "ja", "../../../0001_near.wav");

            Debug.Assert(result == "そこに着いたらもう一度誰かに尋ねてください");
        }
コード例 #20
0
        private static async System.Threading.Tasks.Task TestUserDictionary(string baseUrl, ClassiiiUser classiiiUser)
        {
            UserDictionaryClient userDictionaryClient = new UserDictionaryClient(baseUrl);

            UserDictionaryItem item = new UserDictionaryItem {
                fromLang = "en", fromText = "hello", toLang = "ja", toText = "おはよう"
            };

            bool bRet;

            string[] text = new string[] { "hello" };
            TextTranslationOption option = new TextTranslationOption
            {
                FieldId    = 1,
                SourceLang = "en",
                TargetLang = "ja"
            };
            TextTranslationClient textTranslationClient = new TextTranslationClient(baseUrl);

            TextTranslationResult[] textTranslationResults;
            UserDictionaryItem[]    userDictionaryItems;

            // ユーザー辞書を登録
            bRet = await userDictionaryClient.AddUserDictionaryItemAsync(classiiiUser, item);

            Debug.Assert(bRet);

            // ユーザー辞書を取得
            userDictionaryItems = await userDictionaryClient.GetUserDictionaryAsync(classiiiUser);

            Debug.Assert(userDictionaryItems.Length == 1);
            Debug.Assert(userDictionaryItems[0] == item);

            // ユーザー辞書を確認 -----------------------------------------------------
            textTranslationResults = await textTranslationClient.TranslateTextBySyncModeAsync(classiiiUser, option, text);

            Debug.Assert(textTranslationResults.Length == 1);
            Debug.Assert(textTranslationResults[0].sourceText == "hello");
            Debug.Assert(textTranslationResults[0].translatedText == "おはよう");
            // ユーザー辞書を確認 ----------------------------------------------------- end

            // ユーザー辞書を更新
            UserDictionaryItem itemNew = new UserDictionaryItem {
                fromLang = "en", fromText = "hello", toLang = "ja", toText = "こんにちは"
            };

            bRet = await userDictionaryClient.UpdateUserDictionaryItemAsync(classiiiUser, userDictionaryItems[0].id, itemNew);

            Debug.Assert(bRet);

            // ユーザー辞書を確認 -----------------------------------------------------
            textTranslationResults = await textTranslationClient.TranslateTextBySyncModeAsync(classiiiUser, option, text);

            Debug.Assert(textTranslationResults.Length == 1);
            Debug.Assert(textTranslationResults[0].sourceText == "hello");
            Debug.Assert(textTranslationResults[0].translatedText == "こんにちは");
            // ユーザー辞書を確認 ----------------------------------------------------- end

            // ユーザー辞書を削除
            bRet = await userDictionaryClient.DeleteUserDictionaryItemAsync(classiiiUser, userDictionaryItems[0].id);

            Debug.Assert(bRet);

            // ユーザー辞書数を確認
            userDictionaryItems = await userDictionaryClient.GetUserDictionaryAsync(classiiiUser);

            Debug.Assert(userDictionaryItems.Length == 0);
        }
コード例 #21
0
        private static async System.Threading.Tasks.Task TestTextTranslation(string baseUrl, ClassiiiUser classiiiUser)
        {
            string[] text = new string[] { "hello" };
            TextTranslationOption option = new TextTranslationOption
            {
                FieldId    = 1,
                TargetLang = "ja",
                SourceLang = "en"
            };
            TextTranslationClient textTranslationClient = new TextTranslationClient(baseUrl);

            //  同期テキスト翻訳
            TextTranslationResult[] ret = await textTranslationClient.TranslateTextBySyncModeAsync(classiiiUser, option, text);

            Debug.Assert(ret.Length == 1);
            Debug.Assert(ret[0].sourceText == "hello");
            Debug.Assert(ret[0].translatedText == "こんにちは");


            //  非同期テキスト翻訳
            TranslateTextByAsyncModeFlow flow = new TranslateTextByAsyncModeFlow(baseUrl);

            ret = await flow.TranslateFlowAsync(classiiiUser, option, text);

            Debug.Assert(ret.Length == 1);
            Debug.Assert(ret[0].sourceText == "hello");
            Debug.Assert(ret[0].translatedText == "こんにちは");
        }