A
A
Alexey Smirnov2016-03-10 16:37:20
HTML
Alexey Smirnov, 2016-03-10 16:37:20

How to automatically get the transcription of an English word from Yandex Translator?

Hello.
I want to automatically receive transcriptions of English words from Yandex Translator. Since I did not find the ability to receive transcriptions in addition to a simple translation in the Yandex Translator API, I decided to "independently" receive transcriptions by parsing the html code on the Yandex Translator web page. To do this, I wrote the following console application in C#:

string sURL = "https://translate.yandex.ru/?text=" + "beware" + "&lang=en-ru";

            //string html;
            //HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sURL);
            //HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            //using (StreamReader stream = new StreamReader(
            //     resp.GetResponseStream()))
            //{
            //    html = stream.ReadToEnd();
            //}

            WebClient client = new WebClient();
            var data = client.DownloadData(sURL);
            var html = Encoding.UTF8.GetString(data);

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

            doc.LoadHtml(html);

            var x = doc.DocumentNode;

            Console.WriteLine("После этой строки появляется ошибка" + x); // В консоли заместо  'x'  будет выведено HtmlAgillityPack.HtmlNode

            //var y = x.SelectNodes("//span[@class='dictionary-transcription']");
            var y = x.SelectNodes("id('dictionaryContent')/x:li[1]/x:div/x:span[1]"); //XPATH-селектор мне автоматически сгенерировал браузер Firefox


            var z = y.ToList();


            string Text = "Пусто";
            foreach (HtmlNode node in z)
            {
                Text = node.InnerText;
            }
            Console.WriteLine(Text);
            Console.ReadLine();

While executing the program, at the line:
var y = x.SelectNodes("некий XPATH-селектор");
I get the error:
"An unhandled exception of type 'System.Xml.XPath.XPathException' in System.Xml.dll
Additional information: Namespace Manager or XsltContext is required. This request contains a prefix, variable, or user-defined function."
After this error appeared, I added a namespace just in case:
using System.Xml.Xsl;
but this did not change anything.
How to solve this error?
PS For parsing HTML code, I use the HtmlAgilityPack library.
PPS I have now studied the HTML code that I upload from the Yandex-translator web page, and found:
<span class="dictionary-transcription">[{{transcription}}]</span>

That is, the Yandex translator has not yet managed to insert any transcription between the tags<span></span>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya, 2016-03-10
@ERAFY

I immediately warn you: you must understand that you are violating the rules for using Yandex services and in no case should you use such solutions for commercial purposes. I hope for your discretion.
Regarding the solution: it is better to enter from the side of the internal API used on the site.
Here is the simplest solution. For parsing Json, the Json.Net library is used (you can rewrite it for parsing with the standard .Net library, it was just faster for me to write an example).

const string text = "dog";
var translateUrl = "https://dictionary.yandex.net/dicservice.json/lookup?ui=ru&text="
                    + text + "&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;
    Console.WriteLine("Транскрипция: " + trs);
}

G
gloomyad, 2017-09-23
@gloomyad

The Yandex API Documentation has an explanation of where the transcription lies.
Table 1. Response XML schema elements DicResult
head Result header (not used).
def Array of dictionary entries. The ts attribute may contain the transcription of the searched word.
tr An array of translations.
syn Array of synonyms.
mean An array of values.
ex An array of examples.

B
BuZZZard, 2020-04-22
@BuZZZard

There are no transcriptions in the Yandex Translator API. But in the Yandex Dictionary API - there is.
The keys for the dictionary and the translator need to be different. The key from the translator is not suitable for the dictionary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question