Exemplo n.º 1
0
 /// <summary>
 ///
 /// </summary>
 public SimilarTranslation()
 {
     this.PartOfSpeech = PartOfSpeech.Not_Specified;
     this.Translation  = null;
     //this.SourceText = null;
     this.TranslationsToSourceLanguage = null;// new List<string>();
     this.TranslationFrequency         = TranslationFrequency.Not_Specified;
 }
Exemplo n.º 2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="partOfSpeech"></param>
 /// <param name="translation"></param>
 /// <param name="translationsToSourceLanguage"></param>
 /// <param name="translationFrequency"></param>
 public SimilarTranslation(PartOfSpeech partOfSpeech, string translation, List <string> translationsToSourceLanguage, TranslationFrequency translationFrequency)
 {
     this.PartOfSpeech = partOfSpeech;
     this.Translation  = translation;
     this.TranslationsToSourceLanguage = translationsToSourceLanguage;
 }
Exemplo n.º 3
0
        private Translation Parse2(string HTML)
        {
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(HTML);

            #region Translation Text
            HtmlNodeCollection _targetTextNodes = doc.DocumentNode.SelectNodes("//div[contains(@class, 'text-wrap tlid-copy-target')]");
            if (_targetTextNodes == null)
            {
                return(null);
            }
            string translationText = _targetTextNodes?[0].GetElementsByClass("span", "tlid-translation translation").ToList()[0].InnerHtml;
            translationText = translationText.Replace("<br>", "\r\n");
            translationText = translationText.Replace("</span>", "");
            translationText = translationText.Replace("<span title=\"\">", "");
            #endregion
            #region Translation Language
            string   tl = _targetTextNodes[0].GetElementsByClass("span", "tlid-translation translation").ToList()[0].GetAttributeValue("lang", "");
            Language translationLanguage = Language.Languages.Find(l => l.Code == tl);

            //Type language = typeof(Language);
            //PropertyInfo myPropInfo = language.GetProperty("Russian");
            //PropertyInfo[] l = language.GetProperties();
            //foreach (PropertyInfo prop in language.GetProperties())
            //{
            //    if (prop.GetType() != typeof(Language))
            //        continue;

            //    //object[] attributes = prop.GetCustomAttributes(typeof(MyAttribute), true);
            //    //if (attributes.Length == 1)
            //    //{
            //    //    //Property with my custom attribute
            //    //}
            //}
            #endregion
            #region Source Text
            HtmlNodeCollection _sourceTextNodes = doc.DocumentNode.SelectNodes("//div[contains(@class, 'text-dummy')]");
            string             sourceText       = _sourceTextNodes?[0].InnerHtml;
            //string sourceText = (_sourceTextNodes == null) ? null : _sourceTextNodes[0].InnerHtml;
            //string sourceText = _sourceTextNodes?[0].InnerHtml;
            #endregion
            #region Source Language
            //<div role="button"
            //<div class="lang-btn">
            Language sourcseLanguage = Language.Not_Specified;
            #endregion

            Translation translation = new Translation(sourceText, sourcseLanguage, translationText, translationLanguage);

            #region Translation Transliteration
            HtmlNodeCollection _targetTextTranslitNodes = doc.DocumentNode.SelectNodes("//div[contains(@class, 'tlid-result-transliteration-container result-transliteration-container transliteration-container')]");
            if (_targetTextTranslitNodes != null)
            {
                translation.Transliteration = _targetTextTranslitNodes[0].InnerHtml;
            }
            //string translit = _targetTextTranslitNodes?[0].InnerHtml;
            #endregion
            #region SimilarTranslations
            HtmlNodeCollection _targetTextTranslationsNodes = doc.DocumentNode.SelectNodes("//div[contains(@class, 'gt-cd gt-cd-mbd gt-cd-baf')]");
            if (_targetTextTranslationsNodes != null)
            {
                translation.SimilarTranslations = new List <SimilarTranslation>();
                string               similarTranslation           = null;
                List <string>        translationsToSourceLanguage = null;
                PartOfSpeech         PartOfSpeech         = PartOfSpeech.Not_Specified;
                TranslationFrequency translationFrequency = TranslationFrequency.Not_Specified;

                IEnumerable <HtmlNode> trs = _targetTextTranslationsNodes[0].GetElementsByTagName("tr");
                foreach (HtmlNode tr in trs)
                {
                    List <HtmlNode> tds = tr.GetElementsByTagName("td").ToList();
                    if (tds.Count == 1)
                    {
                        PartOfSpeech = (PartOfSpeech)Enum.Parse(typeof(PartOfSpeech), tds[0].GetElementsByClass("span", "gt-cd-pos").ToList()[0].InnerHtml);
                    }
                    if (tds.Count == 3)
                    {
                        similarTranslation           = tds[0].GetElementsByClass("span", "gt-baf-cell gt-baf-word-clickable").ToList()[0].InnerText;
                        translationsToSourceLanguage = new List <string>();
                        foreach (HtmlNode td in tds[1].GetElementsByClass("span", "gt-baf-back"))
                        {
                            translationsToSourceLanguage.Add(td.InnerText);
                        }
                        string tf = tds[2].GetElementsByClass("div", "gt-baf-cell gt-baf-entry-score").ToList()[0].GetAttributeValue("title", null);
                        translationFrequency = (TranslationFrequency)Enum.Parse(typeof(TranslationFrequency), tf.Replace(' ', '_'));
                    }

                    translation.SimilarTranslations.Add(new SimilarTranslation(PartOfSpeech, similarTranslation, translationsToSourceLanguage, translationFrequency));
                }
            }

            #endregion

            #region Source Translitiration
            //HtmlNodeCollection _sourceTranscriptionNodes = doc.DocumentNode.SelectNodes("//div[contains(@class, 'tlid-source-transliteration-container source-transliteration-container transliteration-container')]");
            //IEnumerable<HtmlNode> tt = _sourceTranscriptionNodes[0].GetElementsByClass("div", "tlid-transliteration-content transliteration-content full");

            HtmlNodeCollection _sourceTextWTranscriptionNodes = doc.DocumentNode.SelectNodes("//div[contains(@class, 'gt-cc-t')]");

            //Source text
            //var tst = _sourceTextWTranscriptionNodes[0].GetElementsByClass("span", "gt-ct-text").ToList()[0].InnerHtml;

            //Source translitiration
            translation.TranslationSource.Transliteration = _sourceTextWTranscriptionNodes?[0].GetElementsByClass("span", "gt-ct-translit")?.ToList()[0].InnerHtml;
            #endregion

            translation.TranslationSource.Definitions = GetSourceDefinitions(doc);
            translation.TranslationSource.Examples    = GetSourceExamples(doc);
            translation.TranslationSource.Synonyms    = GetSourceSynonyms(doc);
            translation.TranslationSource.SeeAlso     = GetSourceSeeAlso(doc);

            //textBox3.Text = _sourceTextWTranscriptionNodes[0].InnerHtml;
            return(translation);
        }