-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_words.py
108 lines (81 loc) · 3.03 KB
/
extract_words.py
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
#%%
## One word
words_data = []
with open(file="data.txt", mode='r') as file:
for line in file:
print(line.split( )[0])
words_data.append((line.split( )[0]))
unique_words_data = list(set(words_data))
#%%
from maha.cleaners.functions import keep
import os
import json
# from collections import defaultdict
words = {}
for category in os.listdir('Sanad'):
for text_file in os.listdir(os.path.join('Sanad', category)):
with open(os.path.join('Sanad', category, text_file)) as file:
text = file.read()
cleaned_text = keep(text,arabic_letters=True)
cleaned_text_list = cleaned_text.split( )
sanad_words_data = list(set(cleaned_text_list))
# print(sanad_words_data)
for word in sanad_words_data:
if word in words.keys():
words[word] += 1
else:
words[word] = 1
#print(words)
#break
output = dict(sorted(words.items(), key=lambda x: x[1],reverse=True))
# json.dumps(words, ensure_ascii=False).encode('utf-8')
with open('output_text.txt', 'w', encoding='utf8') as json_file:
json.dump(output, json_file, ensure_ascii=False, indent=2)
# for word in cleaned_text_list:
# with open('result.txt', 'a') as file:
# file.write(word+"\n")
#print(word+"\n")
# %%
!pip install ipywidgets
#%%
import os
import json
# from collections import defaultdict
from maha.cleaners.functions import keep
import os
import json
from maha.parsers.functions import parse
import re
from tqdm.notebook import trange, tqdm
#%%
words = {}
for category in os.listdir('Sanad'):
for text_file in tqdm(os.listdir(os.path.join('Sanad', category))):
with open(os.path.join('Sanad', category, text_file)) as file:
text = file.read()
cleaned_text = keep(text,arabic_letters=True)
# cleaned_text_list = cleaned_text.split( )
cleaned_text_list = re.findall(r'\b\w+\b \b\w+\b', cleaned_text)
# from maha.parsers.functions import parse
# cleaned_text_list = parse(cleaned_text, custom_expressions=r'\b\w+\b \b\w+\b', include_space=True)
sanad_words_data = list(set(cleaned_text_list))
# print(sanad_words_data)
for word in sanad_words_data:
if word in words.keys():
words[word] += 1
else:
words[word] = 1
#print(words)
#break
#%%
output = dict(sorted(words.items(), key=lambda x: x[1],reverse=True))
# json.dumps(words, ensure_ascii=False).encode('utf-8')
with open('output_text_2words.txt', 'w', encoding='utf8') as json_file:
json.dump(output, json_file, ensure_ascii=False, indent=2)
# for word in cleaned_text_list:
# with open('result.txt', 'a') as file:
# file.write(word+"\n")
#print(word+"\n")
# %%
len(output)
# %%