def was_born_and_mentions_place(Subject, Object): """ Ex: Nasser Sharify was born in Tehran, Iran, in 1925. """ place = Plus(Pos("NNP") + Question(Token(","))) return Subject + Token("was born") + Pos("IN") + place + Pos( "IN") + Object + Question(Pos("."))
class PlotOfQuestion(QuestionTemplate): """ Ex: "what is (the movie/film) Shame about(?)" "what is the plot/story of Shame(?)" "plot of Titanic" """ regex1 = Lemmas("what be") + \ Question(Literal("the") + (Lemma("movie") | Lemma("film"))) + \ Movie() + Question(Lemma("about")) + Question(Pos(".")) regex = regex1 def interpret(self, match): match.movie.add_data(u"dbpprop:counter", u"?counter") plot = PlotOf(match.movie) #plot.add_data(u"dbpprop:counter",u"?counter") old_value = replace_prop(plot, u"dbpprop:name", u"?name") #replace_prop(plot,u"dbpprop:plot", u"?plot") extra = MyExtra('FILTER regex(?name, "%s", "i").' % old_value) plot.add_data(extra, u"") #plot.add_data(OrderBy("DESC(xsd:integer(?counter))"),u"") #plot.head = "*" return plot, "list"
class ActorsOfQuestion(QuestionTemplate): """ Ex: "who are the actors of Titanic?" "who acted in Alien?" "who starred in The Predator?" "Actors of Fight Club" """ prob_threshold = 0.95 tables = ["actors", "actresses"] examples = [ "who are the actors of Titanic?" "who acted in Alien?", "who starred in Depredator?", "Actors of Fight Club" ] regex = (Lemma("who") + Question(Lemma("be") + Pos("DT")) + (Lemma("act") | Lemma("actor") | Lemma("star")) + Pos("IN") + Movie() + Question(Pos("."))) | \ ((Lemma("actors") | Lemma("actor")) + Pos("IN") + Movie()) def interpret(self, match): actor = IsPerson() movie_name = ''.join(match.movie.nodes[0][1][1].split('"')[:-1]) actor.nodes = [u' title like "%' + movie_name + '%"'] actor.tables = ["actors", "actresses"] actor.head = u"name,surname,title" return actor, ("enum", "ActorsOfQuestion")
class WhoIsActorQuestion(QuestionTemplate): """ Ex: "Who is Michelle Pfeiffer?" """ prob_threshold = 0.95 examples = ["Who is Michelle Pfeiffer?"] tables = ["actors", "actresses"] regex = ( ( Lemmas("who be") + Question(Pos("DT")) + Actor() \ ) ) \ + Question(Pos(".")) def interpret(self, match): definition = DefinitionOf(match.actor) #print("Match ",match.movie) actor_name = ''.join(match.actor.nodes[0][1][1].split('"')[:-1]) print("Actor name ", actor_name) definition.nodes = [ u' concat(name, " ", surname) like concat("%", replace("' + actor_name + '", " ", "%"), "%")' ] # what to extract definition.head = u" distinct name, surname, title" definition.tables = ["actors", "actresses"] #print("definition ",definition) return definition, ("define", "WhoIsActorQuestion")
class MovieReleaseDateQuestion(QuestionTemplate): """ Ex: "When was The Red Thin Line released?" "Release date of The Empire Strikes Back" """ prob_threshold = 0.95 tables = ["movies"] examples = [ "When was The Red Thin Line released?", "Release date of The Empire Strikes Back" ] regex = ((Lemmas("when be") + Movie() + Lemma("release")) | (Lemma("release") + Question(Lemma("date")) + Pos("IN") + Movie())) + \ Question(Pos(".")) def interpret(self, match): release_date = ReleaseDateOf(match.movie) release_date.tables = ["movies"] movie_name = ''.join(match.movie.nodes[0][1][1].split('"')[:-1]) release_date.nodes = [u' title like "' + movie_name + '"'] release_date.head = u"year" return release_date, ("literal", "MovieReleaseDateQuestion")
class DirectorOfQuestion(QuestionTemplate): """ Ex: "Who is the director of Big Fish?" # ok "who directed Pocahontas?" # ok director of Sparta? # ok tell me who directed Nemo # ok tell me who is the director of Gran Torino # ok, grammatically wrong """ who = Lemma("who") director = Lemma("director") maybe_tellme = Question(Lemmas("tell me")) regex1 = maybe_tellme + Question(who + Lemma("be") + Pos("DT") + director) + Pos("IN") + Movie() regex2 = maybe_tellme + who + Lemma("direct") + Movie() regex3 = director + Pos("IN") + Movie() regex = (regex1 | regex2 | regex3) \ + maybe_dot_or_qmark def interpret(self, match): director = IsDirector() + DirectorOf(match.movie) director_name = NameOf(director) return director_name, "literal"
class MovieReleaseDateQuestion(QuestionTemplate): """ Ex: "Show me release date of Pocahontas" # ok show me release date of Pocahontas # ok release date of Bambi # ok release date of Bambi? # ok the release date of Bambi? # ok "Tell me the release date of Bambi" # ok when was Big Fish released? # ok """ tellme = Question(Lemmas("show me") | Lemmas("tell me")) maybe_the = Question(Lemma("the")) release_date = Lemmas("release date") regex1 = tellme + maybe_the + release_date + Pos("IN") + Movie() regex2 = Lemma("when") + Lemma("be") + Movie() + Lemma("release") regex = (regex1 | regex2) \ + maybe_dot_or_qmark def interpret(self, match): release_date = ReleaseDateOf(match.movie) print(release_date) return release_date, "literal"
class ActedOnQuestion(QuestionTemplate): """ Ex: "List movies with Hugh Laurie" # ok "Movies with Matt LeBlanc" # not ok: yes, the problem is the big "M" (lemmatizer should do it already or not??) "movies with Matt LeBlanc" # ok "In what movies did Jennifer Aniston appear?" # ok "Which movies did Mel Gibson starred?" # ok "Movies starring Winona Ryder" # not ok movies where Matt LeBlanc starred # ok movies in which Matt LeBlanc played in # ok list movies in which Matt LeBlanc appears # ok """ maybe_list = Question( Lemma("list") | Lemma("elencate") | Lemmas("spell out") + Lemmas("write down") + Lemmas("tell me the")) acted_on = (Lemma("appear") | Lemma("act") | Lemma("star") | Lemmas("play in")) regex1 = maybe_list + movie_lemmas + Lemma("with") + Actor() regex2 = maybe_list + Question( Pos("IN")) + (Lemma("what") | Lemma("which")) + movie_lemmas + ( Lemma("do") | Lemma("have")) + Actor() + acted_on regex3 = maybe_list + movie_lemmas + ( Lemma("where") | Lemmas("in which")) + Actor() + acted_on regex = (regex1 | regex2 | regex3) \ + maybe_dot_or_qmark def interpret(self, match): movie = IsMovie() + HasActor(match.actor) movie_name = NameOf(movie) print(movie_name.__dict__) return movie_name, "enum"
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
class WhatIs(QuestionTemplate): target = Question(Pos("DT")) + Group(Pos("NN"), "target") regex = Lemma("what") + Lemma("be") + target + Question(Pos(".")) def interpret(self, match): thing = match.target.tokens target = HasKeyword(thing) definition = DefinitionOf(target) return definition, "define"
class IsCapitalOfQuestion(QuestionTemplate): regex = Token("is") + Capital() + Pos("DT") + Lemma("capital") + Pos("IN") + \ Question(Pos("DT")) + Country() + Question(Pos(".")) def interpret(self, match): capital = CapitalOf(match.country) label = LabelOf(capital) return label, "enum"
class WhatIsLocation(QuestionTemplate): regex = (Lemma("what") + Lemma("is") + (Lemma("location") | Lemma("place")) + Location() + Question(Pos("."))) | ((Lemma("location") | Lemma("place")) + Location() + Question(Pos("."))) def interpret(self, match): _location, i, j = match.location return _location + HasId(), ReturnValue(i, j)
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
class WhatIsOraError(QuestionTemplate): """ Regex for questions like What is ora-00942? -- ok What is the meaning of ora-00942? -- ok what is meant by ora-00942? -- ok What means by ora-00942? -- ok (?What is) definition of ora-00942? -- ok target; is the key token in the question interpret; create the query link to get specific data, it build the query meta data contains some information for xml creation """ # target = Question(Pos("DT")) + Question((Lemma('mean')) + Pos("IN")) +\ # Group(error_tokens, "target") target = Group(error_tokens, "target") regex = (Question(Lemma("what")) + Question(Lemma("be")) + Question(Pos("DT")) + Question(Lemma("meaning") | Lemma("mean")) + Question(Pos("IN")) + target + Question(Pos("."))) | \ (Question(Lemma("what")) + Question(Lemma("be"))) + Question(Pos("DT")) +\ Lemma("definition") + Pos("IN") + target + Question(Pos(".")) def interpret(self, match): thing = match.target.tokens target = IsError() + ErrorIdOf(thing) meta = "errorNlg", "WHAT" return target, meta
class CreatorOfQuestion(QuestionTemplate): """ Ex: "Who is the creator of Breaking Bad?" """ regex = Question(Lemmas("who be") + Pos("DT")) + \ Lemma("creator") + Pos("IN") + TvShow() + Question(Pos(".")) def interpret(self, match): creator = CreatorOf(match.tvshow) label = LabelOf(creator)
class WhoAreChildrenOfQuestion(QuestionTemplate): """ EX: "Who are the children's of Bill Gates" """ regex = Lemma("who") + Lemma("be") + Pos("DT") + Lemma("child") + Pos("IN") + Person() + \ Question(Pos(".")) | Lemma("child") + Pos("IN") + Person() + Question(Pos(".")) def interpret(self, match): _person, i, j = match.person child_name = HasChild(_person) return child_name, ReturnValue(i, j)
class WhereIsQuestion(QuestionTemplate): """ Ex: "where in the world is the Eiffel Tower" """ regex = Lemma("where") + Question(Lemmas("in the world")) + Lemma("be") + \ Question(Pos("DT")) + Thing() + Question(Pos(".")) def interpret(self, match): location = LocationOf(match.thing) location_name = NameOf(location) return location_name
class WhoWroteQuestion(QuestionTemplate): """ Ex: "who is the author of A Game Of Thrones?" """ regex = (Question(Lemmas("who be") + Pos("DT")) + \ Lemma("author") + Pos("IN") + Book() + \ Question(Pos("."))) def interpret(self, match): author = NameOf(IsPerson() + AuthorOf(match.book)) return author, "literal"
class CreatorOfRegex(RegexTemplate): """ Ex: "Who is the creator of Breaking Bad?" """ regex = Question(Lemmas("who be") + Pos("DT")) + \ Lemma("creator") + Pos("IN") + TvShow() + Question(Pos(".")) def semantics(self, match): creator = CreatorOf(match.tvshow) label = LabelOf(creator) return label, "enum"
class PresidentOfQuestion(QuestionTemplate): """ Regex for questions about the president of a country. Ex: "Who is the president of Argentina?" """ regex = Pos("WP") + Token("is") + Question(Pos("DT")) + \ Lemma("president") + Pos("IN") + Country() + Question(Pos(".")) def interpret(self, match): definition = ToGenerate(match.country) return definition, "presidentofquestion"
class PresidentOfQuestion(QuestionTemplate): """ Ex: "list presidents of Argentina?" """ regex = Question(Lemma("list")) + Lemma("president") + Pos("IN") + \ Country() + Question(Pos(".")) def interpret(self, match): president = IsPresident() + PresidentOf(match.country) name = NameOf(OfficeHolderOf(president)) return name
class WhoAreParentsOfQuestion(QuestionTemplate): """ EX: "Who are the parents of Bill Gates" """ regex = Lemma("who") + Lemma("be") + Pos("DT") + Lemma("parent") + Pos("IN") + Person() + \ Question(Pos(".")) | Lemma("parent") + Pos("IN") + Person() + Question(Pos(".")) def interpret(self, match): person_name, i, j = match.person parents_name = HasParents(person_name) return parents_name, ReturnValue(i, j)
class FoundationQuestion(QuestionTemplate): """ Regex for questions about the creation of a band. Ex: "When was Pink Floyd founded?" "When was Korn formed?" """ regex = Question(Pos("WRB") + Lemma("be")) + Band() + \ (Lemma("form") | Lemma("found")) + Question(Pos(".")) def interpret(self, match): active_years = ActiveYears(match.band) return active_years, "literal"
class WhatIs(QuestionTemplate): """ Regex for questions like "What is a blowtorch Ex: "What is a car" "What is Seinfield?" """ regex = Lemma("what") + Lemma("be") + Question(Pos("DT")) + \ Thing() + Question(Pos(".")) def interpret(self, match): label = DefinitionOf(match.thing) return label, "define"
class ClassPrerequisiteQuestion(QuestionTemplate): """ Ex: "What are the prerequisites for cmpe 273?" """ prerequisite = Group(Lemma("prerequisite"), "prerequisite") regex = Lemmas("what be") + Question( Pos("DT")) + prerequisite + Pos("IN") + Course() + Question(Pos(".")) def interpret(self, match): answer = "The prerequisites for %s are %s" prerequisite = IsClassRelated() + match.course + HasFields('prerequisites'.decode('utf-8')) \ + HasAnswer(answer.decode('utf-8')) return prerequisite
class ChefOfQuestion(QuestionTemplate): """ Regex for questions asking about the head chef of a restaurant Ex: "who is the chef of The Ivy?" "who is the head chef of 21 Club restaurant?" """ regex = (Lemmas("who be") + Lemma("the") + Question(Lemma("head")) + Lemma("chef") + Pos("IN") + \ Restaurant() + Question(Lemma("restaurant")) + Question(Pos("."))) def interpret(self, match): ChefName = ChefOf(match.restaurant) return ChefName, "enum"
class CapitalOfQuestion(QuestionTemplate): """ "What is the capital of Bolivia?" """ opening = Lemma("what") + Token("is") regex = opening + Pos("DT") + Lemma("capital") + Pos("IN") + \ Question(Pos("DT")) + Country() + Question(Pos(".")) def interpret(self, match): capital = CapitalOf(match.country) label = LabelOf(capital) return label, "enum"
class PresidentOfQuestion(QuestionTemplate): """ Ex: " " """ regex = Question(Lemma("list")) + Lemma("president") + Pos("IN") + \ Country() + Question(Pos(".")) def interpret(self, match): _country, i, j = match.country president = IsPresident() + PresidentOf(_country) name = NameOf(OfficeHolderOf(president)) return name, ReturnValue(i, j)
class ListAllLanguagesQuestion(QuestionTemplate): """ Regex for questions about listing all languages in dbpedia Ex: "list all languages?" "list all languages in dbpedia?" """ regex = (Question(Lemma("list")) + Lemma("all") + Lemma("language") + Question(Pos("."))) | \ (Question(Lemma("list")) + Lemma("all") + Lemma("language") + Pos("IN") + Lemma("dbpedia") + Question(Pos("."))) def interpret(self, match): LanguageNames = IsLanguage() return LabelOf(LanguageNames), "enum"
class PresidentOfQuestion(QuestionTemplate): """ Regex for questions about the president of a country. Ex: "Who is the president of Argentina?" """ regex = Pos("WP") + Token("is") + Question(Pos("DT")) + \ Lemma("president") + Pos("IN") + Country() + Question(Pos(".")) def interpret(self, match): president = PresidentOf(match.country) label = LabelOf(president) return label, "enum"