2015-03-03 22:10:12 +00:00
|
|
|
from bisect import bisect_left
|
|
|
|
|
|
|
|
class WhiteList:
|
2015-03-04 18:24:11 +00:00
|
|
|
def __init__(self, words):
|
|
|
|
self.words = words
|
2015-03-03 22:10:12 +00:00
|
|
|
self.numWords = len(self.words)
|
|
|
|
|
|
|
|
def cleanText(self, text):
|
|
|
|
text = text.strip('.,?!')
|
2015-03-04 18:24:11 +00:00
|
|
|
return text.lower()
|
2015-03-03 22:10:12 +00:00
|
|
|
|
|
|
|
def isWord(self, text):
|
2015-03-04 18:24:11 +00:00
|
|
|
return self.cleanText(text) in self.words
|
2015-03-03 22:10:12 +00:00
|
|
|
|
|
|
|
def isPrefix(self, text):
|
|
|
|
text = self.cleanText(text)
|
|
|
|
i = bisect_left(self.words, text)
|
2015-03-04 18:24:11 +00:00
|
|
|
|
2015-03-03 22:10:12 +00:00
|
|
|
if i == self.numWords:
|
|
|
|
return False
|
|
|
|
|
2015-03-04 18:24:11 +00:00
|
|
|
return self.words[i].startswith(text)
|