示例#1
0
        public void Init(StreamReader file, int startIndex = 0, int count = -1)
        {
            mVocabList = new VocabList(file, startIndex, count);

            string line;
            int    lineNum = 0;

            while ((line = file.ReadLine()) != null)
            {
                if (count != -1 && lineNum >= count)
                {
                    break;
                }

                if (lineNum >= startIndex)
                {
                    string[] parts = line.Split(new string[] { " = " }, StringSplitOptions.RemoveEmptyEntries);
                    mVocabList.Add(new Word(parts[1], parts[0]));
                }

                lineNum++;
            }

            PerformGIR();
        }
示例#2
0
        private static VocabList YandexTranslate(List <string> words)
        {
            VocabList vocabList = new VocabList();

            // Yandex doesn't seem to like translating too many words at once so split into groups of 1000 (I haven't figured out if it's a character limit, word limit, or phrase limit. Results are insonsistent)
            int wordsAllowed = 1000;
            int passes       = (-1 + words.Count + wordsAllowed) / wordsAllowed;

            for (int pass = 0; pass < passes; pass++)
            {
                string allText = string.Empty;

                for (int i = 0; i < wordsAllowed; i++)
                {
                    int wordIndex = i + (wordsAllowed * pass);
                    if (wordIndex >= words.Count)
                    {
                        break;
                    }

                    string word = words[wordIndex];
                    // Don't print the &text= part for the first one of each pass because that's added on the QueryString.Add call
                    if (i > 0)
                    {
                        allText += "&text=";
                    }
                    allText += word;
                }

                WebClient webClient = new WebClient();
                webClient.QueryString.Add("key", "trnsl.1.1.20140114T110901Z.06c89613f8b9bdf9.ec1414fa46f30fe0bcdaa6985c3e7b2660ffd9e7");
                webClient.QueryString.Add("lang", "de-en");
                webClient.QueryString.Add("text", allText);
                webClient.Encoding = Encoding.UTF8;
                string        result         = webClient.DownloadString("https://translate.yandex.net/api/v1.5/tr/translate");
                List <string> translatedList = YandexResponseToList(result);

                for (int i = 0; i < translatedList.Count; i++)
                {
                    int wordIndex = i + (wordsAllowed * pass);
                    vocabList.Add(new Word(translatedList[i], words[wordIndex]));
                }
            }

            return(vocabList);
        }
示例#3
0
        public void Init(String[] vocabArray)
        {
            mVocabList = new VocabList();

            foreach (String line in vocabArray)
            {
                string[] parts = line.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length == 2)
                {
                    String source = parts[0].Trim();
                    String target = parts[1].Trim();
                    mVocabList.Add(new Word(source, target));
                }
            }

            PerformGIR();
        }