// This function creates a list of Courses that match a given query or are close enough to be recognized by the spell-checker. // It also creates a string containing the spell-checked version of the user's query. // All these search results are placed in the instance of the SearchResults class called lastSearchResults, which can be accessed // using the getters. // The query can be a course name or course code. Courses that don't match the query at all are not included in the results. public void searchForQuery(string query) { if (string.IsNullOrWhiteSpace(query)) // For the user gives a blank query, display all the courses { lastSearchResults.correctedQuery = ""; lastSearchResults.query = ""; addAllCourses(); return; } string correctedQuery = ""; // Stores the spell-checked version of the user's query. List <List <int> > matching = new List <List <int> >(); // A list where each index represents a word in the query. For each word, there is a list of ints. These ints are the course IDs of courses containing that word in their name or course code. KeyValuePair <string, List <int> > result = new KeyValuePair <string, List <int> >(); // Stores the results from the spellchecker ///////////// Take query, split into words, and run spell checker on each word /////////////// List <string> querySplit = query.Trim().Split().ToList(); foreach (var word in querySplit) { // Needs tweaked: ? if (word.Length > 4 || (querySplit.Count == 1 && word.Length > 2)) // Doesn't correct words smaller than 5 characters, unless it's a single word query. Doing so could lead to bad search results. { result = spelling.CorrectExt(word, true); } else { result = spelling.CorrectExt(word, false); } // Future feature: For single letter queries, list classes whose first letter in course name/code matches letter? if (result.Value[0] != 0) // If the word in the query doesn't match anything in the dictionary and cannot be corrected with the spell-checker, don't add to the corrected query { correctedQuery += result.Key + " "; } matching.Add(result.Value.GetRange(0, result.Value.Count)); if (matching.Last().Count != 0) { matching.Last().RemoveAt(0); } } correctedQuery = correctedQuery.TrimEnd(' '); // Removes any extra spaces at the end lastSearchResults.correctedQuery = correctedQuery; // Setting correctedQuery in the lastSearchResults struct lastSearchResults.query = query; // Setting query in the lastSearchResults struct storeSearchResults(matching, querySplit, correctedQuery); // Calculate the relevance for each course that matched the query, and store that along with all the courses in results }