-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathngram.py
35 lines (34 loc) · 931 Bytes
/
ngram.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
class ngram(object):
def __init__(self):
self.n = 1
self.sList={};
def update(self,inputStr,lang):
try:
letterList = self.sList[lang]
except:
letterList = {}
maxGram = len(inputStr)
if maxGram > self.n:
maxGram = self.n
while maxGram > 0:
fPoint = 0
lPoint = maxGram
while lPoint < len(inputStr) + 1:
try:
letterList[str(maxGram)] = letterList[str(maxGram)] + 1 #Total number of each n letter sets
except:
letterList[str(maxGram)] = 1
try:
letterList[inputStr[fPoint:lPoint]] = letterList[inputStr[fPoint:lPoint]] + 1 #Value of a number of a letter
except:
letterList[inputStr[fPoint:lPoint]] = 1
fPoint = fPoint + 1
lPoint = lPoint + 1
maxGram = maxGram - 1
self.sList[lang] = letterList
def getsList(self):
return self.sList
def clearsList(self):
self.sList = {}
def setsList(self,sL):
self.sList = sL