-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.py
80 lines (64 loc) · 2.19 KB
/
tokenizer.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
import nltk
# NLTK stoplist with 3136 words
STOPLIST = set(nltk.corpus.stopwords.words())
class Tokenizer():
'''
Used to split text
Including two main function tokenize and sent_tokenize
tokenize split text into words list
sent_tokenize split text into sents list
'''
def __init__(self, stop_words=False, lower=False, eng=False):
self.stop_words = stop_words
self.lower = lower
def tokenize(self, text):
'''
tokenize the text into tokens list
take string as input
output words' list
'''
if self.lower:
text = text.lower()
token_list = text.split()
if self.stop_words:
token_list_without_stop = list()
for token in token_list:
if token not in STOPLIST:
token_list_without_stop.append(token)
return token_list_without_stop
return token_list
def sent_tokenize(self, text):
'''
A sample implementation of sentence tokenize
Using . ? and ! to samply seperate text data
take text as input, output a sentence list
'''
list_sent = list()
for sent in text.split('.'):
if '?' in sent:
for sub_sent in sent.split('?'):
if '!' in sub_sent:
for sub_sub_sent in sent.split('!'):
list_sent.append(sub_sub_sent.split())
else:
list_sent.append(sub_sent.split())
elif '!' in sent:
for sub2_sent in sent.split('!'):
list_sent.append(sub2_sent.split())
elif sent:
list_sent.append(sent.split())
else:
pass
return list_sent
def test():
'''
testing function
'''
tokenizer = Tokenizer(lower=True)
test_sent = "God is Great! I won a lottery."
sent_list = tokenizer.sent_tokenize(test_sent)
print(sent_list)
for sent in sent_list:
print(tokenizer.tokenize(sent))
if __name__ == "__main__":
test()