Esempio n. 1
0
        internal static SelectableQuoteCollection ToSelectableQuoteCollection(this IEnumerable <SelectableQuote> items, CultureInfo culture)
        {
            SelectableQuoteCollection returnValue = new SelectableQuoteCollection {
                Culture = culture
            };

            foreach (SelectableQuote item in items)
            {
                returnValue.Add(item);
            }

            return(returnValue);
        }
Esempio n. 2
0
        private static SelectableQuote GetQuote(SelectableQuoteCollection oldQuotes, string quoteText, string additionalInformation)
        {
            SelectableQuote returnValue;
            SelectableQuote oldQuote;

            if (oldQuotes != null && oldQuotes.TryGetValue(quoteText, out oldQuote))
            {
                if (oldQuote.AdditionalInformation != additionalInformation)
                {
                    oldQuote.AdditionalInformation = additionalInformation;
                }

                returnValue = oldQuote;
            }
            else
            {
                returnValue = new SelectableQuote(quoteText, additionalInformation);
            }

            return(returnValue);
        }
Esempio n. 3
0
        private IEnumerator <int> ProcessCollectQuotes(AsyncEnumerator asyncEnumerator, string topic, CultureInfo culture, Uri uri, AssignQuotePageDataDelegate assignQuotesDelegate)
        {
            Uri quoteSourceUri = GetQuoteSourceUri(culture, topic);

            Util.BeginFetchXmlPage(quoteSourceUri, asyncEnumerator.End(AsyncEnumeratorDiscardGroup, Util.EndFetchXmlPage), null);

            yield return(1);

            if (asyncEnumerator.IsCanceled())
            {
                yield break;
            }

            string contents;

            try
            {
                XDocument xDocument = Util.EndFetchXmlPage(asyncEnumerator.DequeueAsyncResult());

                if (xDocument == null)
                {
                    throw new NullReferenceException();
                }

                // extracting the page contents
                contents = GetXmlPageContents(xDocument);
            }
            catch (Exception ex)
            {
                OnErrorCollectingQuotes(new ErrorCollectingQuotesEventArgs(topic, culture, uri, ex));
                yield break;
            }

            IAsyncResult getQuotesResult = BeginParsePageContentsAndExtractQuotes(topic, culture, contents, null,
                                                                                  asyncEnumerator.End(AsyncEnumeratorDiscardGroup, EndParsePageContentsAndExtractQuotes), null);
            IAsyncResult getTopicTranslationsResult = BeginExtractTopicTranslations(contents,
                                                                                    asyncEnumerator.End(AsyncEnumeratorDiscardGroup, EndExtractTopicTranslations), null);

            yield return(2);

            if (asyncEnumerator.IsCanceled())
            {
                yield break;
            }

            try
            {
                TopicTranslation[]        topicTranslations = EndExtractTopicTranslations(getTopicTranslationsResult);
                SelectableQuoteCollection quotes            = EndParsePageContentsAndExtractQuotes(getQuotesResult);

                if (quotes.Count == 0)
                {
                    OnNoQuotesCollected(new NoQuotesCollectedEventArgs(topic, culture, uri));

                    // Change by Ming Slogar on 26Apr2014 at 20:25
                    // Reason: The following line prevents the topic from being returned
                    //		when it has no listed quotes.
                    //yield break;
                }

                assignQuotesDelegate.Invoke(quotes, topicTranslations);
            }
            catch (TopicAmbiguousException ex)
            {
                OnTopicAmbiguous(new TopicAmbiguousEventArgs(topic, culture, uri, ex.TopicChoices));
                yield break;
            }
            catch (Exception ex)
            {
                OnErrorCollectingQuotes(new ErrorCollectingQuotesEventArgs(topic, culture, uri, ex));
                yield break;
            }
            finally
            {
                // cleanup
                asyncEnumerator.DequeueAsyncResult();
                asyncEnumerator.DequeueAsyncResult();

                lock (asyncEnumerator)
                {
                    _asyncEnumerators.Remove(asyncEnumerator);
                }
            }
        }
Esempio n. 4
0
 private IAsyncResult BeginParsePageContentsAndExtractQuotes(string topic, CultureInfo culture, string contents, SelectableQuoteCollection oldQuotes,
                                                             AsyncCallback callback, object state)
 {
     return(_parsePageContentsAndExtractQuotes.BeginInvoke(topic, culture, contents, oldQuotes, callback, state));
 }
Esempio n. 5
0
        private SelectableQuoteCollection ParsePageContentsAndExtractQuotes(string topic, CultureInfo culture, string contents, SelectableQuoteCollection oldQuotes)
        {
            QuoteCollectorRules rules = _cultureMapper[culture].Rules as QuoteCollectorRules;

            if (rules.DisambiguationTemplateIdentifiers != null && rules.DisambiguationTemplateRegex != null)
            {
                for (int i = 0; i < rules.DisambiguationTemplateIdentifiers.Length; i++)
                {
                    if (contents.Contains(rules.DisambiguationTemplateIdentifiers[i]))
                    {
                        throw new TopicAmbiguousException(topic, GetTopicChoices(contents, rules));
                    }
                }
            }

            // removing sections that do not contain quotes (only if required, i.e. quotes have to be determined heuristically)
            if (rules.WikiSectionsToSkipRegex != null)
            {
                contents = rules.WikiSectionsToSkipRegex.Replace(contents, String.Empty);
            }

            // replacing xhtml tags by their contents
            contents = ReplaceXhtmlTags(contents);

            // various string replacements
            contents = HttpUtility.HtmlDecode(ReplaceMiscSequences(contents));

            // extracting quotes
            MatchCollection quoteMatches = rules.QuoteRegex.Matches(contents);

            var quotes = from Match match in quoteMatches
                         where match.Groups[QuoteRegexGroup].Success
                         let quoteHelper                         = match.Groups[QuoteRegexGroup].Value
                                                       let quote = (rules.NewlineRegex != null) ?
                                                                   rules.NewlineRegex.Replace(quoteHelper, "\n") :
                                                                   quoteHelper
                                                                   let additionalInformation = match.Groups[AdditionalInformationRegexGroup].Success ?
                                                                                               rules.NewlineRegex.Replace(match.Groups[AdditionalInformationRegexGroup].Value, " ") :
                                                                                               String.Empty
                                                                                               select GetQuote(oldQuotes, quote.TrimEnd(), additionalInformation.TrimEnd());

            return(quotes.ToSelectableQuoteCollection(culture));
        }