-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWordCollection.cpp
More file actions
121 lines (105 loc) · 3.03 KB
/
Copy pathWordCollection.cpp
File metadata and controls
121 lines (105 loc) · 3.03 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
#include "WordCollection.h"
#include <QDebug>
#include "Word.h"
CWordCollection::CWordCollection(CLexicon * lexicon)
{
m_CorpusCount = 0;
m_MemberName = QString();
m_SortValidFlag = 0;
m_SortStyle = KEY;
m_Lexicon = lexicon;
}
CWordCollection::~CWordCollection()
{
if ( m_sorted_list.size() > 0 ) { m_sorted_list.empty(); }
}
//--------------------//
void CWordCollection::input_words(QMap<QString, int> word_counts){
for(auto i = word_counts.begin(), end = word_counts.end(); i != end; i++){
add_word(i.key(), i.value());
}
sort_word_lists();
}
bool new_reverse_string_compare(QString s1, QString s2)
{
std::reverse(s1.begin(), s1.end());
std::reverse(s2.begin(), s2.end());
return s1 < s2;
}
void CWordCollection::sort_word_lists()
{ m_sorted_list.clear();
m_end_sorted_list.clear();
m_sorted_list.reserve(m_WordMap.size());
foreach(QString word, m_WordMap.keys()){
m_sorted_list.append(word);
}
m_sorted_list.sort();
m_end_sorted_list.reserve(m_WordMap.size());
foreach(QString word, m_WordMap.keys()){
m_end_sorted_list.append(word);
}
std::sort(m_end_sorted_list.begin(),m_end_sorted_list.end(), new_reverse_string_compare);
}
//--------------------//
void CWordCollection::clear_all_words_parse_triple_maps(){
for (auto i = m_WordMap.cbegin(), end = m_WordMap.cend(); i != end; ++i ){
i.value()->clear_parse_triple_map();
}
}
//QMapIterator<QString,CWord*> * CWordCollection::get_iterator(){
// QMapIterator<QString, CWord*> * iter = new QMapIterator<QString,CWord*>(m_WordMap);
// return iter;
//}
int CWordCollection::get_token_count(){
int total = 0;
QMapIterator<QString, CWord* > iter(m_WordMap);
while(iter.hasNext())
{
iter.next();
total += iter.value()->count();
}
qDebug() << total;
return total;
}
CWord* CWordCollection::get_word(const int n){
return m_word_list[n];
}
int CWordCollection::get_word_count(){
return m_WordMap.count();
}
CWord* CWordCollection::find_or_add(const QString& word_string){
if (! m_WordMap.contains(word_string)) {
CWord* pWord = new CWord(word_string);
m_WordMap[word_string] = pWord;
m_word_list.append(pWord);
return pWord;
}else{
return m_WordMap[word_string];
}
}
CWord* CWordCollection::find_or_fail(const QString& word_string){
if (m_WordMap.contains(word_string)){
return m_WordMap[word_string];
}
return NULL;
}
CWord* CWordCollection::add_word(const QString& word, int count)
{ CWord* pWord;
if (m_WordMap.contains(word)){
m_WordMap[word]->SetWordCount(count);
return m_WordMap[word];
}
pWord = new CWord(word);
pWord->SetWordCount(count);
m_WordMap[word] = pWord;
m_word_list.append(pWord);
return pWord;
}
CWord* CWordCollection::operator <<(const QString& word)
{
return this->add_word(word);
}
CWord* CWordCollection::operator ^=(const QString& word)
{
return this->find_or_add(word);
}