Exemplo n.º 1
0
def test_get_subjects_of_verb(spacy_doc):
    expected = [["tests"], ["I"], ["I"], ["programmers"], []]
    main_verbs = [
        tok for sent in spacy_doc.sents
        for tok in utils.get_main_verbs_of_sent(sent)
    ]
    observed = [[tok.text for tok in utils.get_subjects_of_verb(main_verb)]
                for main_verb in main_verbs]
    for obs, exp in zip(observed, expected):
        assert obs == exp
Exemplo n.º 2
0
def para_to_ques(eg_text):
    doc = nlp(eg_text)
    results = []
    for sentence in doc.sents:
        root = sentence.root
        ask_about = spacy_utils.get_subjects_of_verb(root)
        answers = spacy_utils.get_objects_of_verb(root)
        if len(ask_about) > 0 and len(answers) > 0:
            if root.lemma_ == "be":
                question = f'What {root} {ask_about[0]}?'
            else:
                question = f'What does {ask_about[0]} {root.lemma_}?'
            results.append({'question':question, 'answers':answers})
    return results
Exemplo n.º 3
0
    def __call__(self, text):
        """"""
        doc = self.nlp(text)
        svo = []

        for sent in doc.sents:
            chunks = ''

            # Return the main (non-auxiliary) verbs in a sentence.
            verbs = utils.get_main_verbs_of_sent(sent)

            for verb in verbs:
                # Filter negations
                negation = "".join([
                    t.text for t in sent if t.dep_ == 'neg' if t.head == verb
                ])
                # Return all subjects of a verb according to the dependency parse.
                subj = utils.get_subjects_of_verb(verb)

                # Return all objects of a verb according to the dependency parse,
                # including open clausal
                # Get noun chunks of verb
                obj = utils.get_objects_of_verb(verb)

                # Return document indexes spanning all (adjacent) tokens around a verb
                # that are auxiliary verbs or negations.
                start, end = utils.get_span_for_verb_auxiliaries(verb)
                aux = doc[start:end + 1].as_doc().text
                for o in obj:
                    for nc in sent.noun_chunks:
                        # st.write('VERB', verb.text,
                        #     'OBJ HEAD:', o.head.text, 'OBJ:', o.text,
                        #     'NC HEAD:', nc.root.head.text, 'NC:', nc.text,
                        #     'NC ANC:', [a.text for a in nc.root.head.ancestors] )
                        if o.text in nc.text.split():
                            chunks += f' {nc.text}'
                            # st.write('CHUNK:', nc.text)
                # obj = " ".join([f"{nc.text}" for nc in sent.noun_chunks if obj.text in nc.text])
                snippet = f'{" ".join([s.text for s in subj])} {negation} {aux} {chunks}'
                svo.append(snippet)
        return '. '.join(svo)
Exemplo n.º 4
0
# https://stackoverflow.com/questions/55243829/easy-way-for-sentence-to-question-using-spacy-in-python
from textacy.spacier import utils
import spacy
nlp = spacy.load("en_core_web_sm")
inp = input(
)  # The ingredients for an omelette are eggs, bacon, cheese, and onions, I do zip your code
doc = nlp(
    inp
)  # John will be finishing his homework   We were taking classes for decades
string, label = [], ""

for sentence in doc.sents:
    root = sentence.root
    for i in sentence.ents:
        if len(utils.get_subjects_of_verb(root)) or len(
                utils.get_objects_of_verb(root)) > 0:
            label = i.label_
    print(root.tag_)
    print(root.lemma_)
    print(label)
    if len(utils.get_subjects_of_verb(root)) > 0:
        if root.lemma_ == 'be':
            if label == "PERSON":
                ques = 'Who ' + str(root) + " " + str(
                    utils.get_subjects_of_verb(root)[0]) + ' ?'
            elif label == "QUANTITY":
                ques = 'How ' + str(root) + " " + str(
                    utils.get_subjects_of_verb(root)[0]) + ' ?'
            elif label == "MONEY":
                ques = 'How much ' + str(root) + " " + str(
                    utils.get_subjects_of_verb(root)[0]) + ' ?'
Exemplo n.º 5
0
from textacy.spacier import utils
    import spacy
    nlp = spacy.load("en_core_web_sm")
    inp = input()                       # The ingredients for an omelette are eggs, bacon, cheese, and onions, I do zip your code
    doc = nlp(inp)                      # John will be finishing his homework   We were taking classes for decades
    string,label = [],""
    
    for sentence in doc.sents:
        root = sentence.root
        for i in sentence.ents:
            if len(utils.get_subjects_of_verb(root)) or len(utils.get_objects_of_verb(root)) > 0:
                label = i.label_
        print(root.tag_)
        print(root.lemma_)
        print(label)
        if len(utils.get_subjects_of_verb(root)) > 0:
            if root.lemma_ == 'be':
                if label == "PERSON" :
                    ques = 'Who ' + str(root)+" "+ str(utils.get_subjects_of_verb(root)[0]) +' ?'
                elif label == "QUANTITY":
                    ques = 'How ' + str(root)+" "+ str(utils.get_subjects_of_verb(root)[0]) +' ?'
                elif label == "MONEY":
                    ques = 'How much ' + str(root) + " " + str(utils.get_subjects_of_verb(root)[0]) + ' ?'
                elif label == "TIME":
                    ques = 'When ' + str(root)+" "+ str(utils.get_subjects_of_verb(root)[0]) +' ?'
                elif label == "GPE":
                    ques = 'Where ' + str(root)+" "+ str(utils.get_subjects_of_verb(root)[0]) +' ?'
                elif label == 'PRODUCT':
                    ques = 'What ' + str(root)+" "+ str(utils.get_subjects_of_verb(root)[0]) +' ?'
                else:
                    for to in doc:
Exemplo n.º 6
0
doc = nlp(toy_sentence)

"""What are the entities in this sentence?"""

displacy.render(doc, style='ent', jupyter=True)

# Let's find out the main verb in this sentence:

verbs = spacy_utils.get_main_verbs_of_sent(doc)
print(verbs)

# And what are nominal subjects of this verb?

 
for verb in verbs:
    print(verb, spacy_utils.get_subjects_of_verb(verb))

"""*You will notice that this has a reasonable overlap with the noun phrases which we pulled from our part-of-speech tagging but can be different as well.*"""

[(token, token.tag_) for token in doc]

"""Tip: As an exercise, extend this approach to at least add Who, Where and When questions as practice.

## Level Up: Question and Answer

So far, we have been trying to generate questions. But if you were trying to make an automated quiz for students, you would also need to mine the right answer.

The answer in this case will be simply the objects of verb. What is an object of verb?

In the sentence, "Give the book to me," "book" is the direct object of the verb "give," and "me" is the indirect object. - from the Cambridge English Dictionary