Пример #1
0
        public static DigestOutputResult Union(IEnumerable <Tuple <DigestOutputResult, int> > collection)
        {
            int textShift = 0;

            foreach (var t in collection)
            {
                if (t.Item1 != null)
                {
                    foreach (var digestTuple in t.Item1.Tuples)
                    {
                        var sentence = XElement.Parse(digestTuple.SentenceText);
                        RDFUnifier.UpdateAttributeValue(sentence, BlockAttribute.POS, textShift);
                        digestTuple.SetSentenceText(sentence.ToString());
                    }
                }
                textShift += t.Item2;
            }
            return(new DigestOutputResult(collection.Where(t => t.Item1 != null).SelectMany(t => t.Item1.Tuples)));
        }
Пример #2
0
        public LingvisticsResult ProcessText(LingvisticsTextInput input)
        {
            int maxTextSize = Math.Min(input.Text.Length, Config.Default.ProcessorMaxTextSize);

            //цикл на 2 повтора с уменьшением размера блока, если ни один блок за первый раз не обработан
            for (int i = 0; i < 2 && 0 < maxTextSize; ++i)
            {
                var blockList = (input.Text.Length <= maxTextSize) ?
                                //обработка всего документа за раз
                                new[] { input.Text } :
                //итерационная обработка документа по частям
                TextSplitter.Split(input.Text, maxTextSize);

                Logger.DebugFormat("DocSize={0}, PartCount={1}, i={2}", input.Text.Length, blockList.Length, i);

                var rcList = blockList.Select(t =>
                {
                    LingvisticsResult result = null;
                    try
                    {
                        //Повтор в случае ошибки [maxRepeatTimes] раз
                        for (int maxRepeatTimes = 1; true; --maxRepeatTimes)
                        {
                            var sw                  = Stopwatch.StartNew();
                            var poolItem            = _ProcessorPool.GetItem(ProcessingType.Heavy);
                            var totalSecondsGetItem = sw.Elapsed.TotalSeconds;

                            if (Logger.IsDebugEnabled)
                            {
                                Logger.DebugFormat("LoadStatus: {0}", _ProcessorPool.GetLoadingStatus());
                            }
                            try
                            {
                                var lti = new LingvisticsTextInput(input, t);
                                result  = poolItem.ProcessText(lti);
                                break;
                            }
                            catch (System.TimeoutException)
                            {
                                throw;
                            }
                            catch (ApplicationException)
                            {
                                throw;
                            }
                            catch (Exception ex)
                            {
                                Logger.ErrorFormat("Proc({0})\n{1}", poolItem.ProcessID, ex.ToString());
                                if (0 < maxRepeatTimes)
                                {
                                    continue;
                                }
                                throw;
                            }
                            finally
                            {
                                if (poolItem.Busy && !poolItem.Closed)
                                {
                                    _ProcessorPool.FreeItem(poolItem);
                                }
                                sw.Stop();
                                Logger.DebugFormat("Proc({0}) dT={1:F3}c, dT(GetItem)={2:F3}c, dT(Fun)={3:F3}c",
                                                   poolItem.ProcessID, sw.Elapsed.TotalSeconds, totalSecondsGetItem, sw.Elapsed.TotalSeconds - totalSecondsGetItem);
                            }
                        }//Повтор в случае ошибки [maxRepeatTimes] раз
                    }
                    catch (Exception ex)
                    {
                        Logger.ErrorFormat("TextPart={0}\n{1}", t, ex.Message);
                        throw;
                    }
                    return(new Tuple <LingvisticsResult, string>(result, t));
                }
                                              ).ToArray();

                if (rcList.Any(t => t.Item1 != null))
                {
                    //объединение частичных результатов
                    if (1 < blockList.Length)
                    {
                        var rdfIsEmpty = default(bool);
                        var rdf        = RDFUnifier.Union(rcList.Select(t => new Tuple <string, int>(((t.Item1 == null) ? null : t.Item1.RDF), t.Item2.Length)), out rdfIsEmpty);
                        var semNet     = SemnetUnifier.Union(rcList.Where(t => t.Item1 != null)
                                                             .Select(t => new Tuple <ThemeItem[], LinkItem[]>(t.Item1.ThemeList, t.Item1.LinkList))
                                                             );
#if WITH_OM_TM
                        if ((input.Options & LingvisticsResultOptions.OpinionMiningWithTonality) == LingvisticsResultOptions.OpinionMiningWithTonality)
                        {
                            var opinionMiningWithTonalityResult = TonalityResultUnifier.Union(
                                rcList.Select(t => new Tuple <DigestOutputResult, int>(
                                                  (t.Item1 == null) ? null : t.Item1.OpinionMiningWithTonalityResult, t.Item2.Length)
                                              )
                                );
                            return(new LingvisticsResult(input.Options, rdf, semNet.Item1, semNet.Item2, opinionMiningWithTonalityResult));
                        }
                        else
                        if ((input.Options & LingvisticsResultOptions.Tonality) == LingvisticsResultOptions.Tonality)
                        {
                            var tonalityResult = TonalityResultUnifier.Union(rdf, rdfIsEmpty,
                                                                             rcList.Select(t => (t.Item1 == null) ? null : t.Item1.TonalityResult)
                                                                             );
                            return(new LingvisticsResult(input.Options, rdf, semNet.Item1, semNet.Item2, tonalityResult));
                        }
#endif
                        return(new LingvisticsResult(input.Options, rdf, semNet.Item1, semNet.Item2));
                    }
                    else
                    {
                        return(rcList.First().Item1);
                    }
                }
                maxTextSize = maxTextSize >> 1;
            }
            return(null);
        }