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
|
#include <iostream>
#include <fstream>
#include <map>
#include <utility> // for pair
using namespace std;
typedef map<pair<string, string>, int> StrPairIntMap;
typedef map<string, int> StrIntMap;
typedef map<pair<string, string>, double> StrPairDoubleMap;
StrPairDoubleMap CalcBigrams(ifstream&);
void PrintBigrams(StrPairDoubleMap&, string, string);
int main(int argc, char* argv[])
{
if(argc < 2) // checking to see if 2 arguments were passed from command line
{
cout << "TOO FEW ARGUMENTS\n";
return 0;
}
ifstream file(argv[1]) // delcare and open file
if(!file) // check if file opened
{
cout << "FILE NOT OPENED";
return 0;
}
string w1, w2;
StrPairDoubleMap m3 = CalcBigrams(file);
while(true)
{
cout << "Enter two words (q to quit): ";
cin >> w1;
if(w1 == "q") break;
cin >> w2;
if (w2 == "q")break;
PrintBigrams(m3, w1, w2);
}
file.close();
return 0;
}
// This function calculates the probabilities of a pair of words appearing
// using the formula freq(w1, w2)/freq(w1)
StrPairDoubleMap CalcBigrams(ifstream& file)
{
string word1, word2;
StrPairIntMap m1;
StrIntMap m2;
StrPairDoubleMap m3;
// while loop reads in words until end of file
file >> word1;
while(file)
{
file >> word2;
m1[make_pair(word1, word2)]++; // insert pair and increment int value
m2[word1]++; // insert word and increment int value
word1 = word2;
}
StrPairIntMap::iterator it1 = m1.begin();
double prob;
// loop until end of m1
while(it1 != m1.end())
{
prob = (1.0*it1->second)/m2[it1->first.first]; // int value / int value
m3[it1->first] = prob; // insert word1 to m3, set double value to prob
it1++;
}
return m3;
}
//This function simply prrints the probability of pair of words
void PrintBigrams(StrPairDoubleMap& m3, string w1, string w2)
{
cout << m3[make_pair(w1, w2)] << endl;
}
|