コード例 #1
0
def test_get_objects_of_verb(spacy_doc):
    expected = [[], ["Python"], ["incompatibilities"], [], ["God"]]
    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_objects_of_verb(main_verb)]
                for main_verb in main_verbs]
    for obs, exp in zip(observed, expected):
        assert obs == exp
コード例 #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
コード例 #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)
コード例 #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]) + ' ?'
コード例 #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:
コード例 #6
0
[(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

Loosely, object is the piece on which our verb acts. This is almost always the answer to our "what". Let's write a question to find the objects of any verb --- or wait, we can pull it from the textacy.spacier.utils.
"""

spacy_utils.get_objects_of_verb(verb)

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

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

"""Let's look at the output of our functions for the example text. The first is the sentence itself, then the root verb, than the lemma form of that verb, followed by subjects of the verb and then objects."""

doc = nlp(example_text)
for sentence in doc.sents:
    print(sentence, sentence.root, sentence.root.lemma_, spacy_utils.get_subjects_of_verb(sentence.root), spacy_utils.get_objects_of_verb(sentence.root))

"""Bansoori is an Indian classical instrument. is be [Bansoori] [instrument]
Tom plays Bansoori and Guitar. plays play [Tom] [Bansoori, Guitar]
Let's arrange the pieces above into a neat function which we can then re-use