How to convert Sentence to Question using spacy library in python [Refer my code Below for Correction]

时间:2019-03-19 14:52:00

标签: python nlp spacy

I need to convert any sentence to question using spacy in python.

My Code below goes so long and i need even more to be done to complete any sentence into question format. Now in this code I make condition based on be form, need form, have form, do form by checking past tense and present tense.

INPUT: Nina plays the violin.

OUTPUT: Does Nina play the violin?

INPUT: Barbara gave me the chocolates.

OUTPUT: Who gave you the chocolates?

INPUT: He is seeing Joe tomorrow.

OUTPUT: Who is he seeing tomorrow?

INPUT: She comes from Madrid.

OUTPUT: Where does she come from?

Anybody can help me in this! Want to form question for all type of sentence?

 from textacy.spacier import utils
    import spacy
    nlp = spacy.load("en_core_web_sm")
    inp = input()                       
    doc = nlp(inp)                      
    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]) +' ?'

            string.append(ques)

    print(string)

OR IN ANOTHER WAY

Works for these type of formats:

This is for John - Who is this for?

He was watching a film - What was he watching?

Sam will be back on Friday - When will Sam be back?

They were walking fast - How were they walking?

She left because it was late - Why did she leave?

from textacy.spacier import utils
from textacy.spacier import utils as spacy_utils
 import spacy
 import re
 nlp = spacy.load("en_core_web_sm")
-inp = input()
-doc = nlp(inp)
-stop,modal_root,form_root,root = "","","",""
-
-
-for sentence in doc:
-    if sentence.dep_ in ['aux','ROOT']:
-        if sentence.lemma_ in ['must', 'shall', 'will', 'should', 'would', 'can', 'could', 'may','might']:
-            modal_root = sentence
-        elif sentence.lemma_ in ['be','have']:
-            form_root = sentence
-        else:
-            root = sentence
-        for children in sentence.subtree:
-            if children.text in ['because', 'so because']:
-                check = children
-            if children.dep_ in ['dobj','pobj','advmod','acomp']:
-                child = children
-                for prep in child.subtree:
-                    if prep.is_stop:
-                        stop = prep
-                if child.ent_type_:
-                    label = child.ent_type_
-                elif child.pos_ == "ADV":
-                    label = "QUANTITY"
-                else:
-                    label = ""
-
-if modal_root and form_root:
-    root = modal_root
-elif modal_root:
-    root = modal_root
-elif form_root:
-    root = form_root
-
-
-for lemma in doc:
-    if lemma.text in ['we','I']:
-        lem = lemma.text
-    else:
-        lem = ""
-
-if stop:
-    sent = doc.text.replace(str(child),"")
-    sent = sent.replace(" "+str(stop)+" ", "")
-else:
-    sent = doc.text.replace(str(child), "")
-
-if lem:  sent = sent.replace(lem, "you")
-
-if root.lemma_ in ['be','have','must', 'shall', 'will', 'should', 'would', 'can', 'could', 'may', 'might']:
-    if label == 'PERSON':
-        ques = 'who '+str(root) + " " + re.sub('{}'.format(" "+str(root)+" ").lower(), ' ', sent)+'?'
-    elif label in ['GPE','LOC']:
-        ques = 'where '+str(root) + " " + re.sub('{}'.format(" "+str(root)+" ").lower(), ' ', sent)+'?'
-    elif label in ['TIME','DATE']:
-        ques = 'when ' + str(root) + " " + re.sub('{}'.format(" "+str(root)+" ").lower(), ' ', sent)+'?'
-    elif label in ['QUANTITY']:
-        ques = 'How ' + str(root) + " " + re.sub('{}'.format(" "+str(root)+" ").lower(), ' ', sent)+'?'
-    else:
-        ques = 'what ' + str(root) + " " + re.sub('{}'.format(" " + str(root) + " ").lower(), ' ', sent) + '?'
-else:
-    if root.tag_ == 'VBD' or str(utils.get_subjects_of_verb(root)[0]).upper() in ['I', 'You', 'We', 'They', 'He', 'She','It']:
-        if check.text in ['because','so because']:
-            ques = 'Why did ' + str(utils.get_subjects_of_verb(root)[0]) + " " + root.lemma_ + '?'
-        else:
-            ques = 'Did ' + str(utils.get_subjects_of_verb(root)[0]) + " " + root.lemma_ + '?'
-    elif str(utils.get_subjects_of_verb(root)[0]).upper() in ['I', 'You', 'We', 'They']:
-        ques = 'Do ' + str(utils.get_subjects_of_verb(root)[0]) + " " + root.lemma_ + '?'
-    elif str(utils.get_subjects_of_verb(root)[0]).upper() in ['He', 'She', 'It'] or label == "PERSON":
-        ques = 'Does ' + str(utils.get_subjects_of_verb(root)[0]) + " " + root.lemma_ + '?'
-
-print(ques)
-
-

HOW TO ACHIEVE CONVERTING SENTENCE INTO QUESTION USING SPACY LIBRARY IN PYTHON?

0 个答案:

没有答案