-
Notifications
You must be signed in to change notification settings - Fork 0
/
accuracy.cpp
160 lines (142 loc) · 4.79 KB
/
accuracy.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
153
154
155
156
157
158
159
160
//NAME:CANNOO Keelan
//Student ID: 2012638
//Programme: BSc (Hons) Computer Science L1, Group A
//Module: Computer Programming, ICT 1017Y
//DATE: 28 MAY 2021
//Sentence accuracy: 0.1 %
//Word accuracy: 21 %
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class Comparator
{
private:
//vector variable of type string to store the manually sentences and sentences which program has translated
vector <string> manuallyTranslated;
vector <string> programTranslated;
public:
Comparator(vector <string> mT, vector <string> pT);
//break into words
vector <string> getWords(string sentence);
//compare the two vectors and display percentage similarity for sentences
void SentenceAccuracy();
//compare the two vectors and display percentage similarity for words
void WordAccuracy();
};
Comparator::Comparator(vector <string> mT, vector <string> pT)
{
manuallyTranslated=mT;
programTranslated=pT;
}
vector <string> Comparator::getWords(string sentence)
{
vector <string> words;
string currentWord;
words.clear();
for(int i=0;i<sentence.length();i++){
if(sentence[i] == ' ' || sentence[i] == '.' || sentence[i] == '!' || sentence[i] == '?'|| sentence[i] == ','|| sentence[i] == ';'|| sentence[i] == ':')
{
if (currentWord.size()!=0)
words.push_back(currentWord);
currentWord.clear();
}
else
{
currentWord += tolower(sentence[i]);
}
}
return words;
}
void Comparator::SentenceAccuracy()
{
//variable to store manually translated words from sentence
vector <string> manualWords;
vector <string> programWords;
bool correct=true;
int counter=0;
//comparing sentence
for (int i=0;i<manuallyTranslated.size();i++)
{
manualWords=getWords(manuallyTranslated[i]);
programWords=getWords(programTranslated[i]);
correct=true;
//if number of words don't match, it means sentences are different!
if (manualWords.size()!=programWords.size())
continue;
//comparing words of a sentence
for (int j=0;j<manualWords.size();j++){
if (manualWords[j]!=programWords[j]){
correct=false;
break;
}
}
if (correct==true){
counter++;
for (int l=0;l<manualWords.size();l++)
{
cout<<manualWords[l];
if (l==manualWords.size()-1)
cout<<"."<<endl;
else
cout<<" ";
}
}
}
cout<<counter<<" out of "<<programTranslated.size()<<" sentences accurate"<<endl ;
cout<<"Sentence Accuracy (%): "<<(100.0*counter/manuallyTranslated.size())<<endl;
}
void Comparator::WordAccuracy()
{
//variable to store manually translated words from sentence
vector <string> manualWords;
vector <string> programWords;
int counter=0;
int tot=0;
int loopcounter=0;
//comparing sentence
for (int i=0;i<manuallyTranslated.size();i++)
{
manualWords=getWords(manuallyTranslated[i]);
programWords=getWords(programTranslated[i]);
//the size of the sentence having the lesser number of words is used for the loop counter so that index is not out of range
if (manualWords.size()<programWords.size())
loopcounter=manualWords.size();
else
loopcounter=programWords.size();
//comparing words of a sentence
for (int j=0;j<loopcounter;j++){
tot++;
if (manualWords[j]==programWords[j])
counter++;
}
}
cout<< "Word Accuracy (%): "<< (100.0*counter/tot)<<endl;
}
int main()
{
//vector variables to store all the sentences from the dataset
vector<string> manualSentences;
vector<string> ProgramSentences;
//temporary string variable to store each sentence from the dataset
string sentenceFromFile;
//load sentences from translated (by program) file into vector
ifstream infile("Translated.txt");
while (!infile.eof())
{
getline(infile,sentenceFromFile);
ProgramSentences.push_back(sentenceFromFile);
}
infile.close();
//load sentences from manually translated file into a vector
ifstream infile1("1000_KM_A_110521_Test.txt");
while (!infile1.eof())
{
getline(infile1,sentenceFromFile);
manualSentences.push_back(sentenceFromFile);
}
infile1.close();
Comparator optimus(manualSentences,ProgramSentences);
optimus.SentenceAccuracy();
optimus.WordAccuracy();
}