-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
285 lines (251 loc) · 7.21 KB
/
main.cpp
File metadata and controls
285 lines (251 loc) · 7.21 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
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
/*
平台: Windows 11 (25H2)
编译器: g++ 15.2.0 (MinGW-w64, 64-bit)
C++标准: C++17 (ISO/IEC 14882:2017)
编译: g++ -O2 -std=c++17 -static main.cpp -o main.exe
要求: 为保证兼容性, input文件夹下文件为UTF-8编码, 文件名为英文
使用方法: 在input文件夹下导入文本(.txt文件, UTF-8编码), 运行main.exe, 在output文件夹下查看处理结果
*/
#include <algorithm>
#include <cmath>
#include <filesystem> // C++17标准引入
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <Windows.h> // Windows环境
using namespace std;
namespace fs = std::filesystem;
// 收集字频数据
struct info
{
string character;
int frequency;
int level;
};
// 加载常用字表
void load_common(const string &filename, int level, map<string, int> &char_level)
{
ifstream file;
file.open(filename);
if (!file.is_open())
{
return;
}
string line;
while (getline(file, line))
{
size_t i = 0;
// 遍历每一个中文字符
while (i < line.size())
{
unsigned char c = (unsigned char)line[i];
if (c < 0x80) // 跳过ASCII字符
{
i++;
continue;
}
if (i + 2 < line.size() && (c & 0xE0) == 0xE0) // 遍历
{
string ch = line.substr(i, 3);
if (char_level.find(ch) == char_level.end())
{
char_level[ch] = level;
}
i += 3;
}
else
{
i++;
}
}
}
file.close();
}
// 判断是否为全角句子结束标点(用于分割句子)
bool is_sentence_end(const string &ch)
{
// 全角常见句子结束/分隔标点
const set<string> ends = {",", "。", "!", "?", ";", ":", "…", "……", "﹔", "﹕"};
return ends.count(ch) > 0;
}
// 字频排序比较函数
bool sort_compare(const info &a, const info &b)
{
if (a.frequency != b.frequency)
return a.frequency > b.frequency;
return a.character < b.character;
}
// 处理单个文件
void process_file(const string &input_path, const map<string, int> &char_level)
{
fs::path p = input_path;
string filename = p.stem().string();
string output_path = "output\\" + filename + ".csv";
map<string, int> freq; // 字频
vector<int> sentence_lengths; // 每句的长度
ifstream in_file;
in_file.open(input_path);
if (!in_file.is_open())
{
cout << "Unable to open input file: " << input_path << endl;
return;
}
string line;
while (getline(in_file, line))
{
size_t i = 0;
int current_sentence_len = 0;
// 遍历每一个中文字符
while (i < line.size())
{
unsigned char c = (unsigned char)line[i];
if (c < 0x80) // 跳过ASCII字符
{
i++;
continue;
}
if (i + 2 < line.size() && (c & 0xE0) == 0xE0) // 遍历
{
string ch = line.substr(i, 3);
// 如果是句子结束标点
if (is_sentence_end(ch))
{
if (current_sentence_len > 0)
{
sentence_lengths.push_back(current_sentence_len);
}
current_sentence_len = 0;
}
else
{
// 不是结束标点,且是常用字 => 计入当前句子长度
if (char_level.count(ch))
{
freq[ch]++;
current_sentence_len++;
}
}
i += 3;
}
else
{
++i;
}
}
// 文件每行结束时,如果当前句子有长度,也要记录
if (current_sentence_len > 0)
{
sentence_lengths.push_back(current_sentence_len);
}
}
in_file.close();
// 计算平均值和方差
double avg_len = 0.0;
double variance = 0.0;
size_t n = sentence_lengths.size();
if (n > 0)
{
long long sum = 0;
double sum_sq_diff = 0.0;
for (int len : sentence_lengths)
{
sum += len;
}
avg_len = (double)sum / n; // 平均数
for (int len : sentence_lengths)
{
double diff = len - avg_len;
sum_sq_diff += diff * diff;
}
variance = sum_sq_diff / n; // 方差
}
vector<info> data;
for (auto p : freq)
{
data.push_back({p.first, p.second, char_level.at(p.first)});
}
sort(data.begin(), data.end(), sort_compare);
// 输出 CSV
ofstream out_file;
out_file.open(output_path, ios::binary);
if (!out_file.is_open())
{
cout << "Unable to create output file: " << output_path << endl;
return;
}
// 输出BOM开头前三个字节,防止在中文Windows环境下乱码
unsigned char bom[] = {0xEF, 0xBB, 0xBF};
out_file.write((char *)bom, sizeof(bom));
// 标题行
out_file << "字符,频数,级别\n";
// 字频部分
for (info item : data)
{
out_file << item.character << "," << item.frequency << "," << item.level << "\n";
}
// 空行分隔
out_file << "\n";
// 句子统计(放在文件末尾)
out_file << "统计项,值\n";
out_file << "句长平均值," << avg_len << "\n";
out_file << "句长方差," << variance << "\n";
out_file << "句长标准差," << sqrt(variance) << "\n";
out_file.close();
cout << "Processed: " << input_path << " => " << output_path << endl;
}
int main()
{
// 创建output文件夹
if (!fs::exists("output"))
{
if (!fs::create_directory("output"))
{
cout << "Unable to create output folder." << endl;
return 1;
}
}
// 导入常用字表
map<string, int> char_level;
load_common("CommonChar1.txt", 1, char_level);
load_common("CommonChar2.txt", 2, char_level);
load_common("CommonChar3.txt", 3, char_level);
if (char_level.empty())
{
cout << "Warning: No common words loaded. Check common word list files." << endl;
}
string input_dir = "input";
// 检查input文件夹
if (!fs::exists(input_dir) || !fs::is_directory(input_dir))
{
cout << "Input folder does not exist." << endl;
return 1;
}
// 处理文件
int processed_count = 0;
for (const auto &entry : fs::directory_iterator(input_dir))
{
if (entry.is_regular_file())
{
fs::path path = entry.path();
if (path.extension() == ".txt")
{
process_file(path.string(), char_level);
processed_count++;
}
}
}
// 输出处理结果
if (processed_count == 0)
{
cout << "No .txt files found in input folder." << endl;
}
else
{
cout << "\nAll processing completed. Total files processed: " << processed_count << endl;
}
system("pause");
return 0;
}