class ListProjects(QuestionTemplate): """ Regex for questions like "List Projects 2013" """ entity = Group(Pos("NN") | Pos("NNS"), "entity") prep = Group(Pos("IN"), "prep") target = Group(Pos("CD"), "target") regex = LISTOPEN + entity + prep + target # print "test" # print regex def interpret(self, match): #entity = HasKeyword(match.entity.tokens) #print "--"+match.target.lemmas #target_type = HasKeyword(match.target.lemmas) year = match.target.lemmas target_t = FiscalYear(year) #target = HasType(target_type) + IsRelatedTo(entity) #target = HasType(target_type) #target_t = target_t + "^^<http://www.w3.org/2001/XMLSchema#integer>" label = TitleOf(target_t) #print "match3" return label, "enum"
class WhereIsFile(QuestionTemplate): """ Regex for questions like Where is Listener.ora (?locate)? -- ok What is the (?file) location of Listener.ora ((?file)? -- ok How to find Listener.ora? -- ok """ target = Group(file_tokens, "target_file_name") + Group( extension_tokens, "target_file_extension") regex = (Lemmas("how to") + Lemma("find") + target + Question(Lemma("file"))) | \ (Lemma("where") + Lemma("be") + target + Question(Lemma("file")) + Question(Lemma("locate"))) | \ (Pos("WP") + Lemma("be") + Question(Pos("DT")) + Question(Lemma("file")) + Lemma("location") + Pos("IN") + target + Question(Lemma("file"))) def interpret(self, match): name = match.target_file_name.tokens extension = match.target_file_extension.tokens target = IsFile() + FileOf(name + "." + extension) meta = "fileLocationNlg", "WHERE", str(name + "." + extension) return FileLocation(target), meta
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 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 SugarOfQuestion(QuestionTemplate): """ Regex for questions about the sugar in species Ex: "How much sugar in an Apple?" "How much sugar an Apple have?" "Do Apple have sugar?" """ regex = Lemmas("how much") + Lemma("sugar") + Pos("IN") + Pos("DT") + Group(Pos("NNP"), 'species') + Question(Pos(".")) | \ (Lemmas("how much") + Lemma("sugar") + Pos("DT") + Group(Pos("NNP"), 'species') + Lemma("have") + Question(Pos("."))) | \ (Lemma("do") + Group(Pos("NNP"), 'species') + Lemma("have") + Lemma("sugar") + Question(Pos("."))) def interpret(self, match): name = match.species.tokens species = IsSpecies() + HasKeyword(name) suger = SugarOf(species) return suger, "enum"
class FatOfQuestion(QuestionTemplate): """ Regex for questions about the fat in species Ex: "How much fat in an apple?" "How much fat an Apple have?" "Do Apple have fat?" """ regex = Lemmas("how much") + Lemma("fat") + Pos("IN") + Pos("DT") + Group(Pos("NNP"), 'species') + Question(Pos(".")) | \ (Lemmas("how much") + Lemma("fat") + Pos("DT") + Group(Pos("NNP"), 'species') + Lemma("have") + Question(Pos("."))) | \ (Lemma("do") + Group(Pos("NNP"), 'species') + Lemma("have") + Lemma("fat") + Question(Pos("."))) def interpret(self, match): name = match.species.tokens species = IsSpecies() + HasKeyword(name) fat = FatOf(species) return fat, "enum"
class CarbsOfQuestion(QuestionTemplate): """ Regex for questions about the carbs in species Ex: "How much carbs in an apple?" "How much carbs an Apple have?" "Do Apple have carbs?" """ regex = Lemmas("how much") + Lemma("carbs") + Pos("IN") + Pos("DT") + Group(Pos("NNP"), 'species') + Question(Pos(".")) | \ (Lemmas("how much") + Lemma("carbs") + Pos("DT") + Group(Pos("NNP"), 'species') + Lemma("have") + Question(Pos("."))) | \ (Lemma("do") + Group(Pos("NNP"), 'species') + Lemma("have") + Lemma("carbs") + Question(Pos("."))) def interpret(self, match): name = match.species.tokens species = IsSpecies() + HasKeyword(name) carbs = CarbsOf(species) return carbs, "enum"
class ProteinOfQuestion(QuestionTemplate): """ Regex for questions about the protein in species Ex: "How much protein in an apple?" "How much protein an Apple have?" "Do Apple have protein?" """ regex = Lemmas("how much") + Lemma("protein") + Pos("IN") + Pos("DT") + Group(Pos("NNP"), 'species') + Question(Pos(".")) | \ (Lemmas("how much") + Lemma("protein") + Pos("DT") + Group(Pos("NNP"), 'species') + Lemma("have") + Question(Pos("."))) | \ (Lemma("do") + Group(Pos("NNP"), 'species') + Lemma("have") + Lemma("protein") + Question(Pos("."))) def interpret(self, match): name = match.species.tokens species = IsSpecies() + HasKeyword(name) protein = ProteinOf(species) return protein, "enum"
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"
def parse_element_into_books(html_elements): # Based on https://github.com/machinalis/refo/blob/master/examples/xml_reader.py is_header = lambda elem: elem.get('class').startswith('bookMain') is_highlight = lambda elem: elem.get('class').startswith('highlightRow') regex = Group(Predicate(is_header) + Plus(Predicate(is_highlight)), 'book') groups = [ html_elements[g['book'][0]:g['book'][1]] for g in finditer(regex, html_elements) ] return [Book(group) for group in groups]
class WhyError(QuestionTemplate): """ Regex for questions like Why ora-00942? -- ok (?What is the) reason for ora-00942? -- ok """ target = Group(error_tokens, "target") regex = (Group(Pos("WRB"), "wh") | Question(Group(Lemma("what"), "wh")) + Question(Lemma("be")) + Question(Pos("DT")) + Lemma("reason") + Pos("IN")) + target def interpret(self, match): thing = match.target.tokens target = IsError() + ErrorIdOf(thing) meta = "whyError", "WHY" return HasErrorCause(target), meta
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 # print "test1" def interpret(self, match): entity = FirstName(match.entity.tokens) entity = AppId(entity) entity = ProjAppId(entity) #target_type = HasKeyword(match.target.lemmas) #print target_type #target = IsRelatedTo(entity) label = TitleOf(entity) return label, "enum"
class WhereIsQuestion(QuestionTemplate): """ Ex: "where in the world is the Eiffel Tower" projects funded by national ins cnter """ thing = Group(Plus(Pos("NP") | Pos("NNP") | Pos("NNPS")), "thing") entity = Group(Pos("NN") | Pos("NNS"), "entity") verb = Group(Plus(Pos("VBD") | Pos("IN")), "verb") regex = entity + verb + thing def interpret(self, match): print "checked" thing = AgencyName(match.thing.tokens) entity = AgencyAppId(thing) entity = ProjAppId(entity) #target_type = HasKeyword(match.target.lemmas) #print target_type #target = IsRelatedTo(entity) label = TitleOf(entity) return label, "enum"
class IndustriesQuestion(QuestionTemplate): """ Regex for questions about which industries a company belongs to. Ex: Which industries does Google belong to? """ regex = Pos("WDT") + Lemma("industry") + Lemma("do") + Group( Pos("NNP"), 'company') + Lemma("belong") + Lemma("to") + Pos(".") def interpret(self, match): name = match.company.tokens company = IsCompany() + HasKeyword(name) industries = IndustryOf(company) return industries, "enum"
class RevenueOfQuestion(QuestionTemplate): """ Regex for questions about how much revenue a company makes. Ex: How much revenue does Google make? """ regex = Pos("WRB") + Lemma("much") + Lemma("revenue") + Lemma( "do") + Group(Pos("NNP"), 'company') + Lemma("make") + Pos(".") def interpret(self, match): name = match.company.tokens company = IsCompany() + HasKeyword(name) revenue = RevenueOf(company) return revenue, "enum"
class EquityOfQuestion(QuestionTemplate): """ Regex for questions about how much equity a company holds. Ex: How much equity does Google hold? """ regex = Pos("WRB") + Lemma("much") + Lemma("equity") + Lemma("do") + Group( Pos("NNP"), 'company') + Lemma("hold") + Pos(".") def interpret(self, match): name = match.company.tokens company = IsCompany() + HasKeyword(name) equity = EquityOf(company) return equity, "enum"
class WhatIs(QuestionTemplate): """ Regex for questions like "What is ..." Ex: "What is a car" """ 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 = IsDefinedIn(target) return definition
class WhatIsFile(QuestionTemplate): """ Regex for questions like What is Listener.ora (?file)? -- ok What is the meaning of Listener.ora (?file)? -- ok """ target = Group(file_tokens, "target_file_name") + Group( extension_tokens, "target_file_extension") regex = Lemma("what") + Lemma("be") + Question(Pos("DT") + Lemma("meaning") + Pos("IN")) + target + \ Question(Pos(".")) def interpret(self, match): name = match.target_file_name.tokens extension = match.target_file_extension.tokens target = IsFile() + FileOf(name + "." + extension) meta = "fileNlg", "What" return target, meta
class AssetsQuestion(QuestionTemplate): """ Regex for questions about how much assets a company has. Ex: How much assets does Google have? """ regex = Pos("WRB") + Plus(Lemma("much") | Lemma("many")) + Plus( Lemma("asset") | Lemma("assets")) + Lemma("do") + Group( Pos("NNP"), 'company') + Lemma("have") + Pos(".") def interpret(self, match): name = match.company.tokens company = IsCompany() + HasKeyword(name) assets = AssetsOf(company) return assets, "enum"
class DeveloperQuestion(QuestionTemplate): """ Regex for questions about which products a company developed. Ex: Which products were developed by Google? """ regex = Pos("WDT") + Lemma("product") + Lemma("be") + Lemma( "develop") + Lemma("by") + Group(Pos("NNP"), 'company') + Pos(".") def interpret(self, match): name = match.company.tokens company = IsCompany() + HasKeyword(name) products = DevelopedBy(company) labels = NameOf(products) return labels, "enum"
class WhatIs(RegexTemplate): """ Regex for questions like "What is ..." Ex: "What is a car" """ target = Group(Question(Pos("JJ")) + Pos("NN"), "target") regex = Lemma("what") + Lemma("be") + Question(Pos("DT")) + \ target + Question(Pos(".")) def semantics(self, match): target = handle_noun_phrase(match.target) label = DefinitionOf(target) return label, "define"
class OwnedByQuestion(QuestionTemplate): """ Regex for questions about which companies a company owns. Ex: Which companies are owned by Google? """ regex = Pos("WDT") + Lemma("company") + Lemma("be") + Lemma("own") + Pos( "IN") + Group(Pos("NNP"), 'company') + Pos(".") def interpret(self, match): name = match.company.tokens company = IsCompany() + HasKeyword(name) ownedCompanies = OwnedBy(company) label = NameOf(ownedCompanies) return label, "enum"
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 WhatTimeIs(RegexTemplate): """ Regex for questions about the time Ex: "What time is in Cordoba" """ nouns = Plus(Pos("NN") | Pos("NNS") | Pos("NNP") | Pos("NNPS")) place = Group(nouns, "place") openings = (Lemma("what") + Lemma("time") + Token("is")) | Lemma("time") regex = openings + Pos("IN") + place + Question(Pos(".")) def semantics(self, match): place = HasKeyword(match.place.lemmas.title()) + IsPlace() utc_offset = UTCof(place) return utc_offset, "time"
class WhatIs(QuestionTemplate): """ Regex for questions like "What is ..." Ex: "What is a car" """ target = Question(Pos("DT")) + Group(Pos("NN"), "target") regex = Lemma("what") + Lemma("be") + target + Question(Pos(".")) def interpret(self, match): """ Define the interpret method by using the dsl module to convert this into a sql query. """ thing = match.target.tokens target = HasKeyword(thing) definition = IsDefinedIn(target) return definition
class ListRestaurantInCityQuestion(QuestionTemplate): """ Regex for questions about listing all restaurants in a city Ex: "list all restaurants in New York City?" "Restaurants in London?" """ city = Group(Plus(Pos("NN") | Pos("NNS") | Pos("NNP") | Pos("NNPS")), "city") regex = (Question(Lemma("list")) + Question(Lemma("all")) + (Lemma("restaurant") | Lemma("restaurants")) + Pos("IN") + city + Question(Pos("."))) def interpret(self, match): city = HasKeyword(match.city.tokens) restaurants = IsRestaurant() + CityOf(city) return NameOf(restaurants), "enum"
class IngredientOfQuestion(QuestionTemplate): """ Regex for questions about ingredient that uses a species Ex: "what type of food uses Apple as ingredient?" "what kind of food uses Apple as ingredient?" "what food uses Apple as ingredient?" "list food uses Apple as ingredient?" "food uses Apple as ingredient" """ regex = (Lemmas("what type") | Lemmas("what kind")) + Pos("IN") + Lemma("food") + Lemma("use") + Group(Pos("NNP"), 'species') + Pos("IN") + Lemma("ingredient") + Question(Pos(".")) | \ (Question((Lemma("list")) | (Lemma("what")))+ Lemma("food") + Lemma("use") + Group(Pos("NNP"), 'species') + Pos("IN") + Lemma("ingredient") + Question(Pos("."))) def interpret(self, match): name = match.species.tokens species = IsSpecies() + HasKeyword(name) Ingredient = IngredientOf(species) label = NameOf(Ingredient) return label, "enum"
class WhatisHiredate(QuestionTemplate): ''' Regex for questions like: [What is <target> hiredate?] ex: What is Heidi hiredate? # The questions are with respect to the mock data table ''' regex = Lemma("what") + Lemma("be") + Group( Pos("NNP"), "target") + Token("hiredate") + Question(Pos(".")) print "Qustion template: What <is / are> <person> hiredate?" def interpret(self, match): employee_name = match.target.tokens # reset the HasKeyword relation HasKeyword.relation = "proatt:firstname" emp_id = HasKeyword(employee_name) solution = Hiredate(emp_id) return solution
class HowToFixError(QuestionTemplate): """ Regex for questions like How to fix ora-00942? -- ok (?proper) way of fixing ora-00942? -- ok (?proper) steps to fixing ora-00942 -- ok """ target = Group(error_tokens, "target") regex = Lemmas("how to") + Lemma("fix") + target + Question(Pos(".")) | \ Question(Lemma("proper")) + Lemma("way") + Pos("IN") + Lemma("fix") + target + Question(Pos(".")) | \ Question(Lemma("proper")) + Lemma("steps") + Lemma("to") + Lemma("fix") + target + Question(Pos(".")) def interpret(self, match): thing = match.target.tokens target = IsError() + ErrorIdOf(thing) tip = HasErrorTip(target) meta = "errorStepNlg", "HOW" return tip, meta
class WhoIs(QuestionTemplate): ''' Regex for questions like: [Who is <target>?] ex: Who is Heidi? # The questions are with respect to the mock data table ''' target = Group(Pos("NNP"), "target") regex = Lemma("who") + Lemma("be") + target + Question(Pos(".")) print "Question template: Who <is / are> <person>?" # method to interpret this regular expression pattern: def interpret(self, match): thing = match.target.tokens # reset the HasKeyword relation HasKeyword.relation = "proatt:firstname" pro_id = HasKeyword(thing) # find the position of those matched people solution = Position(pro_id) # return the so constructed pattern: return solution