-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathTokenisation.py
More file actions
37 lines (23 loc) · 910 Bytes
/
Tokenisation.py
File metadata and controls
37 lines (23 loc) · 910 Bytes
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
from maxmatch import maxmatch
# create a dictionary for maxmatch algo
def create_dict(filename):
f = open(filename, 'r', encoding='UTF-8')
text = f.read()
dictionary = text.split('\n')
return dictionary
def sentences_to_tokenize(filename):
f = open(filename, 'r', encoding='UTF-8')
text = f.read()
sentences = text.split('\n')
return sentences
def main():
dict = create_dict('dict.txt')
sentences = [sentence.strip() for sentence in sentences_to_tokenize('japanese_texts.txt')]
# all tokens are separated with commas
tokenized_sentences = [', '.join(filter(lambda token: token != ',', maxmatch(sentence, dict))) for sentence in sentences]
results = open('tokenization_result.txt', 'w', encoding='UTF-8')
results.write('\n'.join(tokenized_sentences))
results.close()
return tokenized_sentences
if __name__ == '__main__':
main()