Skip to content Skip to sidebar Skip to footer

How To Get The Span Of A Conjunct In Spacy?

I use spacy, token.conjuncts to get the conjuncts of each token. However, the return type of the token.conjuncts is tuple, but I want to get the span type, for example: import spac

Solution 1:

token.conjuncts returns a tuple of tokens. To get a span, call doc[conj.i: conj.i+1]

import spacy

nlp = spacy.load('en_core_web_sm')


sentence = "I like oranges and apples and lemons."


doc = nlp(sentence)

for token in doc:
    if token.conjuncts:
        conjuncts = token.conjuncts             # tuple of conjuncts
        print("Conjuncts for ", token.text)
        for conj in conjuncts:
            # conj is type of Token
            span = doc[conj.i: conj.i+1]        # Here's span
            print(span.text, type(span))

Post a Comment for "How To Get The Span Of A Conjunct In Spacy?"