Skip to content Skip to sidebar Skip to footer

Spacy Rule-matcher Extract Value From Matched Sentence

I have a custom rule matching in spacy, and I am able to match some sentences in a document. I would like to extract some numbers now from the matched sentences. However, the match

Solution 1:

Since each match contains a single occurrence of LIKE_NUM entity you may just parse the match subtree and return the first occurrence of such a token:

value = [token for token in span.subtree if token.like_num][0]

Test:

results = []
for text in texts:
    doc = nlp(text)
    matches= matcher(doc)
    for match_id, start, endinmatches:
        span = doc[start:end]  # The matched span
        results.append([token for token in span.subtree if token.like_num][0])

print(results) # => [31, 31, 31, 31, 31,2]

Post a Comment for "Spacy Rule-matcher Extract Value From Matched Sentence"