コード例 #1
0
        /// <summary>
        /// Parses the fetched content.
        /// </summary>
        protected override void ParseContent()
        {
            // Initialize the parser
            this.Translation = string.Empty;
            string strContent = this.Content;

            RavSoft.StringParser parser = new RavSoft.StringParser(strContent);

            // Extract the translation
            string strTranslation = string.Empty;

            if (parser.skipToEndOf("[[[\""))
            {
                bool morePhrasesRemaining = true;
                do
                {
                    string translatedPhrase = null;
                    if (parser.extractTo("\",\"", ref translatedPhrase))
                    {
                        strTranslation += translatedPhrase;
                    }
                    morePhrasesRemaining = parser.skipToEndOf(",\"\",\"\"],[\"");
                }while (morePhrasesRemaining);
            }
            this.Translation = strTranslation.Replace(" .", ".").Replace(" ?", "?").Replace(" ,", ",").Replace(" ;", ";").Replace(" !", "!");

            // Set the translation speak url
            this.TranslationSpeakUrl = string.Format("http://translate.google.com/translate_tts?ie=UTF-8&tl={0}&q={1}",
                                                     Translator.LanguageEnumToIdentifier(this.TargetLanguage),
                                                     HttpUtility.UrlEncode(this.Translation));
        }
コード例 #2
0
ファイル: Translator.cs プロジェクト: danygolden/gianfratti
        /// <summary>
        /// Parses the fetched content.
        /// </summary>
        protected override void parseContent()
        {
            // Initialize the scraper
            this.Translation = string.Empty;
            string strContent = this.Content;

            RavSoft.StringParser parser = new RavSoft.StringParser(strContent);

            // Scrape the translation
            string strTranslation = string.Empty;

            if (parser.skipToEndOf("<span id=result_box"))
            {
                if (parser.skipToEndOf("onmouseout=\"this.style.backgroundColor='#fff'\">"))
                {
                    if (parser.extractTo("</span>", ref strTranslation))
                    {
                        strTranslation = StringParser.removeHtml(strTranslation);
                    }
                }
            }

            #region Fix up the translation
            int startClean = 0;
            int endClean   = 0;
            int i          = 0;
            while (i < strTranslation.Length)
            {
                if (Char.IsLetterOrDigit(strTranslation[i]))
                {
                    startClean = i;
                    break;
                }
                i++;
            }
            i = strTranslation.Length - 1;
            while (i > 0)
            {
                char ch = strTranslation[i];
                if (Char.IsLetterOrDigit(ch) ||
                    (Char.IsPunctuation(ch) && (ch != '\"')))
                {
                    endClean = i;
                    break;
                }
                i--;
            }
            this.Translation = strTranslation.Substring(startClean, endClean - startClean + 1).Replace("\"", "");
            #endregion
        }
コード例 #3
0
        protected override string GetString(string text, Match m)
        {
            // Initialize the scraper
            string Translation = string.Empty;
            string strContent = text;
            RavSoft.StringParser parser = new RavSoft.StringParser(strContent);

            // Scrape the translation
            string strTranslation = string.Empty;
            if (parser.skipToEndOf("<span id=result_box"))
            {
                if (parser.skipToEndOf("onmouseout=\"this.style.backgroundColor='#fff'\">"))
                {
                    if (parser.extractTo("</span>", ref strTranslation))
                    {
                        strTranslation = RavSoft.StringParser.removeHtml(strTranslation);
                    }
                }
            }

            #region Fix up the translation
            int startClean = 0;
            int endClean = 0;
            int i = 0;
            while (i < strTranslation.Length)
            {
                if (Char.IsLetterOrDigit(strTranslation[i]))
                {
                    startClean = i;
                    break;
                }
                i++;
            }
            i = strTranslation.Length - 1;
            while (i > 0)
            {
                char ch = strTranslation[i];
                if (Char.IsLetterOrDigit(ch) ||
                    (Char.IsPunctuation(ch) && (ch != '\"')))
                {
                    endClean = i;
                    break;
                }
                i--;
            }
            Translation = strTranslation.Substring(startClean, endClean - startClean + 1).Replace("\"", "");
            #endregion
            return Translation;
        }