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) return label, "enum"
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 CapitalOfQuestion(QuestionTemplate): """ Regex for questions about the capital of a country. Ex: "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 WhoWroteQuestion(QuestionTemplate): """ Ex: "who wrote The Little Prince?" "who is the author of A Game Of Thrones?" """ regex = ((Lemmas("who write") + Book()) | (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 MovieReleaseDateQuestion(QuestionTemplate): """ Ex: "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) return release_date, "literal"
class EpisodeCountQuestion(QuestionTemplate): """ Ex: "How many episodes does Seinfeld have?" "Number of episodes of Seinfeld" """ regex = ((Lemmas("how many episode do") + TvShow() + Lemma("have")) | (Lemma("number") + Pos("IN") + Lemma("episode") + Pos("IN") + TvShow())) + \ Question(Pos(".")) def interpret(self, match): number_of_episodes = NumberOfEpisodesIn(match.tvshow) return number_of_episodes, "literal"
class PlotOfQuestion(QuestionTemplate): """ Ex: "what is Shame about?" "plot of Titanic" """ regex = ((Lemmas("what be") + Movie() + Lemma("about")) | \ (Question(Lemmas("what be the")) + Lemma("plot") + Pos("IN") + Movie())) + \ Question(Pos(".")) def interpret(self, match): definition = DefinitionOf(match.movie) return definition, "define"
class CapitalOfQuestion(QuestionTemplate): """ Regex for questions about the capital of a country. Ex: "What is the capital of Massachussets?" """ opening = Lemma("what") + Lemma("be") regex = Question(opening) + Question(Pos("DT")) + Lemma("capital") + Pos("IN") + \ Question(Pos("DT")) + PopulatedPlace() + Question(Pos(".")) def interpret(self, match): capital = CapitalOf(match.populatedplace) label = LabelOf(capital) return label, "enum"
class GenreQuestion(QuestionTemplate): """ Regex for questions about the genre of a band. Ex: "What is the music genre of Gorillaz?" "Music genre of Radiohead" """ optional_opening = Question(Pos("WP") + Lemma("be") + Question(Pos("DT"))) regex = optional_opening + Question(Lemma("music")) + Lemma("genre") + \ Pos("IN") + Band() + Question(Pos(".")) def interpret(self, match): genre = MusicGenreOf(match.band) label = LabelOf(genre) return label, "enum"
class MovieDurationQuestion(QuestionTemplate): """ Ex: "How long is Pulp Fiction" "What is the duration of The Thin Red Line?" "What is the runtime of Fight Club?" """ regex = ((Lemmas("how long be") + Movie()) | (Lemmas("what be") + Pos("DT") + (Lemma("runtime") | Lemma("duration")) + Pos("IN") + Movie())) + \ Question(Pos(".")) def interpret(self, match): duration = DurationOf(match.movie) return duration, ("literal", "{} minutes long")
class DirectorOfQuestion(QuestionTemplate): """ Ex: "Who is the director of Big Fish?" "who directed Pocahontas?" """ regex = ((Lemmas("who be") + Pos("DT") + Lemma("director") + Pos("IN") + Movie()) | (Lemma("who") + Lemma("direct") + Movie())) + \ Question(Pos(".")) def interpret(self, match): director = IsPerson() + DirectorOf(match.movie) director_name = NameOf(director) return director_name, "literal"
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) #incumbent = IncumbentOf(president) #label = LabelOf(incumbent) return president, "enum"
class ActorsOfQuestion(QuestionTemplate): """ Ex: "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 = NameOf(IsPerson() + StarsIn(match.movie)) return actor, "enum"
class BooksByAuthorQuestion(QuestionTemplate): """ Ex: "list books by George Orwell" "which books did Suzanne Collins wrote?" """ regex = (Question(Lemma("list")) + Lemmas("book by") + Author()) | \ ((Lemma("which") | Lemma("what")) + Lemmas("book do") + Author() + Lemma("write") + Question(Pos("."))) | \ (Question(Lemma("which")) + Lemma("book") + Question(Lemma("be")) + \ Lemma("write") + Pos("IN") + Author() + Question(Pos("."))) def interpret(self, match): book = IsBook() + HasAuthor(match.author) book_name = NameOf(book) return book_name, "enum"
class ListEntity(QuestionTemplate): """ Regex for questions like "List Microsoft software" """ entity = Group(Pos("NNP"), "entity") target = Group(Pos("NN") | Pos("NNS"), "target") regex = LISTOPEN + entity + target def interpret(self, match): entity = HasKeyword(match.entity.tokens) target_type = HasKeyword(match.target.lemmas) target = HasType(target_type) + IsRelatedTo(entity) label = LabelOf(target) return label, "enum"
class ShowsWithQuestion(QuestionTemplate): """ Ex: "List shows with Hugh Laurie" "In what shows does Jennifer Aniston appears?" "Shows with Matt LeBlanc" """ regex = (Lemmas("list show") + Pos("IN") + Actor()) | \ (Pos("IN") + (Lemma("what") | Lemma("which")) + Lemmas("show do") + Actor() + (Lemma("appear") | Lemma("work")) + Question(Pos("."))) | \ ((Lemma("show") | Lemma("shows")) + Pos("IN") + Actor()) def interpret(self, match): show = IsTvShow() + HasActor(match.actor) show_name = ShowNameOf(show) return show_name, "enum"
class AlbumsOfQuestion(QuestionTemplate): """ Ex: "List albums of Pink Floyd" "What albums did Pearl Jam record?" "Albums by Metallica" """ regex = (Question(Lemma("list")) + (Lemma("album") | Lemma("albums")) + \ Pos("IN") + Band()) | \ (Lemmas("what album do") + Band() + (Lemma("record") | Lemma("make")) + Question(Pos("."))) | \ (Lemma("list") + Band() + Lemma("album")) def interpret(self, match): album = IsAlbum() + ProducedBy(match.band) name = NameOf(album) return name, "enum"
class BandMembersQuestion(QuestionTemplate): """ Regex for questions about band member. Ex: "Radiohead members" "What are the members of Metallica?" """ regex1 = Band() + Lemma("member") regex2 = Lemma("member") + Pos("IN") + Band() regex3 = Pos("WP") + Lemma("be") + Pos("DT") + Lemma("member") + \ Pos("IN") + Band() regex = (regex1 | regex2 | regex3) + Question(Pos(".")) def interpret(self, match): member = IsMemberOf(match.band) label = LabelOf(member) return label, "enum"
class LanguageOfQuestion(QuestionTemplate): """ Regex for questions about the language spoken in a country. Ex: "What is the language of Argentina?" "what language is spoken in Argentina?" "which language is spoken in Argentina?" """ openings = (Lemma("what") + Token("is") + Pos("DT") + Question(Lemma("official")) + Lemma("language")) | \ ((Lemma("what") | Lemma("which")) + Lemma("language") + Token("is") + Lemma("speak")) regex = openings + Pos("IN") + Question(Pos("DT")) + Country() + \ Question(Pos(".")) def interpret(self, match): language = LanguageOf(match.country) return language, "enum"
class MoviesByDirectorQuestion(QuestionTemplate): """ Ex: "List movies directed by Quentin Tarantino. "movies directed by Martin Scorsese" "which movies did Mel Gibson directed" "Which movies are directed by Steven Speilberg" """ regex = (Question(Lemma("list")) + (Lemma("movie") | Lemma("film")) + Question(Lemma("direct")) + Lemma("by") + Director()) | \ (Lemma("which") + (Lemma("movie") | Lemma("film")) + Lemma("do") + Director() + Lemma("direct") + Question(Pos("."))) | \ (Lemma("which") + (Lemma("movie") | Lemma("film")) + Pos("VBP") + \ Lemma("direct") + Pos("IN") + Director() + Question(Pos("."))) def interpret(self, match): movie = IsMovie() + DirectedBy(match.director) movie_name = LabelOf(movie) return movie_name, "enum"
class ReleaseDateQuestion(QuestionTemplate): """ Ex: when was Friends release? """ regex = Lemmas("when be") + TvShow() + Lemma("release") + \ Question(Pos(".")) def interpret(self, match): release_date = ReleaseDateOf(match.tvshow) return release_date, "literal"
class Thing(Particle): regex = Question(Pos("JJ")) + ((Pos("NN") | Pos("NNP") | Pos("NNS")) |\ Pos("VBN")) + Question(Pos("NN") | Pos("NNP") | Pos("NNS")) def interpret(self, match): #print(match.words.tokens) tokens = match.words.tokens #tokens = tokens.split(" ")[1] iiit = re.compile("IIIT") tokens = iiit.sub("Indian Institute of Information Technology,", tokens) iit = re.compile("IIT") tokens = iit.sub("Indian Institute of Technology", tokens) return HasKeyword(tokens)
class ActedOnQuestion(QuestionTemplate): """ Ex: "List movies with Hugh Laurie" "Movies with Matt LeBlanc" "In what movies did Jennifer Aniston appear?" "Which movies did Mel Gibson starred?" "Movies starring Winona Ryder" """ acted_on = (Lemma("appear") | Lemma("act") | Lemma("star")) movie = (Lemma("movie") | Lemma("movies") | Lemma("film")) regex = (Question(Lemma("list")) + movie + Lemma("with") + Actor()) | \ (Question(Pos("IN")) + (Lemma("what") | Lemma("which")) + movie + Lemma("do") + Actor() + acted_on + Question(Pos("."))) | \ (Question(Pos("IN")) + Lemma("which") + movie + Lemma("do") + Actor() + acted_on) | \ (Question(Lemma("list")) + movie + Lemma("star") + Actor()) def interpret(self, match): movie = IsMovie() + HasActor(match.actor) movie_name = NameOf(movie) return movie_name, "enum"
class PopulationOfQuestion(QuestionTemplate): """ Regex for questions about the population of a country. Ex: "What is the population of China?" "How many people live in China?" """ openings = (Pos("WP") + Token("is") + Pos("DT") + Lemma("population") + Pos("IN")) | \ (Pos("WRB") + Lemma("many") + Lemma("people") + Token("live") + Pos("IN")) regex = openings + Question(Pos("DT")) + Country() + Question(Pos(".")) def interpret(self, match): population = PopulationOf(match.country) return population, "literal"
class WhereIsQuestion(QuestionTemplate): """ Ex: "where in the world is the Eiffel Tower" """ thing = Group(Plus(Pos("IN") | Pos("NP") | Pos("NNP") | Pos("NNPS")), "thing") regex = Lemma("where") + Question(Lemmas("in the world")) + Lemma("be") + \ Question(Pos("DT")) + thing + Question(Pos(".")) def interpret(self, match): thing = HasKeyword(match.thing.tokens) location = LocationOf(thing) location_name = LabelOf(location) return location_name, "enum"
class CastOfQuestion(QuestionTemplate): """ Ex: "What is the cast of Friends?" "Who works in Breaking Bad?" "List actors of Seinfeld" """ regex = (Question(Lemmas("what be") + Pos("DT")) + Lemma("cast") + Pos("IN") + TvShow() + Question(Pos("."))) | \ (Lemmas("who works") + Pos("IN") + TvShow() + Question(Pos("."))) | \ (Lemmas("list actor") + Pos("IN") + TvShow()) def interpret(self, match): actor = IsPerson() + StarsIn(match.tvshow) name = LabelOf(actor) return name, "enum"
class WhatTimeIs(QuestionTemplate): """ Regex for questions about the time Ex: "What time is it in Cordoba" """ nouns = Plus(Pos("NN") | Pos("NNS") | Pos("NNP") | Pos("NNPS")) place = Group(nouns, "place") openings = (Lemma("what") + ((Token("is") + Token("the") + Question(Lemma("current")) + Question(Lemma("local")) + Lemma("time")) | (Lemma("time") + Token("is") + Token("it")))) | \ Lemma("time") regex = openings + Pos("IN") + place + Question(Pos(".")) def interpret(self, match): place = HasKeyword(match.place.lemmas.title()) + IsPlace() utc_offset = UTCof(place) return utc_offset, "time"
class Movie(Particle): regex = Question(Pos("DT")) + nouns def interpret(self, match): name = match.words.tokens return IsMovie() + HasName(name)
class Country(Particle): regex = Plus(Pos("DT") | Pos("NN") | Pos("NNS") | Pos("NNP") | Pos("NNPS")) def interpret(self, match): name = match.words.tokens.title() return IsCountry() + HasKeyword(name)