This repository has been archived by the owner on Feb 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentence.py
49 lines (42 loc) · 1.48 KB
/
sentence.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
#!/usr/bin/env python
class Sentence(object):
def __init__(self, n):
self.description = ''
self.tokens = []
self.ngrams = set()
self.N = n
def set_desc(self, description):
description = description.lower()
description = description.rstrip()
description = description.replace('\'', '')
description = description.replace('\"', '')
description = description.replace(',', '')
description = description.replace('.', '')
description = description.replace('-', '')
description = description.replace('/', ' ')
description = description.replace('\\', '')
description = description.replace('(', '')
description = description.replace(')', '')
description = description.replace(';', '')
self.description = description
def tokenize(self):
desc_split = self.description.split(' ')
tokens = []
for token in desc_split:
if len(token) > 4:
tokens.append(token)
self.tokens = []
self.tokens = tokens
def generate_ngrams(self):
self.ngrams = set()
for i in range(0, len(self.tokens) - self.N + 1):
s = ""
start = i
end = i + self.N
for j in range(start, end):
s += ' ' + self.tokens[j]
s = s.lstrip()
s = s.rstrip()
self.ngrams.add(s)
def get_ngrams(self):
return self.ngrams