oldschool-toontown/otp/chat/WhiteList.py

49 lines
1.3 KiB
Python
Raw Normal View History

2019-11-02 17:27:54 -05:00
from bisect import bisect_left
class WhiteList:
def __init__(self, wordlist):
self.words = []
for line in wordlist:
2019-12-30 18:17:24 -06:00
self.words.append(line.strip(b'\n\r').lower())
2019-11-02 17:27:54 -05:00
self.words.sort()
self.numWords = len(self.words)
def cleanText(self, text):
2022-01-19 23:02:49 -06:00
text = text.strip('.,?!')
text = text.lower().encode('utf-8')
2019-11-02 17:27:54 -05:00
return text
def isWord(self, text):
text = self.cleanText(text)
i = bisect_left(self.words, text)
if i == self.numWords:
return False
return self.words[i] == text
def isPrefix(self, text):
text = self.cleanText(text)
i = bisect_left(self.words, text)
if i == self.numWords:
return False
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
return self.words[i:j]