This repository was archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.cpp
More file actions
153 lines (125 loc) · 2.78 KB
/
analysis.cpp
File metadata and controls
153 lines (125 loc) · 2.78 KB
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
/**
* Text analysis
*
* Jacob Sanchez Perez (G20812080) <jsanchez-perez@uclan.ac.uk>
* University of Central Lancashire
*
* analysis.cpp
* 2021-04-08
*/
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include "analysis.h"
#include <iostream>
using namespace std;
using namespace sweary;
// Divides a string into words
vector<string> TextAnalysis::extractWords(string text)
{
vector<string> allWords;
string buffer;
bool bufferActive = false;
buffer = "";
for (char c : text)
{
if (isalnum(c))
{
// ;)
bufferActive = true;
buffer.append(true, c);
} else
{
if (bufferActive)
{
if (c != '\'')
{
bufferActive = false;
allWords.push_back(buffer);
buffer = "";
}
}
}
}
if (buffer != "")
allWords.push_back(buffer);
return allWords;
}
// Get map of word frequency
wordmap TextAnalysis::getWordMap(vector<string> wordlist)
{
wordmap w;
for (string word : wordlist)
{
for (char& c : word)
{
c = tolower(c);
}
// Create key if it doesn't exist
if (!w.count(word))
{
w[word] = 0;
}
++w[word];
}
return w;
}
// Get vector of word frequency sorted by frequency, then alphabetically
vector<TextAnalysis::WordFrequency> TextAnalysis::wordMapToSortedVector(wordmap wordlist)
{
vector<TextAnalysis::WordFrequency> wf;
for (auto const &word : wordlist)
{
wf.push_back({ word.first, word.second });
}
// Sort by descending order, frequency as main criteria and alphabetical order as second
sort(wf.begin(), wf.end(), greater<TextAnalysis::WordFrequency>());
return wf;
}
// Get map of word length frequency
wordLengthMap TextAnalysis::analyseWordLength(vector<string> wordlist)
{
wordLengthMap lengthMap;
for (string word : wordlist)
{
// Create key if it doesn't exist
if (!lengthMap.count(word.size()))
{
lengthMap[word.size()] = 0;
}
++lengthMap[word.size()];
}
return lengthMap;
}
// Average word length of word list
double TextAnalysis::averageWordLength(vector<string> wordlist)
{
int sum = 0;
for (string word : wordlist)
{
sum += word.size();
}
return (sum / (double) wordlist.size());
}
// Get map of char frequency
charmap TextAnalysis::getCharMap(vector<string> wordlist)
{
charmap cm;
for (string word : wordlist)
{
for (char& c : word)
{
// Treat upper and lower case versions as a single letter
c = tolower(c);
// Create key if it doesn't exist
if (!cm.count(c))
{
cm[c] = 0;
}
// Increase times that letter was found
++cm[c];
}
}
return cm;
}