def why(const_tree): (happening,_) = reason_cause(const_tree) if happening: q = getBinQ(happening) if q: q = "Why " + q[:1].lower() + q[1:] return q return None
def where(const_tree, nertags): (const_tree, _) = is_where(const_tree, nertags, False) if const_tree: q = getBinQ(const_tree) if q: return 'Where ' + q[:1].lower() + q[1:] else: return None else: return None
def how(const_tree): res = is_how(const_tree) if len(const_tree) > 0 and hasattr(const_tree[0], 'label') and const_tree[0].label() == ',': const_tree.remove(const_tree[0]) if not (res == (None, None)): q = getBinQ(res[0]) if q != None: q = "How " + q[:1].lower() + q[1:] return q return None
def when( const_tree, nertags ): #TODO identify pairs of dates and determine how to ask some question about it (const_tree, phr) = is_time(const_tree, nertags, False) if const_tree: q = getBinQ(const_tree) if q: return 'When ' + q[:1].lower() + q[1:] else: return None else: return None
def getQs(sentences): whereQs = [] whenQs = [] whoQs = [] whyQs = [] howQs = [] howmanyQs = [] binQs = [] whatQs = [] whereQ = None whenQ = None whoQ = None whyQ = None howQ = None howmanyQ = None binQ = None whatQ = None spacy_nlp = spacy.load('en') stop = ['bibliography', 'references', 'see also'] #parser.tagtype = 'ner' for sent in sentences: question = None if sent.strip(' ').lower() in stop: break try: const_tree1 = Tree.fromstring(parser.parse(sent))[0] const_tree2 = const_tree1.copy(deep=True) const_tree3 = const_tree1.copy(deep=True) const_tree4 = const_tree1.copy(deep=True) const_tree5 = const_tree1.copy(deep=True) const_tree6 = const_tree1.copy(deep=True) const_tree7 = const_tree1.copy(deep=True) except: continue try: nertags = parser.ner(sent) except: nertags = [] s1 = spacy_nlp(sent) for w in s1: nertags.append((str(w), w.ent_type_)) if (question == None): try: whyQ = why(const_tree4) question = whyQ except: pass if (question == None): try: howQ = how(const_tree5) question = howQ except: pass if (question == None): try: whereQ = where(const_tree1, nertags) question = whereQ except: pass if (question == None): try: whenQ = when(const_tree2, nertags) question = whenQ except: pass if (question == None): try: whoQ = who(const_tree3, nertags) question = whoQ except: pass if (question == None): try: howmanyQ = howmany(const_tree7, nertags) question = howmanyQ except: pass if (question == None): if (const_tree6[0][0]): binQ = getBinQ(const_tree6) question = binQ if binQ and len(binQ) < 200: binQs.append(addAnt(binQ, spacy_nlp)) if (question == None): try: whatQ = what(sent) question = whatQ except: pass if whereQ: whereQs.append(whereQ) #print(whereQ) if whenQ: whenQs.append(whenQ) #print(whenQ) if whoQ: whoQs.append(whoQ) #print(whoQ) if whyQ: whyQs.append(whyQ) #print(whyQ) if howQ: howQs.append(howQ) #print(howQ) if howmanyQ: howmanyQs.append(howmanyQ) if whatQ: if (len(whatQ.split()) > 3): #filter potentially bad qs if not (whereQ or whenQ or whoQ): whatQs.append(whatQ) return whereQs, whenQs, whoQs, whyQs, howQs, howmanyQs, binQs, whatQs