问题
Does spacy provide a quick conversion from LIKE_NUM
token to a python float, decimal. Spacy can match a LIKE_NUM
token like “31,2”, “10.9”, “10”, “ten”, etc. Does it provide a quick way to get a python number as well? I was expecting a method like .get_value()
to return me the number (not the string), but I couldn't find any.
nlp = spacy.load('en_core_web_sm')
matcher = Matcher(nlp.vocab)
text = "this is just a text and a number 10,2 or 10.2 meaning ten point two"
doc = nlp(text)
pattern = [{"LIKE_NUM": True}]
matcher.add("number_match", None, pattern)
matches = matcher(doc)
print("All matches:")
for match_id, start, end in matches:
string_id = nlp.vocab.strings[match_id] # Get string representation
span = doc[start:end] # The matched span
print(match_id, string_id, start, end, span.text)
print(type(span.text))
Output is :
All matches:
13316671205374851783 number_match 8 9 10,2
<class 'str'>
13316671205374851783 number_match 10 11 10.2
<class 'str'>
13316671205374851783 number_match 12 13 ten
<class 'str'>
13316671205374851783 number_match 14 15 two
<class 'str'>
来源:https://stackoverflow.com/questions/59089385/spacy-like-num-cast-to-its-python-number-equivalent