def morph_character_parsing(self, text): """ Extracts the words from the text, performs morphological analysis on the words, and then parses the text character by character. """ lexRules = self.morphword_pairs_to_rules(self.get_all_words(text)) g = grammar.FeatureGrammar.fromstring(self.grammarText + lexRules) p = parse.FeatureEarleyChartParser(g) return p.parse(text)
def add_to_grammar(rules): ''' This function adds in new rules for the grammar. ''' global GRAMMARSTR global PARSER for rule in rules: GRAMMARSTR += '\n' + rule GRAMMAR = grammar.FeatureGrammar.fromstring(GRAMMARSTR) PARSER = parse.FeatureEarleyChartParser(GRAMMAR)
def gramatica(self, texto): print(texto) flag = True x = "" gramatica2 = grammar.FeatureGrammar.fromstring(""" % start S S[SEM=<?vp(?np)>] -> VP[SEM=?vp] NP[SEM=?np] S[SEM=<jugar(?np)>] -> NP[SEM=?np] VP[SEM=?v] -> V[SEM=?v] PREPO V[SEM=<\\x.jugar(x)>] -> "tirar" | "jugar" | "poner" PREPO -> "en" NP[SEM=?num] -> ARTICULO SUSTANTIVO NUM[SEM=?num] NP[SEM=?num] -> PREPO ARTICULO NUM[SEM=?num] NP[SEM=?num] -> ARTICULO NUM[SEM=?num] ARTICULO -> "el" | "la" SUSTANTIVO -> "casilla" | "posición" | "número" | "espacio" NUM[SEM=<1>] -> "1" | "uno" NUM[SEM=<2>] -> "2" | "dos" NUM[SEM=<3>] -> "3" | "tres" NUM[SEM=<4>] -> "4" | "cuatro" NUM[SEM=<5>] -> "5" | "cinco" NUM[SEM=<6>] -> "6" | "seis" NUM[SEM=<7>] -> "7" | "siete" NUM[SEM=<8>] -> "8" | "ocho" NUM[SEM=<9>] -> "9" | "nueve" """) while (flag): try: parser = parse.FeatureEarleyChartParser(gramatica2) tokens = texto.split() tree = parser.parse_one(tokens) semantica = tree.label()['SEM'] x = (str(semantica).split("("))[1].replace(")", "") flag = False except: self.ejecutarVoz( "No pude reconocer lo que dijiste, intenta de nuevo") texto = self.reconocerVoz() self.gramatica(texto) return x
def __init__(self): self.sql_grammar = """ % start S S[SEM=(?np + WHERE + ?vp)] -> NP[SEM=?np] VP[SEM=?vp] VP[SEM=(?v + ?pp)] -> IV[SEM=?v] PP[SEM=?pp] VP[SEM=(?v + ?ap)] -> IV[SEM=?v] AP[SEM=?ap] NP[SEM=(?det + ?n)] -> Det[SEM=?det] N[SEM=?n] PP[SEM=(?p + ?np)] -> P[SEM=?p] NP[SEM=?np] AP[SEM=?pp] -> A[SEM=?a] PP[SEM=?pp] NP[SEM='Country="greece"'] -> 'Greece' | 'greece' NP[SEM='Country="africa"'] -> 'africa' NP[SEM='Country="thailand"'] -> 'thailand' NP[SEM='Country="china"'] -> 'China' | 'china' Det[SEM='SELECT'] -> 'Which' | 'What' | 'which' | 'what' N[SEM='City FROM city_table'] -> 'cities' N[SEM='Population FROM city_table'] -> 'population' IV[SEM=''] -> 'are' IV[SEM=''] -> 'is' A[SEM=''] -> 'located' P[SEM=''] -> 'in' """ self.grammar = grammar.FeatureGrammar.fromstring(self.sql_grammar) self.parser = parse.FeatureEarleyChartParser(self.grammar) self.menu_display()
import nltk from nltk import grammar, parse from nltk.parse.generate import generate text = open("fbg2.txt", "r") grammar = grammar.FeatureGrammar.fromstring(text) tokens = 'Kim sees this cars'.split() parser = parse.FeatureEarleyChartParser(grammar) trees = parser.parse(tokens) for tree in trees: print(tree) for sentence in generate(grammar, n=20): print(' '.join(sentence))
def construct_parser(self): g = self.construct_feature_grammar() self.parser = parse.FeatureEarleyChartParser(g)
def program(percept): game, nplayer = percept grammar1 = grammar.FeatureGrammar.fromstring(""" %start S S[SEM=<vp(?np)>] -> VP[SEM=?np] NP[SEM=?vp] VP[SEM=<jugar>] -> "Jugar" | "jugar" NP[SEM=?v] -> CO[SEM=?v] CO[SEM=<\\x.coord(x)>] -> "1,1" | "1,2" | "1,3" | "2,1" | "2,2" | "2,3" | "3,1" | "3,2" | "3,3" """) if nplayer == 1: while True: r = sr.Recognizer() with sr.Microphone() as source: print("Di tu tirada con 'Jugar x,y'...") r.adjust_for_ambient_noise(source, duration=1) audio = r.listen(source,phrase_time_limit=5) estado = r.recognize_google(audio, language="es-MX") print(estado) tokens = estado.split() rd_parser = parse.FeatureEarleyChartParser(grammar1) error=0 try: for tree in rd_parser.parse(tokens): error="" except ValueError: error="Error, gramática no válida" print("Audio detectado: "+estado) if error=="": try: if estado == "jugar 1,1": move = '1' estado = '1' speak("El jugador tiró en la casilla"+estado) elif estado == "jugar 1,2": move = '2' estado = '2' speak("El jugador tiró en la casilla"+estado) elif estado == "jugar 1,3": move = '3' estado = '3' speak("El jugador tiró en la casilla"+estado) elif estado == "jugar 2,1": move = '4' estado = '4' speak("El jugador tiró en la casilla"+estado) elif estado == "jugar 2,2": move = '5' estado = '5' speak("El jugador tiró en la casilla"+estado) elif estado == "jugar 2,3": move = '6' estado = '6' speak("El jugador tiró en la casilla"+estado) elif estado == "jugar 3,1": move = '7' estado = '7' speak("El jugador tiró en la casilla"+estado) elif estado == "jugar 3,2": move = '8' estado = '8' speak("El jugador tiró en la casilla"+estado) elif estado == "jugar 3,3": move = '9' estado = '9' speak("El jugador tiró en la casilla"+estado) else: move = '0' estado = '0' speak("No es una jugada válida") return estado except sr.UnknownValueError: print("No se reconoce el audio") except sr.RequestError as e: print("No puede generar resultados del servicio de Google Speech Recognition; {0}".format(e)) else: print("El audio hablado no es válido en la gramática.") if estado in ('1','2','3','4','5','6','7','8','9'): move = estado else: print(estado+" no es válido, vuelve a decir tu jugada: ") r.adjust_for_ambient_noise(source, duration=1) audio = r.listen(source,phrase_time_limit=5) estado = r.recognize_google(audio, language="es-MX") move = estado return move break if nplayer == 2: if algorithm(game): move = algorithm(game) return move
JJ[SEM='Section="SHIFT DIFFERENTIAL"'] -> 'Shift_Differential' JJ[SEM='Section="SALARIES"'] -> 'Salaries' JJ[SEM='Section="SIGN LANGUAGE PREMIUM"'] -> 'Sign_Language' JJ[SEM='Section="COURT APPEARANCES"'] -> 'Court_Appearances' JJ[SEM='Section="CIVIC DUTY"'] -> 'Civic_Duty' JJ[SEM='Section="JURY SERVICE"'] -> 'Jury_Service' JJ[SEM='Section="MILITARY LEAVE"'] -> 'Military_Leave' JJ[SEM='Section="CALL BACK PAY"'] -> 'Call_Back' JJ[SEM='Section="DISTURBANCE CALLS"'] -> 'Disturbance_Calls' JJ[SEM='Section="ON-CALL/STANDBY COMPENSATION"'] -> 'On-Call/Standby' JJ[SEM='Section="TEMPORARY SUPERVISORY PAY"'] -> 'Temporary_Supervisory' JJ[SEM='Section="CIVILIAN SUPERVISORY DIFFERENTIAL"'] -> 'Civilian_Supervisory' JJ[SEM='Section="MILEAGE"'] -> 'Mileage' """ SQL_grammar = grammar.FeatureGrammar.fromstring(SQL_rule) SQL_parser = parse.FeatureEarleyChartParser(SQL_grammar) # simple application to allow user search bonus info based on conditions def app(): condition = '' section_names = ['Salaries','Overtime','Shift_Differential','Bilingual','Sign_Language','Court_Appearances','Civic_Duty','Jury_Service','Military_Leave','Call_Back','Disturbance_Calls','On-Call/Standby','Mileage','Temporary_Supervisory'] while condition not in section_names: condition = str(input('\n Salaries, Overtime, Shift_Differential, Bilingual, Sign_Language, Court_Appearances, Civic_Duty, Jury_Service, Military_Leave, Call_Back, Disturbance_Calls, On-Call/Standby, Mileage, Temporary_Supervisory, Civilian_Supervisory.\nPlease type in a correct section you want to search for bonus info: ''')) question = 'Which employees will receive what percentage/amount of bonus under ' + str(condition) + ' condition' # translate input question into SQL query with grammar structure query_tree = SQL_parser.parse(question.split()) for tree in query_tree: root = tree.label()['SEM'] SQL_query = list(root) query = ' '.join(str(w) for w in SQL_query if w)
S[SEM=<?vp(?np)>] -> VP[SEM=?vp] NP[SEM=?np] S[SEM=<jugar(?np)>] -> NP[SEM=?np] VP[SEM=?v] -> V[SEM=?v] PREPO V[SEM=<\\x.jugar(x)>] -> "tirar" | "jugar" | "poner" PREPO -> "en" NP[SEM=?num] -> ARTICULO SUSTANTIVO NUM[SEM=?num] NP[SEM=?num] -> PREPO ARTICULO NUM[SEM=?num] NP[SEM=?num] -> ARTICULO NUM[SEM=?num] ARTICULO -> "el" | "la" SUSTANTIVO -> "casilla" | "posición" | "número" | "espacio" NUM[SEM=<1>] -> "1" | "uno" NUM[SEM=<2>] -> "2" | "dos" NUM[SEM=<3>] -> "3" | "tres" NUM[SEM=<4>] -> "4" | "cuatro" NUM[SEM=<5>] -> "5" | "cinco" NUM[SEM=<6>] -> "6" | "seis" NUM[SEM=<7>] -> "7" | "siete" NUM[SEM=<8>] -> "8" | "ocho" NUM[SEM=<9>] -> "9" | "nueve" """) parser = parse.FeatureEarleyChartParser(gramatica2) text = "jugar en la casilla 9" tokens = text.split() trees = parser.parse(tokens) semantica = tree.label()['SEM']
DT[SEM=<a>] -> 'a' DT[SEM=<a>] -> 'an' IN[SEM=<with>] -> 'with' IN[SEM=<for>] -> 'for' IN[SEM=<about>] -> 'about' TO[SEM=<to>]-> 'to' DT[SEM=<the>] -> 'the' IN[SEM=<into>] -> 'into' CC[SEM=<\\x.(\\y.(x & y))>] -> 'and' """ #stuff that doesn't work yet """ NP[N=<?jj(?nn)>] -> JJ[SEM=?jj] NN[SEM=?nn] """ GRAMMAR = grammar.FeatureGrammar.fromstring(GRAMMARSTR) PARSER = parse.FeatureEarleyChartParser(GRAMMAR) def add_to_grammar(rules): ''' This function adds in new rules for the grammar. ''' global GRAMMARSTR global PARSER for rule in rules: GRAMMARSTR += '\n' + rule GRAMMAR = grammar.FeatureGrammar.fromstring(GRAMMARSTR) PARSER = parse.FeatureEarleyChartParser(GRAMMAR) def parse_sentence(sentence):
def upload(): try: # Read the Sinhala Grammar singram = grammar.FeatureGrammar.fromstring(sinhala) parser = parse.FeatureEarleyChartParser(singram) # strLine = "මිනිස්සු කුරුල්ලන් ලගින ගස කපති" # strLine = request.form["text"] strLine = request.json['InputText'] tokens = strLine.split() trees = parser.parse(tokens) chkStrgFirst = "Test" chkStrgFinal = "Test" sentenceSize = [] print(len(tokens)) if len(tokens) == 2: i = 0 for tree in trees: for node in tree: for nodenode in node: print(str(node)) if i == 0: chkStrgFirst = str(nodenode) print(str(nodenode) + '\n') i = i + 1 chkStrgFinal = str(nodenode) if len(tokens) == 3: i = 0 for tree in trees: for node in tree: for nodenode in node: if i == 0: chkStrgFirst = str(nodenode) print(str(nodenode) + '\n') i = i + 1 chkStrgFinal = str(nodenode) if len(tokens) == 4: i = 0 for tree in trees: for node in tree: for nodenode in node: if i == 0: chkStrgFirst = str(nodenode) print(str(nodenode) + '\n') i = i + 1 chkStrgFinal = str(nodenode) print(chkStrgFirst) print(chkStrgFinal) chkStrgFirstArry = chkStrgFirst.split("[") chkStrgFirst = chkStrgFirstArry[1] chkStrgFirstArry = chkStrgFirst.split("]") chkStrgFirst = chkStrgFirstArry[0] print(chkStrgFirst) print("++++++++++++++") chkStrgFinalArry = chkStrgFinal.split("[") chkStrgFinal = chkStrgFinalArry[1] chkStrgFinalArry = chkStrgFinal.split("]") chkStrgFinal = chkStrgFinalArry[0] print(chkStrgFinal) jstre = json.dumps(tree) jsting = json.loads(jstre) newstr = str(jsting) newstr02 = newstr.replace("[", "") newstr03 = newstr02.replace("]", "") print(newstr03) tense = "test" singular_plural = "test" gramr = "correct grammer" if "TENSE='Npast'" in chkStrgFinal: tense = "present" if "TENSE='past'" in chkStrgFinal: tense = "past" if "NUM='pl'" in chkStrgFirst and "NUM='pl'" in chkStrgFinal: singular_plural = "plural" if "NUM='sg'" in chkStrgFirst and "NUM='sg'" in chkStrgFinal: singular_plural = "singular" if tense == "test" or singular_plural == "test": gramr = "wrong grammer" #print(sentenceSize) jObj = {} jObj["gramr"] = gramr jObj["tense"] = tense jObj["singular_plural"] = singular_plural except: tense = "test" singular_plural = "test" gramr = "wrong grammer" jObj = {} jObj["gramr"] = gramr jObj["tense"] = tense jObj["singular_plural"] = singular_plural return Response(json.dumps(jObj), mimetype='application/json')
#manner ################## #VLT = volition #true #falls ################## from lang_helper import sinhala from nltk import grammar, parse # Read the Sinhala Grammar singram = grammar.FeatureGrammar.fromstring(sinhala) parser = parse.FeatureEarleyChartParser(singram) f_read = open('sentences.txt', encoding='utf-8') f_write = open('parsedsents.txt', encoding='utf-8', mode='w') for line in f_read: tokens = line.split() trees = parser.parse(tokens) for tree in trees: f_write.write(str(tree) + '\n') print(str(tree) + '\n') ''' Sample Output (S[] (NP[CASE='F1', DEF=?TF, GEN='NE', NUM='pl']