Skip to content

Instantly share code, notes, and snippets.

@d3v-null
Last active May 20, 2016 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3v-null/f64699f8146375ca9f0ab14b75115081 to your computer and use it in GitHub Desktop.
Save d3v-null/f64699f8146375ca9f0ab14b75115081 to your computer and use it in GitHub Desktop.
#include <string>
#include <map>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
// #include <pair>
using namespace std;
typedef pair<char, char> char_pair;
typedef map<char, int> char_count;
typedef pair<char, int> char_count_item;
typedef map<char_pair, char_count> adj_counter;
bool sort_char_count_item(char_count_item a, char_count_item b){
return (a.second < b.second);
}
// typedef unordered_map<char, int> char_counter;
class AdjacencyCounter{
public:
adj_counter counter;
AdjacencyCounter(){
clearCounter();
}
void clearCounter(){
//clear counter map
counter.clear();
}
char sanitizeChar(char unsanitized){
if(unsanitized >= 'A' && unsanitized <= 'Z'){
return unsanitized;
} else if (unsanitized >= 'a' && unsanitized <= 'z'){
return (char)(unsanitized - 'a' + 'A');
} else {
return ' ';
}
}
char_pair makeHead(char first, char second){
return make_pair(sanitizeChar(first), sanitizeChar(second));
}
bool hasTailCounts(char_pair head){
return (bool)(counter.count(head));
}
int getTotalTailCount(char_pair head){
if(hasTailCounts(head)){
char_count tailCounts = counter[head];
int total = 0;
for(char_count::iterator it = tailCounts.begin(); it != tailCounts.end(); ++it){
total += it->second;
}
return total;
} else {
return 0;
}
}
int getTotalTailCount(char first, char second){
return getTotalTailCount(makeHead(first, second));
}
// char_count getTailCounts(char_pair head){
// if(hasTailCounts(head)){
// return counter[head];
// } else {
// return
// }
// }
void incrementCounter(char first, char second, char third){
char_pair head = makeHead(first, second);
char tail = sanitizeChar(third);
if(!hasTailCounts(head)){
counter[head][tail] = 0;
}
counter[head][tail]++;
}
// void incrementCounterAtPointer(char* charPointer){
// incrementCounter(
// charPointer[0],
// charPointer[1],
// charPointer[2]
// )
// }
void incrementCounterFromString(string input){
char first = '\0', second = '\0', third = '\0';
for(string::iterator it = input.begin(); it != input.end(); ++it){
first = second;
second = third;
third = *it;
incrementCounter(first, second, third);
}
}
int getCount(char first, char second, char third){
char_pair head = makeHead(first, second);
if(hasTailCounts(head)){
char tail = sanitizeChar(third);
return counter[head][tail];
} else {
return 0;
}
}
char getMostLikelyTail(char_pair head){
if(hasTailCounts(head)){
char_count tailCounts = counter[head];
char_count::iterator it;
it = max_element(tailCounts.begin(), tailCounts.end(), sort_char_count_item);
return it->first;
// int largestCount = 0;
// char largestVal = '\0';
// for(char_count::iterator it = tailCounts.begin(); it != tailCounts.end(); ++it){
// if (it->second > largestCount){
// largestVal
// }
// }
} else {
return ' ';
}
}
char getMostLikelyTail(char first, char second){
return getMostLikelyTail(makeHead(first, second));
}
};
int main(int argc, char const *argv[]) {
// char_pair pair;
// pair = make_pair('a', 'b');
// cout << pair.first << pair.second << endl;
// adj_counter counter;
// counter[make_pair('a','a')]['a'] = 1;
// cout << counter[make_pair('a','a')]['b'] << endl;
AdjacencyCounter adjacencyCounter;
cout << "count of aaa before increment: " << adjacencyCounter.getCount('a', 'a', 'a') << endl;
adjacencyCounter.incrementCounter('a', 'a', 'a');
cout << "count of aaa after increment: " << adjacencyCounter.getCount('a', 'a', 'a') << endl;
cout << "total count of aa: " << adjacencyCounter.getTotalTailCount('a', 'a') << endl;
adjacencyCounter.clearCounter();
cout << "count of aaa before string parse: " << adjacencyCounter.getCount('a', 'a', 'a') << endl;
adjacencyCounter.incrementCounterFromString("Hello. This is a test string to ensure that everything works properly. I hope that this can be iterated through. Thankyou Derwent for your valiant efforts, I truly appreciate it, and this sentence can probably end now.");
cout << "count of aaa after string parse: " << adjacencyCounter.getCount('a', 'a', 'a') << endl;
char letter1;
char letter2;
cout << "enter letter 1: ";
cin >> letter1;
cout << "enter letter 2: ";
cin >> letter2;
cout << "count for that combo:" << adjacencyCounter.getTotalTailCount(letter1, letter2) << endl;
cout << "most likely tail: " << adjacencyCounter.getMostLikelyTail(letter1, letter2) << endl;
return 0;
}
#include <iostream>
using namespace std;
string crap = "sdlkjSD(*&(*&FA";
char sanitizeChar(char unsanitized){
if(unsanitized >= 'A' && unsanitized <= 'Z'){
return unsanitized;
} else if (unsanitized >= 'a' && unsanitized <= 'z'){
return (char)(unsanitized - 'a' + 'A');
} else {
return ' ';
}
}
int main(int argc, char const *argv[]) {
char unsanitized = '\0';
int i = 0;
do {
unsanitized = crap[i++];
cout << unsanitized << " | " << sanitizeChar(unsanitized) << endl;
} while(unsanitized != '\0');
return 0;
}
In a separate file, create a class that encapsulates the following functionality:
From any given text document, calculate the probability of any character
occurring after any combination of two characters (including where the first
character is a space, i.e. the start of a word) and store these probabilities.
Based on an input of two characters, return the three most likely characters to occur after the two characters.
Use the class to generate 10 random words which sound like English words, but do not exist in the dictionary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment