Upload statistic.py
Browse files- v-corpus-zh/statistic.py +56 -0
v-corpus-zh/statistic.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def get_all_files_in_directory(directory, ext=''):
|
2 |
+
import os
|
3 |
+
import re
|
4 |
+
custom_sort_key_re = re.compile('([0-9]+)')
|
5 |
+
|
6 |
+
def custom_sort_key(s):
|
7 |
+
# 将字符串中的数字部分转换为整数,然后进行排序
|
8 |
+
return [int(x) if x.isdigit() else x for x in custom_sort_key_re.split(s)]
|
9 |
+
|
10 |
+
all_files = []
|
11 |
+
for root, dirs, files in os.walk(directory):
|
12 |
+
for file in files:
|
13 |
+
if file.endswith(ext):
|
14 |
+
file_path = os.path.join(root, file)
|
15 |
+
all_files.append(file_path)
|
16 |
+
return sorted(all_files, key=custom_sort_key)
|
17 |
+
|
18 |
+
|
19 |
+
def simpleSplit(_s: str, _sp, _st=0, _shift=True):
|
20 |
+
_idx = _s.index(_sp, _st)
|
21 |
+
if _shift:
|
22 |
+
return _s[:_idx], _s[_idx + len(_sp):]
|
23 |
+
else:
|
24 |
+
return _s[:_idx], _s[_idx:]
|
25 |
+
|
26 |
+
|
27 |
+
# =================
|
28 |
+
|
29 |
+
a = get_all_files_in_directory(r'D:\datasets\v-corpus-zh', ext='.txt.gz')
|
30 |
+
|
31 |
+
# =================
|
32 |
+
import gzip
|
33 |
+
|
34 |
+
results = {}
|
35 |
+
|
36 |
+
for filePath in a:
|
37 |
+
name = simpleSplit(filePath, 'v-corpus-zh')[1]
|
38 |
+
name = name.split('\\')[1:-1]
|
39 |
+
if len(name) < 3:
|
40 |
+
name.append('game')
|
41 |
+
name = '"' + '","'.join(name) + '"'
|
42 |
+
if name not in results:
|
43 |
+
results[name] = (0, 0)
|
44 |
+
print(name)
|
45 |
+
n_l, n_c = results[name]
|
46 |
+
with gzip.open(filePath, 'rt', encoding='utf-8') if filePath.endswith('.gz') else open(filePath,
|
47 |
+
encoding='utf-8') as f:
|
48 |
+
for line in f:
|
49 |
+
n_l += 1
|
50 |
+
n_c += len(line)
|
51 |
+
results[name] = (n_l, n_c)
|
52 |
+
|
53 |
+
with open('statistic.csv', 'w', encoding='utf-8') as f:
|
54 |
+
f.write('\uFEFF"Company","Title","SubTitle","Lines","Words"\n')
|
55 |
+
for k,v in results.items():
|
56 |
+
f.write(k + f', {v[0]}, {v[1]}\n')
|