Created
January 26, 2017 02:51
-
-
Save mekkanizer/5ff40ce0178fc314c79b85b12ea6c848 to your computer and use it in GitHub Desktop.
Mr. Dobos: N-GRAMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <unordered_map> | |
using namespace std; | |
void ljust(string &str, const size_t num, const char pch = 'X') { | |
if(num > str.size()) | |
str.insert(str.size(), num - str.size(), pch); | |
} | |
int main () { | |
string word; | |
int g_count; | |
bool read; // the "read" state switch | |
char c; // current symbol | |
int m; // choice | |
bool fromfile; // input method | |
string ifp; // input file path | |
ifstream fin; // input file | |
// I Choose method of input | |
cout << "Do I need to read from file? (y/N)"; | |
cin >> c; | |
fromfile = (c == 'y' || c == 'Y') ? true : false; | |
// II Input | |
if (fromfile) { | |
cout << "Enter file path\n"; | |
cin >> ifp; | |
fin.open(ifp); | |
if (!fin.is_open()) | |
cout<<"Error opening file\n"; | |
else { | |
word = ""; | |
g_count = 0; | |
read = false; | |
do { c = fin.get(); | |
if ((c != ' ')&&(c != EOF)) { | |
read = true; | |
if (isalpha(c)) | |
word.insert(word.size(), 1, c); | |
else | |
g_count = (g_count * 10) + (c - '0'); | |
} else if (read) { | |
read = false; | |
} | |
} while (c != EOF); | |
} | |
} else { | |
cout << "Input word: "; | |
cin >> word; | |
cout << "Input # of grams: "; | |
cin >> g_count; | |
} | |
// III Process & Output | |
for (int i = 1; i <= g_count; i++) { | |
unordered_map<string,int> grams; | |
cout << i << "-GRAM:"; | |
for (int j = 0; j < word.length(); j+=i) | |
{ | |
string current = word.substr(j, i); | |
if (current.length() < i) | |
ljust(current, i); | |
grams[current]++; | |
cout << current << ' '; | |
} | |
for (auto it : grams) | |
std::cout << " " << it.first << ":" << it.second; | |
cout << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment