Skip to content

Instantly share code, notes, and snippets.

@AntonisFK
Last active September 19, 2016 18:04
Show Gist options
  • Save AntonisFK/bd25e0a3b45104e2425b89dcf7e6e0fe to your computer and use it in GitHub Desktop.
Save AntonisFK/bd25e0a3b45104e2425b89dcf7e6e0fe to your computer and use it in GitHub Desktop.
#include <iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
struct TokenFreq {
string token;
int freq;
};
vector<string> getWords(string something);
vector<TokenFreq> getTokenFreq( string sentence);
int checkFreq(string word, vector<TokenFreq>* words);
void printFreq(vector<TokenFreq> Freq);
void selectionSort(vector<TokenFreq> &freqList);
vector<TokenFreq> selectionSortV2(vector<TokenFreq> freqList);
int main(){
string something ="And no, I'm not a walking C++ dictionary. I do not keep every technical detail in my head at all times. If I did that, I would be a much poorer programmer. I do keep the main points straight in my head most of the time, and I do know where to find the details when I need them. by Bjarne Stroustrup";
vector<TokenFreq> TokeFreq;
vector<TokenFreq> Toke;
TokeFreq = getTokenFreq(something);
// printFreq(TokeFreq);
// sort
// selectionSort(TokeFreq);
// printFreq(TokeFreq);
Toke = selectionSortV2(TokeFreq);
printFreq(Toke);
return 0;
}
vector<string> getWords(string sentence)
{
vector<string> words;
string temp;
stringstream s (sentence);
while(s>>temp)
words.push_back(temp);
return words;
};
int checkFreq(string word, vector<TokenFreq> words)
{
// cout<<words.size() <<"<-checkFreq size"<<endl;
for(int i=0; i<words.size(); i++)
{
// cout<<word + "<-- word to compare "+ words[i].token + "<--- token"<<endl;
if(word == words[i].token)
{
// cout<<"found"<< i<< endl;
return i;
}
}
// cout<<"didnt find"<<endl;
return -1;
};
vector<TokenFreq> getTokenFreq( string sentence)
{ vector<string> words;
vector<TokenFreq> frequences;
int index;
TokenFreq tokenFeq;
words = getWords(sentence);
for(int i=0; i<words.size();i++)
{
index=checkFreq(words[i],frequences);
// cout<<index <<"<--status"<<endl;
if(index == -1)
{
tokenFeq.token = words[i];
tokenFeq.freq = 1;
frequences.push_back(tokenFeq);
// cout<<"pushed"<<endl;
// cout<< frequences.size() <<"<----freq Size" <<endl;
}
else
{
frequences[index].freq ++;
// cout<<tokenFeq.freq<<"added freq"<<tokenFeq.token<<"<---token"<<endl;
}
}
return frequences;
};
void printFreq(vector<TokenFreq> freq)
{
for(int i=0; i<freq.size(); i++)
{
cout<<freq[i].token << ":token "<<freq[i].freq<<":freq"<<endl;
}
}
//swapping function
void swap(TokenFreq* a, TokenFreq* b){
cout<<"swap"<<endl;
TokenFreq t =*a;
*a = *b;
*b =t;
}
void selectionSort(vector<TokenFreq> &freqList)
{
int index;
int smallestIndex;
int location;
int length = freqList.size();
TokenFreq temp;
for (index = 0; index < length - 1; index++) {
// step a (find smallest element)
smallestIndex = index;
for (location = index + 1; location < length; location++) {
if (freqList[location].freq < freqList[smallestIndex].freq) {
smallestIndex = location;
}
}
// step b (swap elements)
temp = freqList[smallestIndex];
freqList[smallestIndex] = freqList[index];
freqList[index] = temp;
}
}
vector<TokenFreq> selectionSortV2(vector<TokenFreq> freqList)
{
vector<TokenFreq> sortedFreq;
int min = 0;
int index =0;
while(index != freqList.size()-1)
{
for(int i=0; i<freqList.size(); i++)
{
if(freqList[i].freq < freqList[min].freq )
{
min =i;
}
}
sortedFreq.push_back(freqList[min]);
index ++;
min = index;
}
return sortedFreq;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment