コード例 #1
0
        /// <summary>
        /// Semantic Size of the Paragraph
        /// </summary>
        public async Task <decimal> SSOTP(StructuredParagraph paragraph)
        {
            var terms      = paragraph.Terms.Distinct(new TermComparer()).ToList();
            var otherTerms = terms.ToList();

            var Re = 0m;

            foreach (var from in terms)
            {
                otherTerms.Remove(from);

                foreach (var to in otherTerms)
                {
                    Re += await SDIP(from.Id, to.Id, paragraph);
                }
            }

            if (Re == 0)
            {
                return(0);
            }

            //var R = 1m / Re;

            return(Re);
        }
コード例 #2
0
        /// <summary>
        /// Semantic Distance In Paragraph
        /// </summary>
        public async Task <decimal> SDIP(long from, long to, StructuredParagraph paragraph)
        {
            var L = await SDIO(from, to);

            var Ns = new Dictionary <long, long>()
            {
                { 0, 0 }, // речення
                { 2, 0 }, // параграф
            };

            var paragraphSentances = new List <StructuredSentence>();

            paragraphSentances.AddRange(paragraph.Sentences);

            foreach (var sentence in paragraph.Sentences)
            {
                if (sentence.Terms.Any(v => v.Id == from) && sentence.Terms.Any(v => v.Id == to))
                {
                    Ns[0] += sentence.Terms.Count(v => v.Id == from) * sentence.Terms.Count(v => v.Id == to);
                }

                paragraphSentances.Remove(sentence);
                var sentencesTerms = paragraphSentances.SelectMany(v => v.Terms);
                Ns[2] += sentence.Terms.Count(v => v.Id == from) * sentencesTerms.Count(v => v.Id == to)
                         + sentence.Terms.Count(v => v.Id == to) * sentencesTerms.Count(v => v.Id == from);
            }


            var Re = ((Ns[0] * 1m) / (L + 0m)) + ((Ns[2] * 1m) / (L + 2m));

            //var R = 1m / Re;

            return(Re);
        }
コード例 #3
0
        /// <summary>
        /// Return text splited to paragraphs, sentences and with terms
        /// </summary>
        public async Task <StructuredText> GetStructuredText(string text)
        {
            var response = new StructuredText
            {
                OriginalText = text,
                Paragraphs   = new List <StructuredParagraph>(),
                Terms        = new List <Term>()
            };

            var paragraphsSeperators = new Regex(@"[\n]");
            var sentenceSeperators   = new Regex(@"[.!?]");

            var paragraphsTexts = paragraphsSeperators.Split(text).ToList();

            // Поділ на речення, абзаци і тд
            foreach (var paragraphText in paragraphsTexts)
            {
                var paragraph = new StructuredParagraph
                {
                    OriginalText = paragraphText,
                    Sentences    = new List <StructuredSentence>(),
                    Terms        = new List <Term>()
                };

                var sentenceTexts = sentenceSeperators.Split(paragraphText);

                foreach (var sentenceText in sentenceTexts)
                {
                    var sentence = new StructuredSentence
                    {
                        OriginalText = sentenceText,
                        Terms        = new List <Term>()
                    };

                    paragraph.Sentences.Add(sentence);
                }

                response.Paragraphs.Add(paragraph);
            }

            //Отримання всіх термінів
            using (var session = _db.GetSession())
            {
                var result = await session.RunAsync(_transactions.GetTerms());

                var sentences = response.Paragraphs.SelectMany(v => v.Sentences).ToList();
                while (await result.FetchAsync())
                {
                    var item = new Term(result.Current[0] as INode);

                    foreach (var sentance in sentences)
                    {
                        if (IsUsing(item, sentance.OriginalText))
                        {
                            sentance.Terms.Add(item);
                        }
                    }
                }
            }

            foreach (var item in response.Paragraphs)
            {
                item.Terms = item.Sentences.SelectMany(v => v.Terms).ToList();
            }

            response.Terms = response.Paragraphs.SelectMany(v => v.Terms).ToList();

            return(response);
        }