/* * Public, so it can be called from Form1.cs. * Searches the database for the synonyms of * the search terms. Creates an List of HashSet's of the search * terms and their synonyms. * @params searchTerms array and newWordsFullDataSet * @returns List<HashSet<string>> newSearch. */ public List <HashSet <string> > SearchWithDb(string[] searchTerms) { List <HashSet <string> > newSearch = new List <HashSet <string> >(); //initialises and declares the List of HashSets to hold searchTerms and their synonyms. //Returned at end of method. try //handles database call errors. { foreach (string s in searchTerms) //iterates over passed in searchTerm array { NewWordsFullDataSet.WordsRow wordsRow = newWordsFullDataSet.Words.FindByWord(s); //gets the row with s as index if (wordsRow != null) //checks that a row is retrieved. { HashSet <string> term = new HashSet <string>(); //HashSet to add to List term.Add(s); //Adds searchTerm s to HashSet. string[] synList = wordsRow.Synonyms.ToString().Split(','); //splits the word row at , and stores them in an array. for (int j = 0; j < synList.Length; j++) //iterates over the synonyms array, length - 1 times. { term.Add(synList[j]); //Adds the synonym to the term HashSet. } newSearch.Add(term); //Adds the HashSet to the List } } } catch (Exception err) { MessageBox.Show("" + err); } return(newSearch); //returns the List<HashSet<string>> containing searchTerms and their synonyms. }
/* * Public so it can be called from Form1.cs. * Queries the DataSet for the synonyms indexed by the passed in variable * word. Splits the synonym csv and reads it into string array. * @params word, newWordsFullDataSet * @return string str */ public string Query(string word) { string str = ""; //the string variable that is returned. try //handles database errors. { NewWordsFullDataSet.WordsRow wordsRow = newWordsFullDataSet.Words.FindByWord(word); //sets wordsRow to the dataSet row indexed by passed in variable word. if (wordsRow == null) { MessageBox.Show(word + " is not in database"); } if (wordsRow != null)//fires if word is an index in DataSet. { string[] synList = wordsRow.Synonyms.ToString().Split(','); //sets an Array to the split result for the synonym csv. foreach (string s in synList)//iterates over synList array { str += s + "\r\n"; //sets str to a series of string varaibles, that will display on new lines. } } } catch (Exception err) { MessageBox.Show("" + err); } return(str); //returns the string of synonyms to be displayed in QuerySynonym textbox. }
/* * Public so it can be called from Form.cs * Updates the synonym list in the DataSet, * for the word indexed by passed in variable word, * with the passed in variable syn. * @params word, syn and newWordsFullDataSet. */ public void UpdateWord(String word, String syn) { try//handles database errors { NewWordsFullDataSet.WordsRow wordsRow = newWordsFullDataSet.Words.FindByWord(word); if (wordsRow == null) { MessageBox.Show(word + " is not in database"); } if (wordsRow != null) { //sets wordsRow to the DataSet row indexed by word. wordsRow.Synonyms += "," + syn; //adds to the list of existing synonyms in wordsRow. MessageBox.Show("Synonym added to " + word + " synonym list"); } } catch (Exception err) { MessageBox.Show("" + err); } }
/* * Public so it can be called from Form1.cs * Deletes the row the passed in varaible * delWord indexes, from the database. * @params delWord, newWordsFullDataSet. */ public void DeleteWord(String delWord) { try//handles database errors { NewWordsFullDataSet.WordsRow wordsRow = newWordsFullDataSet.Words.FindByWord(delWord); if (wordsRow == null) { MessageBox.Show(delWord + " is not in database"); } if (wordsRow != null) { //sets dataSet newRow to the row in the dataSet indexed by delWord wordsRow.Delete();//deletes from the dataSet. MessageBox.Show(delWord + " and synonyms deleted"); } } catch (Exception err) { MessageBox.Show("Error occured " + err); } }