-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHJBRL.py
282 lines (211 loc) · 9.02 KB
/
HJBRL.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
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
# -*- coding: utf-8 -*-
# Imports #######################################################################
import nltk
import re
import codecs
import itertools as itx
from nltk.tokenize.api import *
from nltk.stem import RegexpStemmer
#################################################################################
# Variables #####################################################################
bangla_alphabet = dict(
consonant = u'[\u0995-\u09b9\u09ce\u09dc-\u09df]',
independent_vowel = u'[\u0985-\u0994]',
dependent_vowel = u'[\u09be-\u09cc\u09d7]',
dependent_sign = u'[\u0981-\u0983\u09cd]',
virama = u'[\u09cd]'
)
bangla_word_pattern = re.compile(ur'''(?:
{consonant}
(?:{virama}{consonant})?
(?:{virama}{consonant})?
{dependent_vowel}?
{dependent_sign}?
|
{independent_vowel}
{dependent_sign}?
)+$'''.format(**bangla_alphabet), re.VERBOSE)
dictionary_path = '/home/AtriyaSen/nltk_data/Dictionaries'
bdictionary_path = '/home/AtriyaSen/nltk_data/EnglishDictionaries'
cleaned_corpus_path = '/home/AtriyaSen/nltk_data/Corpuses'
#dirty_corpus_path = '/home/AtriyaSen/nltk_data/FIRE/bn_ABP'
dirty_corpus_path = '/home/AtriyaSen/nltk_data/ABPBhojonAll'
result_stage1_path = '/home/AtriyaSen/stage1.utf8'
result_stage2_path = '/home/AtriyaSen/stage2.utf8'
result_stage3_path = '/home/AtriyaSen/stage3.utf8'
result_stage4_path = '/home/AtriyaSen/stage4.utf8'
result_stage5_path = '/home/AtriyaSen/stage5.utf8'
result_residue_path = '/home/AtriyaSen/residue.utf8'
frequency_cutoff = 5
dirty_words_distribution = None
dictionary = nltk.corpus.reader.WordListCorpusReader(
dictionary_path, '.*', encoding='utf-8'
)
dictionary_words = set(dictionary.words())
noun_classifier_suffixes = [ur"টা$", ur"টি$", ur"থানা$", ur"থানি$", ur"জন$", ur"টুকু$", ur"গুলো$", ur"গুলি$", ur"রা$"]
noun_case_marker_suffixes = [ur"রা$", ur"দের$", ur"কে$", ur"তে$", ur"ে$"]
noun_emphasizing_suffixes = [ur"ই$", ur"ও$"]
verb_first_suffixes = [ur"ব$", ur"তাম$", ur"িনি$", ur"েছিলাম$", ur"ছিলাম$", ur"লাম$", ur"েছি$", ur"ছি$", ur"ি$"]
verb_second_suffixes = [ur"বে$", ur"তে$", ur"নি$", ur"েছিলে$", ur"ছিলে$", ur"লে$", ur"েছ$", ur"ছ$"]
verb_third_suffixes = [ur"বে$", ur"ত$", ur"েনি$", ur"েছিল$", ur"ছিল$", ur"ল$", ur"েছে$", ur"ছে$", ur"ে$"]
test_corpus_words = [u"তরেছিলাম"]
#################################################################################
# Classes #######################################################################
class BanglaWordTokenizer(StringTokenizer):
def tokenize(self, s):
return BanglaWordTokenizerFunction.tokenize(s)
#################################################################################
# Functions #####################################################################
BanglaWordTokenizerFunction = nltk.tokenize.RegexpTokenizer(ur'[\u0980-\u09DF]+')
def BanglaCorpusWordTokenize(corpus):
return [w for filename in corpus.fileids() for w in corpus.words(filename)]
def WriteToFile (raw_words, filename):
f = codecs.open(filename, 'w', 'utf-8')
for w in dirty_words_distribution.keys():
if w not in raw_words:
continue
w_count = dirty_words_distribution[w]
f.write(w + ' (' + str(w_count) + ')\n')
f.close()
def FilterByDictionary(raw_words):
filtered = raw_words - dictionary_words
print "Dictionary Filter (", len(raw_words), "->", len(filtered), ")"
#WriteToFile(raw_words & dictionary_words, result_stage1_path)
return filtered
def FilterByBorrowedDictionary(raw_words):
bdictionary = nltk.corpus.reader.WordListCorpusReader(
bdictionary_path, '.*', encoding='utf-8'
)
bdictionary_words = set(bdictionary.words())
filtered = raw_words - bdictionary_words
print "Borrowed Dictionary Filter (", len(raw_words), "->", len(filtered), ")"
#WriteToFile(raw_words & bdictionary_words, result_stage2_path)
return filtered
def FilterByCleanedCorpus(raw_words):
cleaned_corpus = nltk.corpus.reader.plaintext.PlaintextCorpusReader(
cleaned_corpus_path, '.*', word_tokenizer = BanglaWordTokenizer(), encoding='utf-8'
)
cleaned_corpus_words = set(BanglaCorpusWordTokenize(cleaned_corpus))
filtered = raw_words - cleaned_corpus_words
print "Cleaned Corpus Filter (", len(raw_words), "->", len(filtered), ")"
#WriteToFile(raw_words & cleaned_corpus_words, result_stage3_path)
return filtered
def FilterByBanglaWordPattern(raw_words):
filtered = set()
invalid = set()
for w in raw_words:
if bangla_word_pattern.match(w):
filtered.add(w)
else:
invalid.add(w)
print "Bangla Word Pattern Filter (", len(raw_words), "->", len(filtered), ")"
#WriteToFile(invalid, result_stage4_path)
return filtered
def FilterByFrequency(raw_words):
filtered = set()
invalid = set()
for w in raw_words:
w_count = dirty_words_distribution[w]
if w_count < frequency_cutoff:
filtered.add(w)
else:
invalid.add(w)
print "Word Frequency Filter (", len(raw_words), "->", len(filtered), ")"
#WriteToFile(invalid, result_stage5_path)
return filtered
#########################################
def QuickStemNounByDictionary(word):
# Code is repetitive by design - a myriad of special cases may need to be incorporated later.
# This might be difficult with more compact code.
ra_blocker_seen = 0
for s in noun_emphasizing_suffixes:
candidate = RegexpStemmer(re.compile(s)).stem(word)
if candidate != word:
word = candidate
break
if word in dictionary_words:
return word
for s in noun_case_marker_suffixes:
candidate = RegexpStemmer(re.compile(s)).stem(word)
if candidate != word:
if s is "রা" or s is "কে" or s is "ে" or s is "তে":
ra_blocker_seen = 1
word = candidate
break
if word in dictionary_words:
return word
for s in noun_classifier_suffixes:
candidate = RegexpStemmer(re.compile(s)).stem(word)
if candidate != word:
if not(s is "রা" and ra_blocker_seen is 1):
word = candidate
break
if word in dictionary_words:
return word
else:
return None
# return word
def QuickStemVerbByDictionary(word):
# Code is repetitive by design - a myriad of special cases may need to be incorporated later.
# This might be difficult with more compact code.
for s in verb_first_suffixes:
candidate = RegexpStemmer(re.compile(s)).stem(word)
if candidate != word:
return candidate
for s in verb_second_suffixes:
candidate = RegexpStemmer(re.compile(s)).stem(word)
if candidate != word:
return candidate
for s in verb_third_suffixes:
candidate = RegexpStemmer(re.compile(s)).stem(word)
if candidate != word:
return candidate
if word in dictionary_words:
return word
else:
return None
# return word
#################################################################################
# Main ##########################################################################
dirty_corpus = nltk.corpus.reader.plaintext.PlaintextCorpusReader(
dirty_corpus_path, '.*', word_tokenizer = BanglaWordTokenizer(), encoding='utf-8'
)
dirty_corpus_words_list = BanglaCorpusWordTokenize(dirty_corpus)
#dirty_corpus_words_dict = dict(it.izip(reversed(dirty_corpus_words_list), reversed(xrange(len(dirty_corpus_words_list)))))
dirty_corpus_words = set(dirty_corpus_words_list)
#dirty_words_distribution = nltk.FreqDist(dirty_corpus_words_list)
# WriteToFile(
# FilterByFrequency(
# FilterByBanglaWordPattern(
# FilterByCleanedCorpus(
# FilterByBorrowedDictionary(
# FilterByDictionary(dirty_corpus_words)
# )
# )
# )
# )
# , result_residue_path)
for w in FilterByBorrowedDictionary(dirty_corpus_words):
#for w in test_corpus_words:
wStem1 = QuickStemNounByDictionary(w)
wStem2 = QuickStemVerbByDictionary(w)
#print wStem1, wStem2
if wStem1 == None and wStem2 == None:
continue
if wStem1 == None:
if wStem2 != w:
print w, "->", wStem2, "(V)"
continue
if wStem2 == None:
if wStem1 != w:
print w, "->", wStem1, "(N)"
continue
if wStem1 == wStem2:
if wStem1 != w:
print w, "->", wStem1, "(NV)"
else:
if wStem1 != w:
print w, "->", wStem1, "(Nmult)"
if wStem2 != w:
print w, "->", wStem2, "(Vmult)"
#################################################################################