2015-03-03 16:10:12 -06:00
|
|
|
from bisect import bisect_left
|
|
|
|
|
|
|
|
class WhiteList:
|
2015-03-04 12:24:11 -06: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-03-04 12:24:11 -06:00
|
|
|
return text.lower()
|
2015-03-03 16:10:12 -06:00
|
|
|
|
|
|
|
def isWord(self, text):
|
2015-03-04 12:24:11 -06: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-03-04 12:24:11 -06:00
|
|
|
|
2015-03-03 16:10:12 -06:00
|
|
|
if i == self.numWords:
|
|
|
|
return False
|
|
|
|
|
2015-03-04 12:24:11 -06:00
|
|
|
return self.words[i].startswith(text)
|