//This function adds the current string in the Node public Boolean add(string current, string word) { //Basic validation if (current == null || current.Length == 0 || word == null || word.Length == 0) { return(false); } else { //Gets the edit distance between the current segment and the complete word int editDistance = LevisthanDistance.Compute(current, word); //If editDistance already is in the Dictionary of SimilarWords if (this.SimilarWords.ContainsKey(editDistance)) { //Get the listOfWords for the editDistance and add the complete word in the list List <String> listOfWords; this.SimilarWords.TryGetValue(editDistance, out listOfWords); listOfWords.Add(word); } else { //Create a new list, add the word and then add the distance and the list in the Dictionary List <String> newList = new List <String>(); newList.Add(word); this.SimilarWords.Add(editDistance, newList); } //Initially when added no word has been used yet this.Popularity.Add(word, 0); //if the current segment of letters is not already a child if (!this.Children.ContainsKey(current)) { //If the current segment has become equal to the current word, wordstatus = true otherwise false if ((current).Equals(word)) { this.Children.Add(current, new Node(current, true)); } else { this.Children.Add(current, new Node(current)); } } //If the child exists. else { //If it is word then change the wors status to true /* * This if statement allows me to add words from file in any order as long as the first letter * remains the same */ if (current.Equals(word)) { Node resultNode; this.Children.TryGetValue(current, out resultNode); resultNode.changeWordStatus(true); } } //If the word already exists as a child //Note: This marks the end of the function. i.e base case if (this.Children.ContainsKey(word)) { return(true); } else { /* * resultNode is the node of the current segment of letters * For e.g: current=accom word=accommodate then at this point the first parameter being passed * in this recursive call is accomm */ Node resultNode; this.Children.TryGetValue(current, out resultNode); return(resultNode.add(current + word.Substring(current.Length, 1), word)); } } }
//This function generates the list of possible replacment words for the incorrect segment. //User doesn't needs access to this function //sensitvity determines whether or not to add a word in the list //Note: correctInitialSegment node has to exist in the data structure private HashSet <String> generateSuggestionList(string incorrectWord, Node correctInitialSegment) { //In case the segment doesn't exists Node temp; if (!this.LookUp(correctInitialSegment.getWord(), out temp)) { return(new HashSet <String>()); } //The list that will be returned HashSet <String> finalListOfWords = new HashSet <String>(); //Edit distance for the incorrect word and the correct initial segment int dis = LevisthanDistance.Compute(incorrectWord, correctInitialSegment.getWord()); //In case the dis is less or the more than the value of all editDistances available in the word segment if (dis < correctInitialSegment.minDistance()) { dis = correctInitialSegment.minDistance(); } else { if (dis > correctInitialSegment.maxDistance()) { dis = correctInitialSegment.maxDistance(); } } //Gets the closest editDistance available if the dis calculated above is not in the SimilarWords //This while loop while iterate until a closest editDistance is not found //maxChecker checkes whether dis has reached max value in the list or not bool maxChecker = false; while (!correctInitialSegment.hasEditDistance(dis)) { //First tries to get the closest greater editDistance if (!maxChecker) { ++dis; } //In case dis reaches the max value in list of the SimilarWords Dictionary if (dis == correctInitialSegment.maxDistance()) { maxChecker = true; } //Tries to get the lowest and the closest value if (maxChecker) { --dis; } } //This value determines whether or not to add a word in the list int sensitivity = 3; //Gets all the list of words which have the same edit distance from the correct segment List <String> choices = correctInitialSegment.getListOfSimilarWords(dis); //Will only iterate until there is atleast one word in the list while (finalListOfWords.Count == 0) { //Iterating over the list of the words retrived above for (int run = 0; run < choices.Count; run++) { //Current word at index run string curWord = choices.ElementAt(run); //If the edit distance between the incorrect word and the word above is less than sensitivty if (LevisthanDistance.Compute(incorrectWord, curWord) < sensitivity) { finalListOfWords.Add(curWord); } //Prevents list from becoming too big if (finalListOfWords.Count == 10) { break; } } ++sensitivity; //Prevents list from becoming too big if (finalListOfWords.Count == 10) { break; } } return(finalListOfWords); }