Last active
June 28, 2020 17:14
-
-
Save aniruddha27/a7435e199bf27478d8a18d530667e6bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# function for rule 1: noun(subject), verb, noun(object) | |
def rule1(text): | |
doc = nlp(text) | |
sent = [] | |
for token in doc: | |
# if the token is a verb | |
if (token.pos_=='VERB'): | |
phrase ='' | |
# only extract noun or pronoun subjects | |
for sub_tok in token.lefts: | |
if (sub_tok.dep_ in ['nsubj','nsubjpass']) and (sub_tok.pos_ in ['NOUN','PROPN','PRON']): | |
# add subject to the phrase | |
phrase += sub_tok.text | |
# save the root of the verb in phrase | |
phrase += ' '+token.lemma_ | |
# check for noun or pronoun direct objects | |
for sub_tok in token.rights: | |
# save the object in the phrase | |
if (sub_tok.dep_ in ['dobj']) and (sub_tok.pos_ in ['NOUN','PROPN']): | |
phrase += ' '+sub_tok.text | |
sent.append(phrase) | |
return sent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment