back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 1dea4eabe5e31039abb77189381b06e3f19dfe7d (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
# Artificial Text Detection

This project is based on my Master's thesis:  
> **"Алгоритм выявления искусственно созданных текстов"**  
> Nizhny Novgorod State Technical University, 2021  
> Author: Andrey Kuznetsov

It is a C++/Qt-based application designed to detect artificially generated
scientific texts (e.g., SCIgen outputs). The detection method is based on
analyzing the internal stylistic consistency of the document using unsupervised
clustering and rank correlation metrics.

<img src="screen_1.png" />

<img src="screen_2.png" />

<img src="screen_3.png" />


## Project Overview

The program processes input text, splits it into fragments, builds a vector
space using N-gram features, computes a pairwise distance matrix using
Spearman rank correlation, and applies clustering to detect stylistic
discontinuities. Such discontinuities are often present in machine-generated
texts.

## Technologies Used

- **C++17 (STL)**
- **Qt 5 Widgets & Charts**
- **Boost**:
    - `boost::python` and `boost::python::numpy` — Used to exchange Numpy arrays
    between C++ and Python during clustering.
- **Python 3** (invoked from C++):
    - `numpy`
    - `scikit-learn`
    - `scikit-learn-extra`

## Features

### Flexible Text Input
- Manual text input via the built-in editor
- File selection via file dialog
- **Drag and drop** support for `.txt` files

### Text Preprocessing
- Removes stop words, non-letter symbols, and repeated spaces
- Converts text to lowercase
- Implemented in a dedicated `prepare()` function for consistent cleaning

### N-Gram Extraction and Dictionary Building
- Extracts N-grams with:
  - Minimum N = 2
  - Maximum N set via UI parameters
- Builds a global dictionary from all fragments
- Performs feature selection by filtering top N-grams based on 90% frequency threshold

### Vector Space Model Construction
- Uses the selected dictionary to vectorize document fragments
- Produces a nested structure: `vector<vector<vector<int>>>`
    where:
    - Outer vector = documents
    - Middle vector = fragments
    - Inner vector = N-gram frequencies

### Distance Calculation Using Rank Correlation
- Calculates pairwise distances between fragments using **Spearman’s rank correlation coefficient**
- Computes **average rank dependence (ZVT)** with a sliding window approach
- Handles inter-document and intra-document fragment comparisons

### Matrix Normalization
- Normalizes the pairwise distance matrix shape by padding shorter rows with zeros
- Ensures consistent dimensions for clustering algorithms

### Unsupervised Clustering with Python
- Clustering algorithms supported:
    - **K-Medoids**
    - **K-Means**
    - **Agglomerative Clustering**
- Implemented in Python using:
    - `scikit-learn`
    - `scikit-learn-extra`
- Distance matrix passed from C++ to Python via:
    - `Boost.Python`
    - `Boost.NumPy`

### C++ Python Interoperability
To perform clustering with Python libraries, the project uses `Boost.Python` and `Boost.NumPy`:

- Converts a `std::vector<std::vector<double>>` (distance matrix) to a NumPy array.
- Initializes the embedded Python interpreter.
- Imports the custom Python module `mymodule.py`.
- Calls one of the clustering functions: `kmedoids`, `kmeans`, or `agglClus`.
- Extracts prediction results and returns them back to C++.

This approach allows combining the performance and UI capabilities of C++/Qt with the ML power of Python.

### Visualization
- Displays predicted vs. real document labels in two separate charts.
- Cluster assignments are color-coded.
- Charts are rendered interactively via Qt.

### Multithreaded Execution with Progress Bar
- Runs the detection algorithm in a separate thread using `QThread`
- Keeps the GUI responsive during processing
- Displays progress via `QProgressDialog`


##  Algorithm Pipeline (Detailed)

1. **Text Input**
    - User provides text via input box, file dialog, or drag-and-drop.
    - Text is stored in `target_doc`.

2. **Preprocessing** (`prepare()`)
    - Removes stop words (prepositions, conjunctions, etc.)
    - Removes all characters except letters and spaces
    - Removes repeated characters (e.g., spaces)
    - Converts text to lowercase

3. **Fragmentation**
    - Text is split into equal-length chunks based on UI parameters

4. **N-Gram Extraction**
    - Combines all documents into a single corpus
    - Calculates N-grams for N from 2 up to max N (from UI)
    - Uses a sliding window algorithm
    - Aggregates results into a dictionary

5. **N-Gram Filtering**
    - Selects N-grams above 90th percentile of frequency
    - Saves filtered list to a text file

6. **Vector Space Modeling**
    - For each document, for each fragment:
    - Computes vector of N-gram frequencies via `freq_in_chunk()`
    - Produces nested structure of frequency vectors

7. **Rank Correlation (Spearman's rho)**
    - Calculates rank correlation distance between fragment vectors:  
        ρ = 1 - (6 * ∑ d_i^2) / (n(n^2 - 1))

    - Implemented via `zv_calc()` and `correlation()`

8. **Average Rank Dependence**
    - Computes `ZVT` values for each fragment (based on 10 previous)
    - Combines `ZVT` into full pairwise distance matrix

9. **Matrix Padding**
    - Matrix may have uneven rows; padded with zeros to square shape

10. **C++ to Python Transfer**
    - Converts distance matrix to NumPy arrays using `Boost.NumPy`
    - Initializes embedded Python interpreter
    - Calls `mymodule.py::{kmedoids, kmeans, agglClus}`
    - Extracts prediction results

11. **Post-Processing Results**
    - Splits results into clusters for artificial vs. input document
    - Compares distributions:
        - If both match → **Artificial text**
        - If different → **Human-written text**

12. **Visualization & UI**
    - Result shown in message box
    - Prediction charts drawn with `QChartView`

## Build Instructions

### Prerequisites
Before building, make sure the following are installed:

- **Qt 5.x** (QtWidgets, QtCharts, QtCore, QtGui)
- **Boost** (with `Boost.Python` and `Boost.NumPy` modules built)
- **Python 3.x** (tested with Python 3.8)
- Python packages:
```bash
pip install numpy scikit-learn scikit-learn-extra
```
### Boost Notes

- You must build Boost with Python support (`b2 --with-python`).
- Ensure that Boost is compiled with the same Python version you plan to use.
- On Windows, you may need to specify Boost and Python library paths in the `.pro` file, for example:

```qmake
LIBS += \
    -L"C:\Program Files\boost\boost_1_76_0\stage\x86\lib" \
    -LC:/Users/<USERNAME>/AppData/Local/Programs/Python/Python38-32/libs
```

### Building

- Clone the repository:
    
```
git clone https://git.scratko.xyz/artifical-text-detection

cd artifical-text-detection
```
- Open the `.pro` file in Qt Creator.
- Adjust library paths for Boost and Python if necessary.
- Build and run the project.

### Running

- Ensure that the Python interpreter can import mymodule.py.
- Make sure required Python packages are installed in the environment used by the application.

##  Notes

- Designed to detect machine-generated documents, especially SCIgen-based fakes.
- Easily extendable to detect LLM-generated texts with modified features.
- No pretrained models required — fully unsupervised method.