forked from AUTOMATIC1111/stable-diffusion-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_present.py
32 lines (26 loc) · 989 Bytes
/
word_present.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
from functools import lru_cache
from nltk.stem.wordnet import WordNetLemmatizer
wnl = WordNetLemmatizer()
lemmatize = lru_cache(maxsize=50000)(wnl.lemmatize)
def binary_search(word, word_list):
left = 0
right = len(word_list) - 1
while left <= right:
mid = (left + right) // 2
if word_list[mid] == word:
return True
elif word_list[mid] < word:
left = mid + 1
else:
right = mid - 1
return False
def word_present(sentence, flagged_words):
# Iterate through every word in test sentence
for word in sentence.split():
if not binary_search(word, ['a', 'above', 'an', 'and', 'are', 'at', 'being', 'below', 'doing', 'from', 'has', 'in', 'is',
'of', 'on', 'the', 'to', 'up']):
# Lemmatize word
word = lemmatize(word, 'v')
# Check if word is in word list
if binary_search(word, flagged_words):
return True