summaryrefslogtreecommitdiffstats
path: root/kmouth/wordcompletion/wordcompletion.cpp
blob: 7936044c66151e8eed2c1579ac0b8ca4c96d2ae5 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <tqregexp.h>
#include <tqfile.h>

#include <kapplication.h>
#include <kstandarddirs.h>
#include <kconfig.h>

#include "wordcompletion.h"

class WordCompletion::WordCompletionPrivate {
friend class WordCompletion;
private:
   typedef TQMap<TQString,int> WordMap;
   struct DictionaryDetails {
      TQString filename;
      TQString language;
   };

   TQString lastText;
   TQMap<TQString,int> map;
   TQMap<TQString,int> addedWords;
   TQMap<TQString,DictionaryDetails> dictDetails;
   TQStringList dictionaries;
   TQString current;
   bool blockCurrentListSignal;
   bool wordsToSave;
};

WordCompletion::WordCompletion() : KCompletion () {
   d = new WordCompletionPrivate();
   d->blockCurrentListSignal = false;
   d->wordsToSave = false;
   configure ();
}

WordCompletion::~WordCompletion() {
   save ();
   delete d;
}

typedef TQPair<int,TQString> Match;
typedef TQValueList<Match> MatchList;

TQString WordCompletion::makeCompletion(const TQString &text) {
   if (d->lastText != text) {
      d->lastText = text;
      KCompletion::clear();

      int border = text.findRev(TQRegExp("\\W"));
      TQString suffix = text.right (text.length() - border - 1).lower();
      TQString prefix = text.left (border + 1);

      if (suffix.length() > 0) {
         MatchList matches;
         TQMap<TQString,int>::ConstIterator it;
         for (it = d->map.begin(); it != d->map.end(); ++it)
            if (it.key().startsWith(suffix))
               matches += Match (-it.data(), it.key());
         qHeapSort(matches);

         MatchList::ConstIterator iter = matches.begin();
         for (int count = 0; (iter != matches.end()) && (count < 10); ++iter, ++count) {
            int length = (*iter).second.length() + prefix.length() - text.length();
            KCompletion::addItem(text + (*iter).second.right(length), -(*iter).first);
         }
      }
   }

   // call the KCompletion::makeCompletion(...) method
   return KCompletion::makeCompletion (text);
}

TQStringList WordCompletion::wordLists() {
   return d->dictionaries;
}

TQStringList WordCompletion::wordLists(const TQString &language) {
   TQStringList result;
   for (TQStringList::Iterator it = d->dictionaries.begin();
         it != d->dictionaries.end(); ++it)
      if (d->dictDetails[*it].language == language)
         result += *it;
   return result;
}

TQString WordCompletion::languageOfWordList(const TQString &wordlist) {
   if (d->dictDetails.contains(wordlist))
      return d->dictDetails[wordlist].language;
   else
      return TQString();
}

TQString WordCompletion::currentWordList() {
   return d->current;
}

bool WordCompletion::isConfigured() {
   KConfig *config = new KConfig("kmouthrc");
   bool result = config->hasGroup("Dictionary 0");
   delete config;

   return result;
}

void WordCompletion::configure() {
   if (d->wordsToSave)
      save ();
   d->wordsToSave = false;

   d->dictionaries.clear();
   d->dictDetails.clear();

   KConfig *config = new KConfig("kmouthrc");
   TQStringList groups = config->groupList();
   for (TQStringList::Iterator it = groups.begin(); it != groups.end(); ++it)
      if ((*it).startsWith ("Dictionary ")) {
         config->setGroup(*it);
         WordCompletionPrivate::DictionaryDetails details;
         details.filename = config->readEntry("Filename");
         details.language = config->readEntry("Language");
         TQString name = config->readEntry("Name");
         d->dictDetails[name] = details;
         d->dictionaries += name;
      }
   delete config;
   
   d->blockCurrentListSignal = true;
   setWordList(d->current);
   d->blockCurrentListSignal = false;
   emit wordListsChanged (wordLists());
   emit currentListChanged (d->current);
}

bool WordCompletion::setWordList(const TQString &wordlist) {
   if (d->wordsToSave)
      save ();
   d->wordsToSave = false;

   d->map.clear();
   bool result = d->dictDetails.contains (wordlist);
   if (result)
      d->current = wordlist;
   else
      d->current = d->dictionaries[0];
   
   TQString filename = d->dictDetails[d->current].filename;
   TQString dictionaryFile = TDEApplication::kApplication()->dirs()->findResource("appdata", filename);
   TQFile file(dictionaryFile);
   if (file.exists() && file.open(IO_ReadOnly)) {
      TQTextStream stream(&file);
      stream.setEncoding (TQTextStream::UnicodeUTF8);
      if (!stream.atEnd()) {
         if (stream.readLine() == "WPDictFile") {
            while (!stream.atEnd()) {
               TQString s = stream.readLine();
               if (!(s.isNull() || s.isEmpty())) {
                  TQStringList list = TQStringList::split("\t", s);
                  bool ok;
                  int weight = list[1].toInt(&ok);
                  if (ok && (weight > 0))
                     d->map [list[0]] = weight;
               }
            }
         }
      }
      file.close();
   }
   if (!d->blockCurrentListSignal)
      emit currentListChanged (d->current);
   d->lastText = "";
   d->wordsToSave = false;
   return result;
}

void WordCompletion::addSentence (const TQString &sentence) {
   TQStringList words = TQStringList::split(TQRegExp("\\W"), sentence);
   
   TQStringList::ConstIterator it;
   for (it = words.begin(); it != words.end(); ++it) {
      if (!(*it).contains(TQRegExp("\\d|_"))) {
         TQString key = (*it).lower();
         if (d->map.contains(key))
            d->map[key] += 1;
         else
            d->map[key] = 1;
         if (d->addedWords.contains(key))
            d->addedWords[key] += 1;
         else
            d->addedWords[key] = 1;
      }
   }
   d->wordsToSave = true;
}

void WordCompletion::save () {
   if (d->wordsToSave) {
      TQString filename = d->dictDetails[d->current].filename;
      TQString dictionaryFile = TDEApplication::kApplication()->dirs()->findResource("appdata", filename);
      TQFile file(dictionaryFile);
      if (!file.exists())
         return;
      if (!file.open(IO_WriteOnly))
         return;

      TQTextStream stream(&file);
      stream.setEncoding (TQTextStream::UnicodeUTF8);

      stream << "WPDictFile\n";
      TQMap<TQString,int>::ConstIterator it;
      for (it = d->map.begin(); it != d->map.end(); ++it) {
         if (d->addedWords.contains(it.key())) {
            stream << it.key() << "\t" << d->addedWords[it.key()] << "\t1\n";
            stream << it.key() << "\t" << it.data() - d->addedWords[it.key()] << "\t2\n";
         }
         else
            stream << it.key() << "\t" << it.data() << "\t2\n";
      }
      file.close();
      d->wordsToSave = false;
   }
}

#include "wordcompletion.moc"