-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspellcheck.cpp
152 lines (138 loc) · 3.62 KB
/
spellcheck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Spell Checker
*
* This application compares an input text file with a word dictionary
* to check for spelling mistakes in that file. If the word does not
* occur in the dictionary, it is printed to the console as an
* incorrectly spelled word.
*
* The word dictionary is read into a BinarySearchTree and the text
* input file is read into a vector. The program then loops over
* the words in the string vector and checks for occurrences in the
* BinarySearchTree.
*
* @author - Toby Cook (40316565)
* @version - 1.0.2
*
* Last modified - 15:00, 17/04/2018
*
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <cstring>
#include "bst.h"
/**
* Read in a file and return the contents to a string.
*
* @param {string} &filename - Takes reference to name of file read in
* @return {string} data - Returns a string containing data from text file
*
*/
std::string read_file(const std::string& filename)
{
std::ifstream file(filename);
if (!file)
{
return std::string("");
}
std::string data;
// Find total number of bytes of file
file.seekg(0, file.end);
data.resize(file.tellg());
file.seekg(0, file.beg);
// Read text into data string
file.read(&data[0], data.size());
file.close();
return data;
}
/**
* Take the data that has been read in from the file and
* split into tokens, adding them to words vector.
*
* @param {vector<string>} &words - Takes a reference to string vector to store each tokenized word
* @param {string} &data - Takes a reference to the data that has been read in from the input file
*
*/
void split_words(std::vector<std::string>& words, std::string& data)
{
const char* delim = " .,?!:;/\"\'\n\t";
// Set token to first word
char* token = strtok(&data[0], delim);
// Split rest of words
while (token != nullptr)
{
// Convert each word from dictionary to lower case
for (int i = 0; i < strlen(token); ++i)
{
char word = tolower(token[i]);
token[i] = word;
}
// Push word to end of vector
words.push_back(token);
// Get the next word
token = strtok(nullptr, delim);
}
}
int main(int argc, char** argv)
{
std::string file_to_check, file_data;
const std::string word_dictionary = "dictionary.txt";
BinarySearchTree* tree = new BinarySearchTree();
std::vector<std::string> words;
for (int i = 0; i < argc; ++i)
{
// Set file name if provided as argument
if (std::string(argv[i]) == "-i" && argv[i+1] != nullptr)
{
file_to_check = argv[i+1];
}
}
// Prompt user for file name
if (file_to_check.empty())
{
std::cout << "File name: ";
getline(std::cin, file_to_check);
std::cout << std::endl;
}
if (!file_to_check.empty())
{
// Read words from dictionary.txt into file_data string
file_data = read_file(word_dictionary);
// Split the words and store into vector
split_words(words, file_data);
// Insert words into Binary Search Tree
for (int i = 0; i < words.size(); ++i)
{
std::stringstream(words[i]) >> *tree;
}
// Store the data read from specified file
file_data = read_file(file_to_check);
// Split sentences and store each word in words vector
split_words(words, file_data);
std::cout << "\n";
int spell_count = 0;
// Loop through words vector and check if it exists in dictionary
for (int i = 0; i < words.size(); ++i)
{
// Print out non-occurring words
if (!tree->exists(words[i]))
{
spell_count++;
std::cout << words[i] << std::endl;
}
}
std::cout << "\n";
std::cout << spell_count << " spelling mistakes" << std::endl;
}
else
{
// If still no file specified, print message and exit
std::cerr << "No file specified!";
return 1;
}
// Free the memory
delete tree;
return 0;
}