A
A
Alexey Smirnov2016-03-10 22:05:38
Programming
Alexey Smirnov, 2016-03-10 22:05:38

How to solve the problem with (probably) memory overflow?

Hello.
I have some array of strings (consisting of English words) obtained as follows:

string stringFromBook = "...Здесь разные англоязычные слова (разделенные пробелами) из некоторой книги...";
string[] words = stringFromBook.Split(' '); //Превращаю строку в массив

I am extracting elements one by one from the "words" array (which contain some English words) and further, using the method:
static string GetTranscriptionFromYandexTranslator(string englishWord) {...} //Тело метода приведено ниже

I receive transcriptions of these English. words from Yandex translator.
After that, the resulting transcriptions, along with the original English. in words, line by line I output to the console:
Console.WriteLine(transcripAndWord);
And everything works fine, except that only 29 terms with transcriptions are output to the console (that is, only 29 elements of the array are output to the console, despite the fact that there are only about 50 elements in the array), and then the program stops with the message:
"Unhandled exception of type 'System.ArgumentOutOfRangeException' in mscorlib.dll
Additional information: Index out of range. Index must be a positive number and its size must not exceed the size of the collection."
When I am in the program line:
string stringFromBook = "...Здесь разные англоязычные слова (разделенные пробелами) из некоторой книги...";

entered (for testing) some other string (with other English words), then the maximum number of array elements displayed in the console (English words with transcriptions) decreased to 24 (was 29).
PS I have an assumption that this may be somehow related to memory overflow. Perhaps the transcriptions themselves "weigh a lot." But that's all my guess, I don't know for sure.
What is causing this problem and/or how to solve it?
The code I'm using is:
class Program
    {

        //Метод получения транскрипции через API Яндекс-переводчика
        static string GetTranscriptionFromYandexTranslator(string englishWord)
        {
            string transcription;

            var translateUrl = "https://dictionary.yandex.net/dicservice.json/lookup?ui=ru&text=" + englishWord + "&lang=en-ru&flags=23";

            using (var wc = new WebClient())
            {
                wc.Encoding = Encoding.UTF8;
                var resultHtml = wc.DownloadString(translateUrl);
                dynamic trsJson = JObject.Parse(resultHtml);

                var trs = trsJson.def[0].ts;
                transcription = trs;
            }
            return transcription;
        }


        static void Main(string[] args)
        {
           string stringFromBook = "...Здесь разные англоязычные слова (разделенные пробелами) из некоторой книги...";

            string[] words = stringFromBook.Split(' ');

            string transcripAndWord, transcription;

            for (int i = 0; i < words.Length; i++)
            {
                    transcription = GetTranscriptionFromYandexTranslator(words[i]);

                    transcripAndWord = words[i] + " [" + transcription + "]";
     
                    Console.WriteLine(transcripAndWord); 
            }
           Console.ReadLine();
        }
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question