Skip to content

Instantly share code, notes, and snippets.

@luizamboni
Created August 3, 2021 17:09
Show Gist options
  • Save luizamboni/d16a3d2b56db266bdddae7121784daed to your computer and use it in GitHub Desktop.
Save luizamboni/d16a3d2b56db266bdddae7121784daed to your computer and use it in GitHub Desktop.
detect ranges filter with nltk
import nltk
from typing import List,Union, Dict, TypedDict, Any
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
def is_range_value(sentence: str) -> bool :
tokens: List[str] = nltk.word_tokenize(sentence)
taggeds = nltk.pos_tag(tokens)
cds = list(filter(lambda tagged_token: tagged_token[1] == 'CD', taggeds))
if len(cds) > 1:
return True
else:
return False
examples: List[Dict[str, Any]] = [
{
"message": "5 a 10 kg",
"is_range": True,
}
]
for example in examples:
print(example)
print(
is_range_value(
example["message"]
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment