File: textanalysis.cpp

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (463 lines) | stat: -rw-r--r-- 16,011 bytes parent folder | download
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "textanalysis.h"

#include "smallUsefulFunctions.h"
#include "latexdocument.h"

#include "qeditor.h"
#include "qdocument.h"
#include "qdocumentline.h"

#include <QFileDialog>

//#include "latexeditorview.h"
//#include <QMessageBox>
ClsWord::ClsWord(QString nw, int nc)
{
	word = nw;
	count = nc;
}

bool ClsWord::operator<(const  ClsWord &cmpTo) const
{
	if (count > cmpTo.count) return true;
	else if (count < cmpTo.count) return false;
	else return word.localeAwareCompare(cmpTo.word) < 0;
}


int TextAnalysisModel::rowCount(const QModelIndex &parent) const
{
	if (parent.isValid()) return 0;
	else return words.size();
}

bool TextAnalysisModel::hasChildren(const QModelIndex &parent) const
{
	return !parent.isValid();
}

QVariant TextAnalysisModel::data(const QModelIndex &index, int role) const
{
	if (!index.isValid() || (index.parent().isValid()))
		return QVariant();

	if (index.row() >= words.size())
		return QVariant();

	if (role == Qt::DisplayRole) {
		if (index.column() == 0) return words[index.row()].word;
		else if (index.column() == 1)  return words[index.row()].count;
		else if (index.column() == 2 && wordCount > 0)  return QString::number(words[index.row()].count * relativeProzentMultipler, 'g', 3) + " %";
	}
	return QVariant();
}

QVariant TextAnalysisModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (role != Qt::DisplayRole)
		return QVariant();

	if (orientation != Qt::Horizontal) return QString::number(section);
	else if (section == 0) return QString(TextAnalysisDialog::tr("Word/Phrase"));
	else if (section == 1) return QString(TextAnalysisDialog::tr("Count", "count as noun"));
	else if (section == 2) return QString(TextAnalysisDialog::tr("Count relative"));
	else return QVariant();
}

int TextAnalysisModel::columnCount(const QModelIndex &parent) const
{
	Q_UNUSED(parent);

	return 3;
}

void TextAnalysisModel::updateAll()
{
	wordCount = 0;
	characterInWords = 0;
	for (int i = 0; i < words.size(); i++) {
		wordCount += words[i].count;
		characterInWords += words[i].count * words[i].word.size();
	}
	qSort(words);
	if (words.size() > 0) relativeProzentMultipler = 100.0 / words[0].count;
	else relativeProzentMultipler = 0;
	//emit dataChanged?
}


TextAnalysisDialog::TextAnalysisDialog(QWidget *parent,  QString name)
	: QDialog(parent), document(0), editor(0), alreadyCount(false), lastSentenceLength(-1), lastMinSentenceLength(-1), lastParsedMinWordLength(-1)
{
	setWindowTitle(name);
	setAttribute(Qt::WA_DeleteOnClose);
	ui.setupUi(this);

	ui.resultView->setWordWrap(false);
//connect(ui.comboBox, SIGNAL(activated(int)),this,SLOT(change(int)));


	connect(ui.countButton, SIGNAL(clicked()), SLOT(slotCount()));
	connect(ui.closeButton, SIGNAL(clicked()), SLOT(slotClose()));
	connect(ui.searchSelectionButton, SIGNAL(clicked()), SLOT(slotSelectionButton()));
	connect(ui.exportButton, SIGNAL(clicked()), SLOT(slotExportButton()));
	connect(ui.resultView, SIGNAL(doubleClicked(const QModelIndex &)), SLOT(slotSelectionButton()));

}

TextAnalysisDialog::~TextAnalysisDialog()
{
}

void TextAnalysisDialog::setEditor(QEditor *aeditor)
{
	if (editor) disconnect(editor, 0, this, 0);
	if (aeditor) {
		editor = aeditor;
		document = aeditor->document();
		cursor = aeditor->cursor();
		connect(editor, SIGNAL(destroyed()), this, SLOT(editorDestroyed()));
	} else {
		document = 0;
		cursor = 0;
		editor = 0;
	}
}

int lowestStructureLevel(StructureEntry *entry)
{
	if (!entry) return 1000;
	if (entry->level >= 1) return entry->level; //0 is part, 1 chapter, 2 section, ... we skip part, and take the next that exists
	int r = 1000;
	for (int i = 0; i < entry->children.count(); i++)
		r = qMin(r, lowestStructureLevel(entry->children.at(i)));
	return r;
}

void TextAnalysisDialog::interpretStructureTree(StructureEntry *entry)
{
	interpretStructureTreeRec(entry, lowestStructureLevel(entry));
}

void TextAnalysisDialog::interpretStructureTreeRec(StructureEntry *entry, int targetLevel)
{
	if (!entry) return;
	if (entry->level == targetLevel) {
		chapters.append(QPair<QString, int> (entry->title, entry->getCachedLineNumber()));
		ui.comboBox->addItem(entry->title);
	} else for (int i = 0; i < entry->children.count(); i++)
			interpretStructureTree(entry->children.at(i));
}

void TextAnalysisDialog::init()
{
	alreadyCount = false;
	chapters.clear();
}

void TextAnalysisDialog::needCount()
{
	if (alreadyCount && lastSentenceLength == ui.sentenceLengthSpin->value() &&
	        lastParsedMinWordLength == (ui.minimumLengthMeaning->currentIndex() == 4 ? ui.minimumLengthSpin->value() : 0) &&
	        lastEndCharacters == (ui.respectEndCharsCheck->isChecked() ? ui.sentenceEndChars->text() : "") &&
	        lastMinSentenceLength == (ui.wordsPerPhraseMeaning->currentIndex() == 1 ? ui.sentenceLengthSpin->value() : 0)) return;
	lastSentenceLength = ui.sentenceLengthSpin->value();
	lastMinSentenceLength = (ui.wordsPerPhraseMeaning->currentIndex() == 1 ? ui.sentenceLengthSpin->value() : 0);
	int minimumWordLength = 0;
	if (ui.minimumLengthMeaning->currentIndex() == 4) minimumWordLength = ui.minimumLengthSpin->value();
	lastParsedMinWordLength = minimumWordLength;
	bool respectSentenceEnd = ui.respectEndCharsCheck->isChecked();
	lastEndCharacters = respectSentenceEnd ? ui.sentenceEndChars->text() : "";
	for (int i = 0; i < 3; i++) {
		maps[i].resize(2 + chapters.size());
		for (int j = 0; j < maps[i].size(); j++)
			maps[i][j].clear();
		lineCount[i].resize(2 + chapters.size());
		for (int j = 0; j < lineCount[i].size(); j++)
			lineCount[i][j] = 0;
	}
	lineCount[0][0] = document->lines();
	int selectionStartLine = -1;
	int selectionEndLine = -1;
	int selectionStartIndex = -1;
	int selectionEndIndex = -1;
	if (cursor.hasSelection()) {
		QDocumentSelection sel = cursor.selection();
		selectionStartLine = sel.startLine;
		selectionEndLine = sel.endLine;
		selectionStartIndex = sel.start;
		selectionEndIndex = sel.end;
		if (selectionStartLine > selectionEndLine) {
			int temp = selectionStartLine;
			selectionStartLine = selectionEndLine;
			selectionEndLine = temp;
			temp = selectionStartIndex;
			selectionStartIndex = selectionEndIndex;
			selectionEndIndex = temp;
		} else if (selectionStartLine == selectionEndLine && selectionStartIndex > selectionEndIndex) {
			int temp = selectionStartIndex;
			selectionStartIndex = selectionEndIndex;
			selectionEndIndex = temp;
		}
		lineCount[0][1] = selectionEndLine - selectionStartLine + 1;
	}

	int nextChapter = 0;
	int extraMap = 0;
	QList<QString> lastWords[3];
	int sentenceLengths[3] = {0, 0, 0};
	for (int l = 0; l < document->lines(); l++) {
		if (nextChapter < chapters.size() && l + 1 >= chapters[nextChapter].second) {
			if (nextChapter == 0) extraMap = 2;
			else extraMap++;
			nextChapter++;
			if (extraMap >= maps[0].size()) extraMap = 0;
		}
		if (extraMap != 0) lineCount[0][extraMap]++;
		QString line = document->line(l).text();
		bool commentReached = false;
		bool lineCountedAsText = false;
		int state;
		int lastIndex = 0;
		LatexReader lr(line);
		while ((state = lr.nextWord(true)) != LatexReader::NW_NOTHING) {
			if (lr.word.endsWith('.')) lr.word.chop(1);
			bool inSelection;
			if (selectionStartLine != selectionEndLine)
				inSelection = ((l < selectionEndLine) && (l > selectionStartLine)) ||
				              ((l == selectionStartLine) && (lr.index > selectionStartIndex)) ||
				              ((l == selectionEndLine) && (lr.wordStartIndex <= selectionEndIndex));
			else
				inSelection = (l == selectionStartLine) && (lr.index > selectionStartIndex) && (lr.wordStartIndex <= selectionEndIndex);
			QString curWord = lr.word.toLower();
			int curType = -1;
			if (commentReached) {
				if (respectSentenceEnd) for (int i = lastIndex; i < lr.wordStartIndex; i++)
						if (lastEndCharacters.contains(line.at(i))) {
							sentenceLengths[2] = 0;
							lastWords[2].clear();
							break;
						}
				curType = 2;
			} else if (state == LatexReader::NW_COMMENT) {
				//comment is only % character
				curType = 2;
				if (!commentReached) {
					commentReached = true;
					lineCount[2][0]++;
					if (inSelection) lineCount[2][1]++;
					if (extraMap != 0) lineCount[2][extraMap]++;
					//find sentence end characters which belong to the words before the comment start
					if (respectSentenceEnd)
						for (int i = lastIndex; i < lr.wordStartIndex; i++) {
							if (line.at(i) == QChar('%') && (i == 0 || line.at(i - 1) != QChar('\\'))) {
								lastIndex = i;
								break;
							} else {
								if (lastEndCharacters.contains(line.at(i))) {
									sentenceLengths[0] = 0;
									lastWords[0].clear();
									break;
								}
							}
						}
				}
			} else if (state == LatexReader::NW_COMMAND) {
				curType = 1;
			} else if (state == LatexReader::NW_TEXT) {
				curType = 0;
				if (!lineCountedAsText) {
					lineCountedAsText = true;
					lineCount[1][0]++;
					if (inSelection) lineCount[1][1]++;
					if (extraMap != 0) lineCount[1][extraMap]++;
				}
			}
			if (respectSentenceEnd && !commentReached)
				for (int i = lastIndex; i < lr.wordStartIndex; i++)
					if (lastEndCharacters.contains(line.at(i))) {
						sentenceLengths[0] = 0;
						lastWords[0].clear();
						break;
					}
			lastIndex = lr.index;
			if (curType != -1 && curWord.size() >= minimumWordLength) {
				//if (lastSentenceLength>1) {
				lastWords[curType].append(curWord);
				sentenceLengths[curType] += curWord.size() + 1;
				if (lastWords[curType].size() > lastSentenceLength) {
					sentenceLengths[curType] -= lastWords[curType].first().size() + 1;
					lastWords[curType].removeFirst();
				}
				if (lastWords[curType].size() >= lastMinSentenceLength) {
					curWord = "";
					curWord.reserve(sentenceLengths[curType]);
					foreach (const QString &s, lastWords[curType])
						curWord += s + " ";
					curWord.truncate(curWord.size() - 1);
					maps[curType][0][curWord] = maps[curType][0][curWord] + 1;
					if (inSelection) maps[curType][1][curWord] = maps[curType][1][curWord] + 1;
					if (extraMap != 0) maps[curType][extraMap][curWord] = maps[curType][extraMap][curWord] + 1;
				}
			}
		};
		if (respectSentenceEnd) //it makes sense to ignore in something like .%. the sentence end after the % (and is less work)
			for (int i = lastIndex; i < line.size(); i++)
				if (lastEndCharacters.contains(line.at(i))) {
					sentenceLengths[commentReached ? 2 : 0] = 0;
					lastWords[commentReached ? 2 : 0].clear();
					break;
				}

	}

	alreadyCount = true;
}

void TextAnalysisDialog::insertDisplayData(const QMap<QString, int> &map)
{
	int minLen = 0;
	int minCount = ui.minimumCountSpin->value();
	int phraseLength = ui.sentenceLengthSpin->value();
	QRegExp wordFilter;
	bool filtered = ui.filter->currentIndex() != 0;
	QString curFilter = ui.filter->currentText();
	if (ui.filter->currentIndex() == -1 ||
	        curFilter != ui.filter->itemText(ui.filter->currentIndex())) wordFilter = QRegExp(curFilter);
	else wordFilter = QRegExp(curFilter.mid(curFilter.indexOf("(")));

	switch (ui.minimumLengthMeaning->currentIndex()) {
	case 2: //at least one word must have min length, all shorter with space: (min-1 +1)*phraseLength-1
		minLen = ui.minimumLengthSpin->value(); //no break!
		for (QMap<QString, int>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it)
			if (it.value() >= minCount) {
				if (it.key().size() >= minLen * phraseLength) {
					if (filtered || wordFilter.exactMatch(it.key()))
						displayed.words.append(ClsWord(it.key(), it.value()));
				} else {
					if (filtered && !wordFilter.exactMatch(it.key())) continue;
					QString t = it.key();
					int last = 0;
					int i = 0;
					for (; i < t.size(); i++)
						if (t.at(i) == ' ') {
							if (i - last >= minLen) {
								displayed.words.append(ClsWord(it.key(), it.value()));
								break;
							} else last = i + 1;
						}
					if (i == t.size() && i - last >= minLen) displayed.words.append(ClsWord(it.key(), it.value()));
				}
			}
		break;
	case 3: //all words must have min len, (min +1)*phraseLength-1
		minLen = ui.minimumLengthSpin->value();
		for (QMap<QString, int>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it)
			if (it.value() >= minCount && it.key().size() >= minLen) { //not minLen*phraseCount because there can be less words in a phrase
				if (filtered && !wordFilter.exactMatch(it.key())) continue;
				QString t = it.key();
				int last = 0;
				bool ok = true;
				for (int i = 0; i < t.size(); i++)
					if (t.at(i) == ' ') {
						if (i - last < minLen) {
							ok = false;
							break;
						} else last = i + 1;
					}
				if (ok && t.size() - last >= minLen)
					displayed.words.append(ClsWord(it.key(), it.value()));
			}
		break;
	case 1:
		minLen = ui.minimumLengthSpin->value(); //no break!
	default:
		if (filtered) {
			for (QMap<QString, int>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it)
				if (it.value() >= minCount && it.key().size() >= minLen && wordFilter.exactMatch(it.key()))
					displayed.words.append(ClsWord(it.key(), it.value()));
		} else {
			for (QMap<QString, int>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it)
				if (it.value() >= minCount && it.key().size() >= minLen)
					displayed.words.append(ClsWord(it.key(), it.value()));
		}
	}
}

void TextAnalysisDialog::slotCount()
{
	needCount();
	displayed.words.clear(); //insert into map to sort
	int currentMap = ui.comboBox->currentIndex();
	if (ui.normalTextCheck->isChecked()) insertDisplayData(maps[0][currentMap]);
	if (ui.commandsCheck->isChecked()) insertDisplayData(maps[1][currentMap]);
	if (ui.commentsCheck->isChecked()) insertDisplayData(maps[2][currentMap]);

	displayed.updateAll();

	ui.resultView->setModel(NULL);
	ui.resultView->setModel(&displayed);

	ui.resultView->setShowGrid(false);
	ui.resultView->resizeRowsToContents();

	ui.totalLinesLabel->setText(QString::number(lineCount[0][currentMap]));
	ui.textLinesLabel->setText(QString::number(lineCount[1][currentMap]));
	ui.commentLinesLabel->setText(QString::number(lineCount[2][currentMap]));

	ui.displayedWordLabel->setText(QString::number(displayed.wordCount));
	ui.differentWordLabel->setText(QString::number(displayed.words.size()));
	ui.characterInWordsLabel->setText(QString::number(displayed.characterInWords));
}

void TextAnalysisDialog::editorDestroyed()
{
	setEditor(0);
}

void TextAnalysisDialog::slotSelectionButton()
{
	if (!editor) return;
	int s = ui.resultView->currentIndex().row();
	if (s < 0 || s >= displayed.words.size()) return;
	QString selected = displayed.words[s].word;
	if (selected == "") return;
	if (lastSentenceLength > 1 && selected.contains(' ')) {
		selected.replace(" ", "\\W+");
		editor->find(selected, true, true);
	} else editor->find(selected, true, false);

}

QByteArray escapeAsCSV(const QString &s)
{
	QByteArray ba = s.toUtf8();
	if (ba.contains(',')) {
		ba.replace('"', "\"\"");
		ba.prepend('"');
		ba.append('"');
	}
	return ba;
}

void TextAnalysisDialog::slotExportButton()
{
	QString fn = QFileDialog::getSaveFileName(this, tr("CSV Export"), editor ? editor->document()->getFileName().replace(".tex", ".csv") : QString(), tr("CSV file") + " (*.csv)" ";;" + tr("All files") + " (*)");
	if (fn.isEmpty()) return;
	QFile f(fn);
	if (!f.open(QFile::WriteOnly))
		return;
#ifdef Q_OS_WIN
	QByteArray les = "\r\n";
#else
	QByteArray les = "\n";
#endif
	foreach (const ClsWord &w, displayed.words)
		f.write(escapeAsCSV(w.word) + ',' + QByteArray::number(w.count) + les);
}

void TextAnalysisDialog::slotClose()
{
	close();
}