forked from Palamariuk/TextAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextanalyzer.cpp
190 lines (149 loc) · 4.3 KB
/
textanalyzer.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "textanalyzer.h"
TextAnalyzer::TextAnalyzer(Text text):
text_(text){}
QVector<QString> TextAnalyzer::splitIntoSentences()
{
return text_.text().split(QRegExp("[.?!]\\s*"),
Qt::SkipEmptyParts).toVector();
}
QVector<QString> TextAnalyzer::splitIntoWords()
{
return text_.text().split(QRegExp("\\W"),
Qt::SkipEmptyParts).toVector();
}
QVector<QPair<QString, int>> TextAnalyzer::getWordsPositions()
{
QString text = text_.text();
QVector<QString> words = splitIntoWords();
QVector<QPair<QString, int>> wordsWithPos;
int pos = 0;
for(; words.length() > 0; ){
QString word = words[0];
words.pop_front();
pos = text.indexOf(word, pos);
wordsWithPos.push_back(QPair<QString, int>(word, pos));
pos += word.length();
}
return wordsWithPos;
}
QVector<QString> TextAnalyzer::getIdenticalSentences()
{
auto sentences = splitIntoSentences();
std::map<QString, size_t> repeats;
QVector<QString> repeatedSentences;
for(auto sentence: sentences){
repeats[sentence]++;
}
for(auto sentence: repeats){
if(sentence.second > 1){
repeatedSentences.push_back(sentence.first);
}
}
return repeatedSentences;
}
QVector<QString> TextAnalyzer::getIdenticalProperNames()
{
QFile file(":/Resourses/Dictionary/proper_names.json");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
qDebug() << "File is not opened.";
}
QJsonObject json = QJsonDocument::fromJson(file.readAll()).object();
auto words = splitIntoWords();
words.erase(std::partition(words.begin(), words.end(),
[](const QString word){return word[0].isUpper();}),
words.end());
std::map<QString, size_t> repeats;
QVector<QString> repeatedWords;
for(auto word: words){
repeats[word]++;
}
for(auto word: repeats){
if(word.second > 1){
repeatedWords.push_back(word.first);
}
}
QVector<QString> properNames;
for(auto word: repeatedWords){
if(json[word].toString().toInt()){
properNames.push_back(word);
}
}
return properNames;
}
QVector<int> TextAnalyzer::findAll(QString substring)
{
QString text = text_.text();
QVector<int> positions;
int pos = 0;
while ((pos = text.indexOf(substring, pos)) != -1) {
positions.push_back(pos);
pos += substring.length();
}
return positions;
}
int TextAnalyzer::computeNumberOfLetters()
{
return text_.text().count(QRegExp("[a-zA-z]"));
}
double TextAnalyzer::computeAverageWordsLength()
{
double sum = 0;
auto words = splitIntoWords();
for(auto word: words){
sum += word.length();
}
return (sum / words.size());
}
double TextAnalyzer::computeAverageSentencesLength()
{
double sum = 0;
auto sentences = splitIntoSentences();
for(QString sentence: sentences){
sum += sentence.length();
}
return (sum / sentences.size());
}
int TextAnalyzer::computeNumberOfSymbol(char symbol)
{
return text_.text().count(symbol);
}
Text TextAnalyzer::generateRepeatNumbers()
{
auto words = getWordsPositions();
auto text = text_.text();
std::map<QString, size_t> repeats;
size_t offset = 0;
for(auto word: words){
repeats[word.first]++;
QString number = "<b style='color:gray'>("+
QString::number(repeats[word.first])+")</b>";
text.insert(word.second + word.first.length() + offset, number);
offset += number.length();
}
text.replace("\n", "<br>");
return Text(text);
}
int TextAnalyzer::computeNumberOfWords()
{
return splitIntoWords().size();
}
int TextAnalyzer::computeNumberOfSentences()
{
return splitIntoSentences().size();
}
int TextAnalyzer::computeMinWordsLength()
{
auto words = splitIntoWords();
int minLen = std::min_element(words.begin(), words.end(), [](QString a, QString b){
return a.length() < b.length();
})->length();
return minLen;
}
int TextAnalyzer::computeMaxWordsLength()
{
auto words = splitIntoWords();
int maxLen = std::max_element(words.begin(), words.end(), [](QString a, QString b){
return a.length() < b.length();
})->length();
return maxLen;
}