Spacy LIKE_NUM cast to it's python number equivalent

梦想的初衷 提交于 2019-12-11 16:28:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!