这是问题。麻烦帮我参照下面的学长代码改成上面的
III. Spell checker
Now you are ready to implement a spell checker by using a linear or quadratic or double
hashing algorithm. Given a document, your program should output all of the correctly
spelled words, labeled as such, and all of the misspelled words. For each misspelled word
you should provide a list of candidate corrections from the dictionary, that can be formed by
applying one of the following rules to the misspelled word:
a) Adding one character in any possible position
b) Removing one character from the word
c) Swapping adjacent characters in the word
Your program should run from the command line as follows:
% ./spell_check <document file> <dictionary file>
You will be provided with a small document named document1_short.txt, document_1.txt,
and a dictionary file with approximately 370k words named wordsEnglish.txt.
As an example, your spell checker should correct the following mistakes.
deciive -> decisive (Case A)
deciasion -> decision (Case B)
ocuntry -> country (Case C)
Correct any word that does not exist in the dictionary file provided, (even if it is correct
in the English language).
这是问题给出的代码要求补充
#include "quadratic_probing.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int testSpellingWrapper(int argument_count, char** argument_list) {
const string document_filename(argument_list[1]);
const string dictionary_filename(argument_list[2]);
// Call functions implementing the assignment requirements.
// HashTableDouble<string> dictionary = MakeDictionary(dictionary_filename);
// SpellChecker(dictionary, document_filename);
return 0;
}
// Sample main for program spell_check.
// WE WILL NOT USE YOUR MAIN IN TESTING. DO NOT CODE FUNCTIONALITY INTO THE
// MAIN. WE WILL DIRECTLY CALL testSpellingWrapper. ALL FUNCTIONALITY SHOULD BE
// THERE. This main is only here for your own testing purposes.
int main(int argc, char** argv) {
if (argc != 3) {
cout << "Usage: " << argv[0] << " <document-file> <dictionary-file>"
<< endl;
return 0;
}
testSpellingWrapper(argc, argv);
return 0;
}
这是(学长)之前完成的代码不过就是格式不一样,我不会改
/*
Title: DoubleProbing.h
Author: Vlad Shostak
Created on: April 2, 2017
Description: This is the main driver program to test the spell checking portion
of the requirnment in the specification.
Modifications:
Notes:
*/
#include "QuadraticProbing.h"
#include "LinearProbing.h"
#include "DoubleProbing.h"
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
using namespace std;
// ./SpellCheck document1_short.txt wordsEn.txt
template <typename HashTableType>
void
printMisspelledWords(HashTableType &hash_table, const string &document_filename, const string &dictionary_filename) {
cout << "TestFunctionForHashTables..." << endl;
cout << "Document filename: " << document_filename << endl;
cout << "Dictionary filename: " << dictionary_filename << endl;
hash_table.MakeEmpty();
hash_table.resetCollisions();
ifstream document_file;
document_file.open(document_filename.c_str());
if (document_file.fail())
{
cerr << "Error opening document file\n";
}
ifstream dictionary_file;
dictionary_file.open(dictionary_filename.c_str());
if (dictionary_file.fail())
{
cerr << "Error opening dictionary file\n";
}
// ==================================
string line;
// store dictionary
while(dictionary_file.good()) {
getline(dictionary_file, line);
hash_table.Insert(line);
}
bool skip = false; // use to skip apostrophised words
// START check each word in document
while(document_file >> line) {
string letter = "";
string word = "";
// START get each word
for (int i = 0; i < line.length(); i++) {
letter = tolower(line[i]);
if(letter == "\'") {
skip = true; // skip apostrophised words
}
if ( (letter >= "a" && letter <= "z" ) ) {
word += letter;
}
}
// END get each word
// check word, 3 cases
string newWord = word;
if ( !skip && !hash_table.Contains(newWord)) {
// 1) add one char in each possible position
for(int i = 0; i < word.length() + 1; i++) {
// insert a - z
for (char ch = 'a';ch <= 'z'; ch++) {
newWord.insert(i, 1, ch);
if(hash_table.Contains(newWord)) {
cout << word << " -> " << newWord << endl;
}
newWord = word; // reset
}
}
newWord = word;
// 2) remove one character from the word
for(int i = 0; i < word.length(); i++) {
// begins at the character position pos and spans len characters
newWord.erase(i,1);
if(hash_table.Contains(newWord)) {
cout << word << " -> " << newWord << endl;
}
newWord = word; // reset
}
// 3) Swap adjacent characters in the word
for(int i = 0; i < word.length() - 1; i++) {
// begins at the character position pos and spans len characters
swap(newWord[i], newWord[i+1]);
if(hash_table.Contains(newWord)) {
cout << word << " -> " << newWord << endl;
}
newWord = word; // reset
}
} // end if
skip = false;
} // end while line
// END check each word in document
document_file.clear();
document_file.close();
dictionary_file.clear();
dictionary_file.close();
}
// Sample main for program CreateAndTestHash
int
main(int argc, char **argv) {
if (argc != 4) {
cout << "Usage: " << argv[0] << " <documentfilename> <dictionaryfilename> <flag>" << endl;
return 0;
}
const string words_filename(argv[1]);
const string query_filename(argv[2]);
const string param_flag(argv[3]);
if (param_flag == "linear") {
HashTableLinear<string> linear_probing_table;
printMisspelledWords(linear_probing_table, words_filename, query_filename);
} else if (param_flag == "quadratic") {
HashTable<string> quadratic_probing_table;
printMisspelledWords(quadratic_probing_table, words_filename, query_filename);
} else if (param_flag == "double") {
HashTableDouble<string> double_probing_table;
printMisspelledWords(double_probing_table, words_filename, query_filename);
} else {
cout << "Uknown tree type " << param_flag << " (User should provide linear, quadratic, or double)" << endl;
}
return 0;
}