2015-03-03 16:10:12 -06:00
|
|
|
from bisect import bisect_left
|
2015-03-31 06:59:36 -05:00
|
|
|
|
2015-04-01 06:39:59 -05:00
|
|
|
class WhiteList:
|
2015-04-01 15:30:08 -05:00
|
|
|
def __init__(self, words):
|
|
|
|
self.words = words
|
2015-03-03 16:10:12 -06:00
|
|
|
self.numWords = len(self.words)
|
|
|
|
|
|
|
|
def cleanText(self, text):
|
|
|
|
text = text.strip('.,?!')
|
2015-04-01 15:30:08 -05:00
|
|
|
return text.lower()
|
2015-03-03 16:10:12 -06:00
|
|
|
|
|
|
|
def isWord(self, text):
|
2015-04-01 15:30:08 -05:00
|
|
|
return self.cleanText(text) in self.words
|
2015-03-03 16:10:12 -06:00
|
|
|
|
|
|
|
def isPrefix(self, text):
|
|
|
|
text = self.cleanText(text)
|
|
|
|
i = bisect_left(self.words, text)
|
2015-04-01 15:30:08 -05:00
|
|
|
|
2015-03-03 16:10:12 -06:00
|
|
|
if i == self.numWords:
|
|
|
|
return False
|
|
|
|
|
2015-04-02 07:23:24 -05:00
|
|
|
return self.words[i].startswith(text)
|
|
|
|
|
|
|
|
def prefixCount(self, text):
|
|
|
|
text = self.cleanText(text)
|
|
|
|
i = bisect_left(self.words, text)
|
|
|
|
j = i
|
|
|
|
while j < self.numWords and self.words[j].startswith(text):
|
|
|
|
j += 1
|
|
|
|
|
|
|
|
return j - i
|
|
|
|
|
|
|
|
def prefixList(self, text):
|
|
|
|
text = self.cleanText(text)
|
|
|
|
i = bisect_left(self.words, text)
|
|
|
|
j = i
|
|
|
|
while j < self.numWords and self.words[j].startswith(text):
|
|
|
|
j += 1
|
|
|
|
|
2015-06-15 19:56:12 -05:00
|
|
|
return self.words[i:j]
|