我需要使用 python 中的 spacy 将任何句子转换为问题。
我下面的代码太长了,我需要做更多的工作才能将任何句子完成为问题格式。现在,在这段代码中,我通过检查过去时和现在时来创建基于be 形式、需要形式、有形式、执行形式的条件。
输入:尼娜拉小提琴。
输出:尼娜拉小提琴吗?
输入:芭芭拉给了我巧克力。
输出:谁给你巧克力?
输入:他明天要见乔。
输出:他明天要见谁?
输入:她来自马德里。
输出:她从哪里来?
任何人都可以帮助我!想要为所有类型的句子提出问题吗?
 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)
或者以另一种方式
适用于这些类型的格式:
这是给约翰的 - 这是给谁的?
他正在看电影——他在看什么?
山姆将于周五回来 - 山姆什么时候回来?
他们走得很快——他们走得怎么样?
她离开是因为太晚了——她为什么离开?
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)
-
-
如何使用Python中的SPACY库实现句子转疑问句?