def company_relationship(Subject, Object): """ Ex: Gary Sykes (Born 13 February 1984) is a British super featherweight boxer. """ anything = Star(Any()) born = Star(Pos(":")) + Question(Token("Bought") | Token("buy")) + Question(Token("c.")) entity_leftover = Star(Pos("NNP")) return Subject + entity_leftover + Pos("-LRB-") + born + Object + Pos("-RRB-") + anything
def born_date_in_parenthesis(Subject, Object): """ Ex: Gary Sykes (Born 13 February 1984) is a British super featherweight boxer. """ anything = Star(Any()) born = Star(Pos(":")) + Question(Token("Born") | Token("born")) + Question( Token("c.")) entity_leftover = Star(Pos("NNP")) return Subject + entity_leftover + Pos("-LRB-") + born + Object + Pos( "-RRB-") + anything
def born_two_dates_in_parenthesis(Subject, Object): """ Ex: James Cunningham (born 1973 or 1974) is a Canadian stand-up comedian and TV host. """ anything = Star(Any()) born = Question(Token("Born") | Token("born")) entity_leftover = Star(Pos("NNP")) subject = Subject + entity_leftover or_object = (Object + Token("or") + Pos("CD") | Pos("CD") + Token("or") + Object) return subject + Pos("-LRB-") + born + or_object + Pos("-RRB-") + anything
def born_date_and_death_in_parenthesis(Subject, Object): """ Ex: Carl Bridgewater (January 2, 1965 - September 19, 1978) was shot dead """ anything = Star(Any()) return Subject + Pos("-LRB-") + Object + Token("-") + anything + Pos( "-RRB-") + anything
def mentions_real_name(Subject, Object): """ Ex: Harry Pilling, born Ashtonunder-Lyne, Lancashire on 2 February 1943, played ... """ anything = Star(Any()) real_name = Plus(Pos("NNP") + Question(Token(","))) return Subject + Token("born") + real_name + Pos("IN") + Object + anything
def was_born_explicit_mention(Subject, Object): """ Ex: Shamsher M. Chowdhury was born in 1950. """ anything = Star(Any()) return anything + Subject + Token("was born") + Pos( "IN") + Object + anything
def incorrect_labeling_of_place_as_person(Subject, Object): """ Ex: Sophie Christiane of Wolfstein (24 October 24, 1667 - 23 August 1737) Wolfstein is a *place*, not a *person* """ anything = Star(Any()) person = Plus(Pos("NNP") + Question(Token(","))) return anything + person + Token("of") + Subject + anything
def was_born_and_mentions_place_2(Subject, Object): """ Ex: Theodone C. Hu was born in 1872 in Huangpu town, Haizhu District, Guangzhou, Guangdong, China. """ anything = Star(Any()) place = Plus(Pos("NNP") + Question(Token(","))) return Subject + Token("was born") + Pos("IN") + Object + Pos( "IN") + place + anything
def customize_rules(): # some rules for matching # TODO: customize your own rules here person = (W(pos="nr") | W(pos="x") | W(pos="nrt") | W(pos="nz")) movie = (W(pos="nz")) place = (W("出生地") | W("出生")) intro = (W("简介") | W(pos="介绍")) rules = [ Rule(condition=W(pos="r") + W("是") + person | \ person + W("是") + W(pos="r"), action=who_is_question), Rule(condition=person + Star(Any(), greedy=False) + place + Star(Any(), greedy=False), action=where_is_from_question), Rule(condition=movie + Star(Any(), greedy=False) + intro + Star(Any(), greedy=False) , action=movie_intro_question) ] return rules
class LowMatchAny(RegexTemplate): weight = 0.5 regex = Star(Any()) def semantics(self, match): expr = None for word in match.words: if expr is not None: expr += HasKeyword(word.token) else: expr = HasKeyword(word.token) return expr
class LowMatchAny(QuestionTemplate): weight = 0.5 regex = Star(Any()) def interpret(self, match): expr = None for word in match.words: if expr is not None: expr += HasKeyword(word.token) else: expr = HasKeyword(word.token) return expr
class RegexTemplate(object): """ Regex base. Subclass from this to implement a regex handler. """ regex = Star(Any()) # Must define when subclassing weight = 1 # Redefine this to give different priorities to your regexes. def semantics(self, match): """ Returns the semantics of the regex. `match` is of type `quepy.regex.Match` and is analogous to a python re match. It contains matched groups in the regular expression. When implementing a regex one must fill this method. """ raise NotImplementedError() def get_semantics(self, words): rulename = self.__class__.__name__ logger.debug("Trying to match with regex: {}".format(rulename)) match = refo.match(self.regex + Literal(_EOL), words + [_EOL]) if not match: logger.debug("No match") return None, None try: match = Match(match, words) result = self.semantics(match) except BadSemantic as error: logger.debug(str(error)) return None, None try: expression, userdata = result except TypeError: expression, userdata = result, None expression.rule_used = rulename return expression, userdata
class NeuronsQuery_MoreSpecific(QuestionTemplate): weight = 3 subquery = G(Qu(opener), 'opener') \ + Qu(universal_quantifier) + Qu(L('of')) \ + Qu(L('the') | L('a') | L('an')) \ + ( (Qu( neuron_modifiers ) + neurons) | (neuron_modifiers + Qu(neurons)) ) \ + (Qu(synaptic_phrase) + Qu(clauses)) \ + Qu(Qu(L('and') | L('or')) \ + in_lem + G(in_region_list, 'region_list')) + \ Qu(clauses) subqueries = subquery + Star(Qu(P(',')) + (L('and') | L('or')) + subquery) regex = subqueries \ + Qu(G(color, 'color') | G(color, 'varcolor')) \ + Qu(L('as') + Qu(L('a')) + G(noun, 'formatting')) \ + Qu(P('.')) def interpret(self, match): return interpret_NeuronsQuery_MoreSpecific(self, match)
# TODO 问题模板/匹配规则 """ 1. 某校区包含哪些校园(多少个) 2. 某校区/校园包含哪些学院(多少个) 3. 某校区/校园/学院包含哪些专业(多少个) 4. 某专业属于哪些(个)学院 5. 某专业/学院/栋楼属于哪些(个)校园 6. 某专业/学院/校园/栋楼属于哪些(个)校区 7. 某校园的通讯地址/面积 8. 某学院的通讯地址 || 某栋楼的地址/tag || 某校园/校区包含哪些楼 """ rules = [ # 属于 Rule(condition_num=3, condition=my_entity + Star(Any(), greedy=False) + ((belongTo + Star(Any(), greedy=False) + zone) | (W("是") + Star(Any(), greedy=False) + zone + Star(Any(), greedy=False) + W("的"))) + Star(Any(), greedy=False), action=QuestionSet.belongTo_zone_question), Rule(condition_num=3, condition=my_entity + Star(Any(), greedy=False) + ((belongTo + Star(Any(), greedy=False) + campus) | (W("是") + Star(Any(), greedy=False) + campus + Star(Any(), greedy=False) + W("的"))) + Star(Any(), greedy=False), action=QuestionSet.belongTo_campus_question), Rule(condition_num=3, condition=my_entity + Star(Any(), greedy=False) + ((belongTo + Star(Any(), greedy=False) + faculty) | (W("是") + Star(Any(), greedy=False) + faculty + Star(Any(), greedy=False) + W("的"))) + Star(Any(), greedy=False),
drug_to_person_entity = (W(pos=pos_drug_to_person)) case = (W("案件") | W("案子") | W("参案")) person = (W("人") | W("人员") | W("被告人")) cooperation = (W("同伙") | W("同伴")) trade = (W("交易关系") | W("交易")) drug = (W("毒品")) several = (W("多少") | W("几次")) seller = (W("贩毒人员")) information = (W("基本信息") | W("基本资料")) sell = (W("贩卖") | W("卖")) time = (W("年") | W("时间")) rules = [ Rule(condition_num=2, condition=person_entity + Star(Any(), greedy=False) + case + Star(Any(), greedy=False), action=QuestionSet.has_case_question), Rule(condition_num=2, condition=case_entity + Star(Any(), greedy=False) + person + Star(Any(), greedy=False), action=QuestionSet.has_person_question), Rule(condition_num=3, condition=person_entity + Star(Any(), greedy=False) + person_entity + Star(Any(), greedy=False) + cooperation, action=QuestionSet.has_cooperation_question), Rule(condition_num=3, condition=person_entity + Star(Any(), greedy=False) + drug_to_person_entity + Star(Any(), greedy=False) + trade, action=QuestionSet.has_trade_question), Rule(condition_num=3,
"default_field": "dynasty", "query": w.token } }] } }, "size": 100 } break return query rules = [ Rule(condition_num=1, condition=((poet_entity + Star(Any(), greedy=False) + poem + Star(Any(), greedy=False)) | (poem + Star(Any(), greedy=False) + poet_entity + Star(Any(), greedy=False))), action=Question.poet_search_question), Rule(condition_num=2, condition=((poem_entity + Star(Any(), greedy=False) + poet + Star(Any(), greedy=False)) | (poet + Star(Any(), greedy=False) + poem_entity + Star(Any(), greedy=False))), action=Question.poem_search_question), Rule(condition_num=3, condition=(poet_entity + Star(Any(), greedy=False) + dynasty + Star(Any(), greedy=False)), action=Question.poet_search_question), Rule(condition_num=4,
for w in word_objects: if w.pos == pos_book_or_movie: e_douban = u"?m :movie_info_name '{movie}'.\n" \ u"?m :has_director ?a.\n" \ u"?a :movie_person_name ?x".format(movie=w.token) e_db = u"?m rdfs:label '{movie}'@en.\n" \ u"?m dbo:director ?p.\n" \ u"?p foaf:name ?x".format(movie=re.sub(r"(\w)([A-Z])", r"\1 \2", w.token)) sparql = SPARQL_SELECT_TEM_FD.format(prefix=SPARQL_PREFIX, select=select, expression_douban=e_douban, expression_db=e_db) break return sparql movie_info_rules_fd = [ Rule(condition_num=11, condition=book_or_movie_entity + Star(Any(), greedy=False) + movie_info + Star(Any(), greedy=False), action=QuestionSet.has_movie_info), Rule(condition_num=11,condition=(book_or_movie_entity + Star(Any(), greedy=False) + actor + Star(Any(), greedy=False)) | (actor + Star(Any(), greedy=False) + book_or_movie_entity + Star(Any(), greedy=False)), action=QuestionSet.has_actor), Rule(condition_num=11,condition=(book_or_movie_entity + Star(Any(), greedy=False) + writer + Star(Any(), greedy=False)) | (writer + Star(Any(), greedy=False) + book_or_movie_entity + Star(Any(), greedy=False)), action=QuestionSet.has_writer), Rule(condition_num=11,condition=(book_or_movie_entity + Star(Any(), greedy=False) + director + Star(Any(), greedy=False)) | (writer + Star(Any(), greedy=False) + book_or_movie_entity) + Star(Any(), greedy=False), action=QuestionSet.has_director), ] basic_movie_info_fd = [ KeywordRule(condition=(book_or_movie_entity + Star(Any(), greedy=False) + country + Star(Any(), greedy=False))|( country + Star(Any(), greedy=False) + book_or_movie_entity + Star(Any(), greedy=False)), action=MoviePropertyValueSet.return_movie_info_country_value_FD), KeywordRule(condition=(book_or_movie_entity + Star(Any(), greedy=False) + language + Star(Any(), greedy=False))|( language + Star(Any(), greedy=False) + book_or_movie_entity + Star(Any(), greedy=False)), action=MoviePropertyValueSet.return_movie_info_language_value_FD), KeywordRule(condition=(book_or_movie_entity + Star(Any(), greedy=False) + duration + Star(Any(), greedy=False))|( duration + Star(Any(), greedy=False) + book_or_movie_entity + Star(Any(), greedy=False)), action=MoviePropertyValueSet.return_movie_info_duration_value_FD), KeywordRule(condition=(book_or_movie_entity + Star(Any(), greedy=False) + summary + Star(Any(), greedy=False))|( summary + Star(Any(), greedy=False) + book_or_movie_entity + Star(Any(), greedy=False)), action=MoviePropertyValueSet.return_movie_info_summary_value_FD), ]
def just_born(Subject, Object): """ Ex: Lyle Eugene Hollister, born 6 July 1923 in Sioux Falls, South Dakota, enlisted in the Navy.... """ anything = Star(Any()) return Subject + Token(", born") + Object + anything
def test_rule(Subject, Object): anything = Question(Star(Any())) return Subject + Token("(") + Object + Token("-") + anything
#普通名词 common_noun = W(pos=pos_common_noun) #比较关键词 larger = (W("bigger") | W("larger")) smaller = (W("smaller") | W("lower")) compare = (larger | smaller) rules = [ # What is the name of sb-uname? # What is the age of sb-uname ? # What is the username of sb-uname? # What is the phone number of sb-uname? # What is the password of sb-name? Rule(condition_num=3, condition=what + Star(Any(), greedy=False) + attr_noun + Star(Any(), greedy=False) + of + common_noun + Star(Any(), greedy=False), action=QuestionSet.proccess_attr_noun), # Whose age is larger than 18 ? Rule(condition_num=4, condition=whose + attr_noun + Star(Any(), greedy=False) + compare + Star(Any(), greedy=False) + number_entity + Star(Any(), greedy=False), action=QuestionSet.who_age_compare) ] # 合并的好处是,减少上面的规则数,遍历更快,如果吧下面的放在上面,拿那么如果访问比较多最后一类,那效率就很低 attr_keyword_rules = [ KeywordRule(condition=what + Star(Any(), greedy=False) + name + Star(Any(), greedy=False) + of + common_noun + Star(Any(), greedy=False),
# TODO 问题模板/匹配规则 """ 1. 某演员演了什么电影 2. 某电影有哪些演员出演 3. 演员A和演员B合作出演了哪些电影 4. 某演员参演的评分大于X的电影有哪些 5. 某演员出演过哪些类型的电影 6. 某演员出演的XX类型电影有哪些。 7. 某演员出演了多少部电影。 8. 某演员是喜剧演员吗。 9. 某演员的生日/出生地/英文名/简介 10. 某电影的简介/上映日期/评分 """ rules = [ Rule(condition_num=2, condition=person_entity + Star(Any(), greedy=False) + movie + Star(Any(), greedy=False), action=QuestionSet.has_movie_question), Rule(condition_num=2, condition=(movie_entity + Star(Any(), greedy=False) + actor + Star(Any(), greedy=False)) | (actor + Star(Any(), greedy=False) + movie_entity + Star(Any(), greedy=False)), action=QuestionSet.has_actor_question), Rule(condition_num=3, condition=person_entity + Star(Any(), greedy=False) + person_entity + Star(Any(), greedy=False) + (movie | Star(Any(), greedy=False)), action=QuestionSet.has_cooperation_question), Rule(condition_num=4, condition=person_entity + Star(Any(), greedy=False) + compare + number_entity + Star(Any(), greedy=False) + movie +
break return sparql_list # 问题模版, 匹配规则 """ # 某书籍的图片/出版社/出版日期/页数/简介/目录/评分/评价人数 # 某书籍的类别 # 某书籍的作者 # 某书籍的译者 # 某书籍的详细信息 """ book_info_rules = [ Rule(condition_num=3, condition=book_or_movie_entity + Star(Any(), greedy=False) + book_info + Star(Any(), greedy=False), action=QuestionSet.has_book_info), Rule(condition_num=3, condition=book_or_movie_entity + Star(Any(), greedy=False) + genre + Star(Any(), greedy=False), action=QuestionSet.has_book_genre), Rule(condition_num=3, condition=book_or_movie_entity + Star(Any(), greedy=False) + author + Star(Any(), greedy=False), action=QuestionSet.has_author), Rule(condition_num=3, condition=book_or_movie_entity + Star(Any(), greedy=False) + translator + Star(Any(), greedy=False), action=QuestionSet.has_translator), Rule(condition_num=3, condition=book_or_movie_entity + Star(Any(), greedy=False) + detail_information + Star(Any(), greedy=False), action=QuestionSet.has_detail_information) ] basic_book_info = [ KeywordRule(condition=book_or_movie_entity + Star(Any(), greedy=False) + image_url + Star(Any(), greedy=False), action=BookPropertyValueSet.return_book_info_image_url_value), KeywordRule(condition=book_or_movie_entity + Star(Any(), greedy=False) + press + Star(Any(), greedy=False),action=BookPropertyValueSet.return_book_info_press_value), KeywordRule(condition=book_or_movie_entity + Star(Any(), greedy=False) + publish_year + Star(Any(), greedy=False),action=BookPropertyValueSet.return_book_info_publish_year_value), KeywordRule(condition=book_or_movie_entity + Star(Any(), greedy=False) + page_num + Star(Any(), greedy=False),action=BookPropertyValueSet.return_book_info_page_num_value), KeywordRule(condition=book_or_movie_entity + Star(Any(), greedy=False) + price + Star(Any(), greedy=False),action=BookPropertyValueSet.return_book_info_price_value), KeywordRule(condition=book_or_movie_entity + Star(Any(), greedy=False) + content_abstract + Star(Any(), greedy=False),action=BookPropertyValueSet.return_book_info_content_abstract_value), KeywordRule(condition=book_or_movie_entity + Star(Any(), greedy=False) + catalog + Star(Any(), greedy=False),action=BookPropertyValueSet.return_book_info_catalog_value), KeywordRule(condition=book_or_movie_entity + Star(Any(), greedy=False) + rating + Star(Any(), greedy=False),action=BookPropertyValueSet.return_book_info_rating_value),
def list_of(things): thing_seq = Plus(R(lowercase_is_in(things))) return thing_seq + Star(Qu(P(',')) + Qu(L('and') | L('or')) + thing_seq)
""" 1.某company的英文名 经营范围 公司类型 公司口号 年营业额 董事长 成立时间 总部地址 电话 网站 2.某company的成立时间[什么时候/何时成立?] 3.某person/company 4.某company提供/有 哪些类型的服务/服务的类型 5.某company 有哪些 XX 类型 的 服务 (有哪些) 6.某company提供/有 哪些服务 7.某company XX 服务 7. 某演员出演了多少部电影。 8. 某演员是喜剧演员吗。 """ rules = [ Rule(condition_num=2, condition=company_entity + Star(Any(), greedy=False) + company_basic + Star(Any(), greedy=False), action=QuestionSet.has_company_basicinfo_question), Rule(condition_num=2, condition=company_entity + Star(Any(), greedy=False) + when + incorporate + Star(Any(), greedy=False), action=QuestionSet.has_company_basicinfo_question), Rule(condition_num=1, condition=Star(Any(), greedy=False) + (company_entity | person_entity) + Star(Any(), greedy=False), action=QuestionSet.has_companyOrPerson_all_basicinfo_question), #4 Rule(condition_num=3, condition=company_entity + Star(Any(), greedy=False) + (word_type + Star(Any(), greedy=False) + word_service | word_service + Star(Any(), greedy=False) + word_type),
import argparse parser = argparse.ArgumentParser(description="Prints an xml document tree" + "... sort of.") parser.add_argument("filename", action="store") cfg = parser.parse_args() text = open(cfg.filename).read() from refo import finditer, Predicate, Literal, Any, Group, Star def notin(xs): return lambda x: x not in xs name = Predicate(notin("/")) + Star(Predicate(notin(" >"))) name = Group(name, "name") inside = name + Star(Any(), greedy=False) opentag = Literal("<") + inside + Literal(">") opentag = Group(opentag, "open") closetag = Literal("<") + Literal("/") + inside + Literal(">") closetag = Group(closetag, "close") regex = closetag | opentag depth = 0 for m in finditer(regex, text): if "open" in m: i, j = m["name"] print(" " * depth + text[i:j]) depth += 1 else:
# NOTE: We don't yet have a (hard-coded, at least) list of supported genetic markers yet, # so this is largely unused. expressing_marker = L('express') + G(nouns, 'expressing_marker') transmitters_clause = ( ( Qu(L('that')|L('which')) + (L('transmit')|L('release')) ) | ( ((Qu(L('that')) + L('have')) | L('with')) + Qu(L('transmitter') | L('neurotransmitter'))) ) \ + G( list_of(transmitters), 'transmitters' ) brainregion = Qu(P('DT')) + notneurons brainregion2 = Qu(P('DT')) + R(lowercase_is_in(regions)) brainregion_arb = Qu(P('DT')) + R(lowercase_is_in(arbregions)) in_lem = Qu(L('be')) + (L('in') | L('within') | (L('inside') + Qu(L('of'))) | L('of') | L('from')) + Star(L('both')) connection_cnoun = ( Qu(L('synaptic')) + L('connection') ) \ | ( L('dendrite') | L('axon') | L('process') | L('input') | L('output') ) \ | ( Qu( L('dendritic') | L('axonic') | L('axon') | L('axonal') ) + ( L('arborization') | L('arborizations') | L('innervation') ) ) connections = ( Qu( L('with') | (Qu(L('that')) + L('have')) ) + connection_cnoun + in_lem ) \ | ( Qu(L('that') | L('which')) + (L('innervate') | L('arborize')) + Qu(in_lem) ) connections_clause = G( (((L('with') | (Qu(L('that')) + L('have'))) + connection_cnoun + in_lem) | (Qu(L('that') | L('which')) + (L('innervate') | L('arborize')) + Qu(in_lem))) + brainregion_arb + Star(
def is_born_in(Subject, Object): """ Ex: Xu is born in 1902 or 1903 in a family of farmers in Hubei .. """ anything = Star(Any()) return Subject + Token("is born in") + Object + anything
def apply(self, sentence): matches = [] for m in finditer(self.condition, sentence): i, j = m.span() matches.extend(sentence[i:j]) return self.action(matches), self.condition_num rules = [ # 【疾病】是什么? Rule( condition_num=1, condition=Words.WORD_DISEASE + Words.WORD_IS + Words.WORD_ASK + Star(Any(), greedy=False), action=QuestionSet.disease_info, ), # 【药品】是什么? Rule( condition_num=1, condition=Words.WORD_MEDICINE + Words.WORD_IS + Words.WORD_ASK + Star(Any(), greedy=False), action=QuestionSet.medicine_info, ), # 【疾病】怎么办? Rule( condition_num=1, condition=Words.WORD_DISEASE + Star(Any(), greedy=False) + Words.WORD_ASK + Star(Any(), greedy=False), action=QuestionSet.medicine_for_disease,
# 某人演了哪些电影 # 某人写了哪些电影 # 某人指导了哪些电影 # 某人的详细信息 # 未完成 # 某人出演了多少部电影 # 某人写了多少部电影 # 某人导演了多少部电影 # 某演员参演的评分大于X的电影有哪些 # 某演员出演过哪些类型的电影 # 演员A和演员B合作出演了哪些电影 """ movie_person_rules = [ Rule(condition_num=2, condition=person_entity + Star(Any(), greedy=False) + movie_person_info + Star(Any(), greedy=False), action=QuestionSet.has_movie_person_info), Rule(condition_num=2, condition=person_entity + Star(Any(), greedy=False) + director + Star(Any(), greedy=False), action=QuestionSet.has_directed_in), Rule(condition_num=2, condition=person_entity + Star(Any(), greedy=False) + writer + Star(Any(), greedy=False), action=QuestionSet.has_writed_in), Rule(condition_num=2, condition=person_entity + Star(Any(), greedy=False) + actor + Star(Any(), greedy=False), action=QuestionSet.has_acted_in), Rule(condition_num=2,
war_death = (W("战损") | W("死亡人数")) when = (W("哪个地方") | W("何时") | W("什么时候")) where = (W("在哪里") | W("哪儿") | W("何地") | W("何处") | W("在") + W("哪")) # TODO 问题模板/匹配规则 """ 1. 某个国家打赢了什么战争 2. 某场战争有哪些参战者 3. 某国参加了多少战争 4. 某场战争在何时发生 5. 某场战争的战损是多少 """ rules = [ Rule(condition_num=2, condition=combatant_entity + Star(Any(), greedy=False) + war + Star(Any(), greedy=False), action=QuestionSet.participate_war), Rule(condition_num=2, condition=(war_entity + Star(Any(), greedy=False) + combatant + Star(Any(), greedy=False)) | (combatant + Star(Any(), greedy=False) + war_entity + Star(Any(), greedy=False)), action=QuestionSet.combatant_participate), Rule(condition_num=3, condition=combatant_entity + Star(Any(), greedy=False) + several + Star(Any(), greedy=False) + (war | Star(Any(), greedy=False)), action=QuestionSet.count_participated_wars), Rule(condition_num=2, condition=war_entity + Star(Any(), greedy=False) + when, action=QuestionSet.war_take_place),