/// <summary> /// forms a string containing text of a given word fragment and /// information about which lines the fragment occupies in the text /// </summary> /// <param name="lines">text split into lines containing the fragment</param> /// <param name="fragment">word fragment to take information from (start position, /// end position)</param> /// <returns>a new string with the word fragment content and information</returns> static string FormFragmentString(string[] lines, WordFragment fragment) { StringBuilder builder = new StringBuilder(); for (int i = fragment.StartLine; i <= fragment.EndLine; i++) { if (i == fragment.StartLine) { if (fragment.StartLine == fragment.EndLine) { builder.Append(lines[i].Substring(fragment.StartIndex, fragment.Length)); } else { builder.Append(lines[i].Substring(fragment.StartIndex)); } } else if (i == fragment.EndLine) { builder.Append(lines[i].Substring(0, fragment.EndIndex + 1)); } else { builder.Append(lines[i]); } builder.AppendLine(); } builder.Append(string.Format("Fragmentas yra eilutėse {0} - {1}.", fragment.StartLine + 1, fragment.EndLine + 1)); builder.AppendLine(); return(builder.ToString()); }
/// <summary> /// calculates the length (in characters) of word fragment /// </summary> /// <param name="lines">lines of text containing the fragment (only used if the /// fragment occupies more than one two lines)</param> /// <param name="fragment">word fragment to calculate length of</param> static void CalculateFragmentLength(string[] lines, WordFragment fragment) { for (int i = fragment.StartLine; i <= fragment.EndLine; i++) { if (i == fragment.StartLine) { if (fragment.StartLine == fragment.EndLine) { fragment.Length += fragment.EndIndex - fragment.StartIndex + 1; } else { fragment.Length += lines[i].Length - fragment.StartIndex; } } else if (i == fragment.EndLine) { fragment.Length += fragment.EndIndex + 1; } else { fragment.Length += lines[i].Length; } } }
/// <summary> /// find all word fragments, where the last word character /// matches the first character of the next word, in the text /// </summary> /// <param name="lines">text split into lines</param> /// <param name="words">all words of the text</param> /// <returns>List containing all word fragments</returns> static List <WordFragment> FindAllWordFragments(string[] lines, List <Word> words) { List <WordFragment> fragments = new List <WordFragment>(); bool inFragment = false; WordFragment fragment = null; Word lastWord = words[0]; for (int i = 1; i < words.Count; i++) { Word word = words[i]; if (!inFragment && Char.ToLower(lastWord.LastCharacter) == Char.ToLower(word.FirstCharacter)) { inFragment = true; fragment = new WordFragment(lastWord.Line, lastWord.StartIndex); } if (inFragment && Char.ToLower(lastWord.LastCharacter) != Char.ToLower(word.FirstCharacter)) { inFragment = false; CompleteFragmentAndAddToList(fragments, fragment, lastWord.Line, lastWord.EndIndex, lines); } else if (inFragment && i == words.Count - 1) { inFragment = false; CompleteFragmentAndAddToList(fragments, fragment, word.Line, word.EndIndex, lines); } lastWord = word; } return(fragments); }
/// <summary> /// completes (fills with correct information) /// a WordFragment object and adds it to a list containing all fragments /// </summary> /// <param name="fragments">list to store the fragment in</param> /// <param name="fragment">word fragment to be completed</param> /// <param name="endLine">ending line of the fragment</param> /// <param name="endIndex">ending index of the fragment</param> /// <param name="lines">text containing the fragment split into lines</param> static void CompleteFragmentAndAddToList(List <WordFragment> fragments, WordFragment fragment, int endLine, int endIndex, string[] lines) { fragment.EndLine = endLine; fragment.EndIndex = endIndex; CalculateFragmentLength(lines, fragment); fragments.Add(fragment); }
/// <summary> /// sorts word fragments by length in descending order /// </summary> /// <param name="fragments">list of fragments to sort</param> static void SortWordFragments(List <WordFragment> fragments) { for (int i = 0; i < fragments.Count - 1; i++) { for (int j = i + 1; j < fragments.Count; j++) { if (fragments[j] > fragments[i]) { WordFragment temp = fragments[i]; fragments[i] = fragments[j]; fragments[j] = temp; } } } }
/// <summary> /// removes all fragments from a list, except the ones with the highest length /// </summary> /// <param name="fragments">list to remove fragments from</param> static void KeepOnlyLongestFragments(List <WordFragment> fragments) { if (fragments.Count == 0) { return; } WordFragment longestFragment = fragments[0]; for (int i = 1; i < fragments.Count; i++) { if (fragments[i].Length < longestFragment.Length) { fragments.RemoveAt(i--); } } }