1

Question

Is there a way to detect whether a token is a conjunction head in spaCy?

Problem description

I'd like the following sentence:

"Both Americans and Muslim friends and citizens, tax-paying citizens, and Muslims in nations were just appalled and could not believe what -- what we saw on our TV screens."

...to return the following custom_chunks:

custom_chunks = [
    Americans,
    Muslim friends, 
    citizens,          # `poss` modifier Muslim to be added as a hidden element
    tax-paying citizens, 
    Muslims in nations, 
    what, 
    what, 
    we, 
    TV screens]

This sentence contains a sub-conjunction within its main conjunction, which makes the task more complicated:

# main conjunction
"Americans" : Conjuncts(friends, citizens, Muslims, citizens)
"Americans" : Children(Both, and, friends, ,, citizens, ,, and, Muslims)

# sub-conjunction
"Friends : Conjuncts(Americans, citizens, Muslims, citizens)
"Friends" : Children(Muslim, and, citizens) # `children` attribute correctly identifies the sub-conjunction tokens

Presently, I'm using the following code that produces the desired answer:

# if the word has conjuncts but does not have a `conj` dependency it is the head of the main conjunction.
if word.conjuncts and word.dep != conj:
        # prev_end is the current word index
    prev_end = word.i         
    yield word.left_edge.i, word.i + 1, cc_label    
            
# if the word has a `conj` dependency and its subtree contains `conj` dependencies, it is the head of a sub-conjunction to a main conjunction
elif word.dep == conj and list(word.rights) and conj in [t.dep for t in word.rights]:
    # prev_end is the current word index
    prev_end = word.i            
    yield word.left_edge.i, word.i + 1, cc_label
    
# for when the word is not part of a conjunction    
elif word.dep in np_deps: # `conj` added to np_deps for other tokens of a conjunction
    # prev_end marks the right edge of the token subtree
    prev_end = word.right_edge.i                     
    yield word.left_edge.i, word.right_edge.i + 1, cc_label

The if and elif statements to identify both conjunction and sub-conjunction heads feels somewhat hacky, is there a more affirmative way to identify whether a token is a conjunction head, or could such an attribute be requested?

0 Answers0