Ejemplo n.º 1
0
        private void calculateScore(RankedSelectedMeaning meaning, TextSplitted textSplitted)
        {
            meaning.Score = 0.0;
            StringBuilder explanationSB = new StringBuilder();

            explanationSB.Append("Main paragraph:");
            addParagraphScore(meaning, textSplitted.MainParagraph, 0, explanationSB);

            List <List <string> > paragraphsToCheck = new List <List <string> > {
                textSplitted.LeftParagraphs, textSplitted.RightParagraphs
            };
            bool leftParagraph = true;

            foreach (List <string> paragraphsList in paragraphsToCheck)
            {
                int distance = 0;
                foreach (string paragraph in paragraphsList)
                {
                    distance++;
                    explanationSB.AppendFormat("{2}{0} paragraph {1}:", leftParagraph ? "Left" : "Right", distance, Environment.NewLine);
                    addParagraphScore(meaning, paragraph, distance, explanationSB);
                }
                leftParagraph = false;
            }

            explanationSB.AppendFormat("{1}Total score: {0}", meaning.Score, Environment.NewLine);
            meaning.Explanation = explanationSB.ToString();
        }
Ejemplo n.º 2
0
        private TextSplitted getTextSplitted(FoundAcronym acronym, string text)
        {
            TextSplitted ret = new TextSplitted();

            string          leftText = StringTreatment.NormalizeText(text.Substring(0, acronym.Index));
            MatchCollection newLines = Regex.Matches(leftText, Environment.NewLine, RegexOptions.RightToLeft);

            if (newLines.Count == 0)
            {
                ret.MainParagraph = leftText.Trim();
            }
            else
            {
                ret.MainParagraph = leftText.Substring(newLines[0].Index).Trim();
                leftText          = leftText.Substring(0, newLines[0].Index);
                for (int i = 1; i <= _paragraphsAround && i <= newLines.Count; i++)
                {
                    string nextParagraph = leftText.Substring(i < newLines.Count ? newLines[i].Index : 0);
                    if (!string.IsNullOrWhiteSpace(nextParagraph))
                    {
                        ret.LeftParagraphs.Add(nextParagraph.Trim());
                    }
                    if (i < newLines.Count)
                    {
                        leftText = leftText.Substring(0, newLines[i].Index);
                    }
                }
            }

            ret.MainParagraph += " "; // +acronym.Caption + " ";

            string rightText = StringTreatment.NormalizeText(text.Substring(acronym.Index + acronym.Caption.Length));

            newLines = Regex.Matches(rightText, Environment.NewLine);
            if (newLines.Count == 0)
            {
                ret.MainParagraph += rightText.Trim();
            }
            else
            {
                ret.MainParagraph += rightText.Substring(0, newLines[0].Index).Trim();
                int indexOfLastLineBreak = newLines[0].Index;
                for (int i = 1; i <= _paragraphsAround && i <= newLines.Count; i++)
                {
                    string nextParagraph = rightText.Substring(indexOfLastLineBreak, i < newLines.Count ? (newLines[i].Index - indexOfLastLineBreak)
                                                                                                        : rightText.Length - indexOfLastLineBreak);
                    if (!string.IsNullOrWhiteSpace(nextParagraph))
                    {
                        ret.RightParagraphs.Add(nextParagraph.Trim());
                    }
                    if (i < newLines.Count)
                    {
                        indexOfLastLineBreak = newLines[i].Index;
                    }
                }
            }

            return(ret);
        }
Ejemplo n.º 3
0
        public override SelectedMeaningBase SelectMeaning(FoundAcronym acronym, string text)
        {
            List <RankedSelectedMeaning> meanings = acronym.Acronym.Meanings.Select(x => new RankedSelectedMeaning(x)).ToList();

            if (_criteriaOrder.Contains(RankingCriteria.Score))
            {
                TextSplitted textSplitted = getTextSplitted(acronym, text);
                foreach (RankedSelectedMeaning meaning in meanings)
                {
                    calculateScore(meaning, textSplitted);
                }
            }

            return(sortByCriteriaOrder(meanings).FirstOrDefault());
        }