Ejemplo n.º 1
0
        private MatchSiteResult searchFirstOcurrence(string inText,
                                                     Site[] inSites)
        {
            log.Debug(">>");
            MatchSiteResult result = new MatchSiteResult();

            try
            {
                foreach (var site in inSites)
                {
                    using (WebClient web = new WebClient())
                    {
                        var fullUrl  = string.Format(site.Url + "{0}", inText);
                        var htmlSite = "";
                        // if there is poor signal, try again
                        int webTryCounter = 3;
                        while (webTryCounter > 0)
                        {
                            try
                            {
                                htmlSite      = web.DownloadString(fullUrl);
                                webTryCounter = 0;
                            }
                            catch (Exception ex)
                            {
                                log.Debug(ex.StackTrace);
                                log.Debug("Poor signal, try again");
                                Macros.userMsg(Constants.WEAK_NETWORK_SIGNAL);
                                webTryCounter--;
                                htmlSite = "";
                            }
                        }

                        Macros.userMsg(Constants.SEARCHING_SITE, fullUrl);
                        foreach (var pattern in site.ParternList)
                        {
                            if (pattern.SearchTextFound)
                            {
                                // search text found in result
                                var regexSearchText   = new Regex(@pattern.TextFoundPatter, RegexOptions.IgnoreCase);
                                var matchesSearchText = regexSearchText.Matches(htmlSite);

                                if (matchesSearchText.Count > 0)
                                {
                                    var matches = matchesSearchText[0].Groups;

                                    if (matches.Count >= 2)
                                    {
                                        result.TextFound = Regex.Replace(matches[1].Value, pattern.TextFoundNormPatter, pattern.TextFoundNormReplace, RegexOptions.IgnoreCase);
                                    }
                                }
                            }

                            var regexPr       = new Regex(@pattern.RegularEx, RegexOptions.IgnoreCase);
                            var matchesResult = regexPr.Matches(htmlSite);

                            if (matchesResult.Count > 0)
                            {
                                result.Site    = site;
                                result.Pattern = pattern;
                                result.Matches = matchesResult;
                                result.IsMatch = true;
                                return(result);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.Debug(ex.StackTrace);
                log.Debug("Creating empty dictionary config");
                string info = LocationLangSrv.getTagValue(Constants.WEB_DICT_FAIL) + ":" + ex.Message + Constants.BRLINE;
                GlobalRuntimeMsgsInfoSrv.infoMsg(info);
                Macros.userMsg(Constants.WEB_DICT_FAIL, ex.Message);
                result = new MatchSiteResult();
            }
            log.Debug("<<");
            return(result);
        }
Ejemplo n.º 2
0
        //---------------------------------------------------------------------------
        // searchText
        //---------------------------------------------------------------------------
        public void searchText(string inText, QueryDictResult inResult)
        {
            if (!this.loadConfigFlag)
            {
                inResult.resetAttrs();
                return;
            }

            // search pronunciation
            try
            {
                var textNormalice = inText.Replace(" ", "+");
                Macros.userMsg(LocationLangSrv.getTagValue(Constants.SEARCHING_PR));
                MatchSiteResult result = searchFirstOcurrence(textNormalice, dicConf.Pronunciation.Sites);
                if (result.IsMatch)
                {
                    var pattern = (PrPattern)result.Pattern;
                    var idxPr   = pattern.IdxPrGroup;
                    var prData  = idxPr > -1 ? result.Matches[0].Groups[idxPr + 1].Value : "";

                    if (pattern.CleanResult)
                    {
                        inResult.pronunciation = Regex.Replace(prData, pattern.CleanPattern, "", RegexOptions.IgnoreCase);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Debug(ex.StackTrace);
                log.Debug("Setting pronunciation to empty string value");
                inResult.pronunciation = "";
            }

            // search translations
            try
            {
                Macros.userMsg(LocationLangSrv.getTagValue(Constants.SEARCHING_TR));
                MatchSiteResult result = this.searchFirstOcurrence(inText, this.dicConf.Translation.Sites);
                if (result.IsMatch)
                {
                    if (result.TextFound != inText)
                    {
                        return;
                    }
                    inResult.IsEmpty = false;
                    var pattern = (TrPattern)result.Pattern;
                    foreach (Match match in result.Matches)
                    {
                        var idxTr     = pattern.IdxTrGroup;
                        var idxTrType = pattern.IdxTrTypeGroup;

                        DictTrEntry trEntry = new DictTrEntry();
                        trEntry.translation = idxTr > -1 ? match.Groups[idxTr].Value : "";
                        trEntry.type        = idxTrType > -1 ? match.Groups[idxTrType].Value : "";

                        if (pattern.CleanTypeResult)
                        {
                            trEntry.type = Regex.Replace(trEntry.type, pattern.CleanTypePattern, "", RegexOptions.IgnoreCase);
                        }
                        inResult.translations.Add(trEntry);
                    }
                    //inResult.translations = inResult.translations.GroupBy(x => x.translation).Select(g => g.First()).ToList();
                }
            }
            catch (Exception ex)
            {
                log.Debug(ex.StackTrace);
                log.Debug("Removing last translation entry");
                if (inResult.translations.Count > 0)
                {
                    inResult.translations.RemoveAt(inResult.translations.Count - 1);
                }
            }
        }