Ejemplo n.º 1
0
def execute_query(query, yes_no_grammar, wh_grammar, database):
    query = query.translate(string.maketrans("", ""), string.punctuation)
    query_list = [i for i in query.lower().split()]
    yes_no = set([
        "was", "did", "is", "does", "were", "could", "do", "are", "have",
        "had", "should"
    ])
    if (query_list[0] in yes_no):
        cp = load_parser(yes_no_grammar, trace=3)
    else:
        cp = load_parser(wh_grammar)  #, trace=3)
    trees = list(cp.parse(query_list))
    answer = trees[0].label()['SEM']
    answer = [s for s in answer if s]
    q = ' '.join(answer)
    print(q)

    results = []
    rows = chat80.sql_query(database, q)
    for r in rows:
        results.append(r[0])
        print(r[0])

    if (len(results) == 0 and query_list[0] in yes_no):
        print "No"
Ejemplo n.º 2
0
def example6(s1):
    ep = load_parser('635/rattachement2.cfg')
    for tree in ep.parse(s1.split()):
        print(tree)


#example6('il a_vu l ours sur la montagne pendant la nuit')
Ejemplo n.º 3
0
def grammaticalFeatures():
    kim = {'CAT': 'NP', 'ORTH': 'Kim', 'REF': 'k'}
    chase = {'CAT': 'V', 'ORTH': 'chased', 'REL': 'chase'}

    chase['AGT'] = 'sbj'
    chase['PAT'] = 'obj'

    sent = "Kim chased Lee"
    tokens = sent.split()
    lee = {'CAT': 'NP', 'ORTH': 'Lee', 'REF': 'l'}

    def lex2fs(word):
        for fs in [kim, lee, chase]:
            if fs['ORTH'] == word:
                return fs

    subj, verb, obj = lex2fs(tokens[0]), lex2fs(tokens[1]), lex2fs(tokens[2])
    verb['AGT'] = subj['REF']  # agent of 'chase' is Kim
    verb['PAT'] = obj['REF']  # patient of 'chase' is Lee
    for k in ['ORTH', 'REL', 'AGT', 'PAT']: # check featstruct of 'chase' ...
        print "%-5s => %s" % (k, verb[k])

    print "=============== Using Attributes and Constraints ==============="
    nltk.data.show_cfg('grammars/book_grammars/feat0.fcfg')

    tokens = 'Kim likes children'.split()
    from nltk import load_parser
    cp = load_parser('grammars/book_grammars/feat0.fcfg', trace=2)
    trees = cp.parse(tokens)
    for tree in trees:
        print tree
Ejemplo n.º 4
0
def GrammaticalFeatures():

    kim = {'CAT': 'NP', 'ORTH': 'Kim', 'REF': 'k'}
    chase = {'CAT': 'V', 'ORTH': 'chased', 'REL': 'chase'}

    chase['AGT'] = 'sbj'
    chase['PAT'] = 'obj'

    sent = "Kim chased Lee"
    tokens = sent.split()

    lee = {'CAT': 'NP', 'ORTH': 'Lee', 'REF': 'l'}

    def lex2fs(word):
        for fs in [kim, lee, chase]:
            if fs['ORTH'] == word:
                return fs

    subj, verb, obj = lex2fs(tokens[0]), lex2fs(tokens[1]), lex2fs(tokens[2])
    verb['AGT'] = subj['REF']
    verb['PAT'] = obj['REF']

    for k in ['ORTH', 'REL', 'AGT', 'PAT']:
        print "%-5s => %s" % (k, verb[k])

    surprise = {'CAT': 'V', 'ORTH': 'surprised', 'REL': 'surprise',
            'SRC': 'sbj', 'EXP': 'obj'}

    nltk.data.show_cfg('grammars/book_grammars/feat0.fcfg')


    tokens = 'Kim likes children'.split()
    from nltk import load_parser
    cp = load_parser('grammars/book_grammars/feat0.fcfg', trace=2)
    trees = cp.nbest_parse(tokens)
Ejemplo n.º 5
0
def parse_sentence(parser_grammer, sent, trace=0):
    try:
        # 1- if it's a sentence else throw exceptions
        if is_question(sent) < 1:
            return Exception(
                "Only one questions is allowed, and should be asked using ",
                supported_questions_str_list, ", questions types")
        # 2- senitize it
        sent = senitize_question(sent)
        # More Senitize by removing stop words
        words = word_tokenize(sent)
        sent = ' '.join([
            w for w in words if not w in stop_words
            or w in supported_questions_str_list or w in sen_words
        ])

        # 3- parse it
        cp = load_parser(parser_grammer, trace)
        trees = list(cp.parse(sent.split()))

        answer = trees[0].label()['SEM']
        answer = [s for s in answer if s]

        q = ' '.join(answer)
    except IndexError as e:
        raise e
    return q  # string return
	def test_loadExampleFeatureGrammar(self):
		# arrange
		# act
		cp = nltk.load_parser("file:../docs/example_grammar.fcfg")

		# assert
		self.assertTrue(cp != None)
Ejemplo n.º 7
0
 def __init__(self, grammar, drt_parser):
     assert isinstance(grammar, str) and grammar.endswith('.fcfg'), \
                         "%s is not a grammar name" % grammar
     self.drt_parser = drt_parser()
     self.presupp_parser = PresuppDrtParser()
     self.logic_parser = LogicParser()
     self.parser = load_parser(grammar, logic_parser=self.drt_parser) 
Ejemplo n.º 8
0
def verbose(sentence):
    tokens = sentence.split()
    parser = load_parser('./grammar.fcfg', trace=1)
    trees = parser.parse(tokens)
    for tree in trees:
        fol = (tree.label()['SEM'])
        print("\nResult:\n" + str(fol))
        break
Ejemplo n.º 9
0
def main():
    test_data = [
        'I am happy'.split(),
        'she is happy'.split(),
        'you are happy'.split(),
        'you is happy'.split(),
    ]

    print('grammer (8)')
    cp8 = load_parser('grammer8.fcfg')
    for tokens in test_data:
        parse_and_print(cp8, tokens)

    print('grammer (20)')
    cp20 = load_parser('grammer20.fcfg', trace=1)
    for tokens in test_data:
        parse_and_print(cp20, tokens)
Ejemplo n.º 10
0
def main():
    test_data = [
        'I am happy'.split(),
        'she is happy'.split(),
        'you are happy'.split(),
        'you is happy'.split(),
    ]

    print('grammer (8)')
    cp8 = load_parser('grammer8.fcfg')
    for tokens in test_data:
        parse_and_print(cp8, tokens)

    print('grammer (20)')
    cp20 = load_parser('grammer20.fcfg', trace=1)
    for tokens in test_data:
        parse_and_print(cp20, tokens)
Ejemplo n.º 11
0
def convert(sentence):
    #print(sentence)
    tokens = sentence.split()
    parser = load_parser('./grammar.fcfg')
    trees = parser.parse(tokens)
    for tree in trees:
        fol = (tree.label()['SEM'])
        #print(ans)
        fol = str(fol)
        break
    print("%" + fol)
    try:
        fol
    except:
        print("Unsupported sentence")
        return

    fol = fol.replace('.', ' ')
    fol = fol.replace('(', ' ( ')
    fol = fol.replace(')', ' ) ')
    fol = fol.replace(',', ' , ')
    fol = fol.replace('-', ' - ')
    fol = fol.replace('- >', '->')
    fol = fol.split()
    #print(fol)

    print(sentence)
    print("$$")

    for i in range(len(fol)):
        if fol[i] == "all":
            print("\\forall", end=' ')

        elif fol[i] == "exists":
            print("\\exists", end=' ')

        elif isVariable(i, fol):
            print(replaceNum(i, fol), end=' ')

        elif fol[i] == '-':
            print("\\neg", end=' ')

        elif fol[i] == '&':
            print("\\wedge", end=' ')

        elif fol[i] == '->':
            print("\\Rightarrow", end=' ')

        elif fol[i] == '(' or fol[i] == ')' or fol[i] == ',':
            print(fol[i], end=' ')

        else:
            print("\\text{", end=' ')
            print(fol[i], end=' ')
            print('}', end=' ')

    print("\n$$")
    print('\\newline')
Ejemplo n.º 12
0
def parse(question):
    cp = nltk.load_parser('grammar.fcfg',cache=False)
    parsed = list(cp.parse(question.lower().split()))

    if len(parsed) < 1:
        return None

    tree = list(parsed)[0]
    return "".join(tree.label()['SEM']), "".join(tree.label()['QT'])
Ejemplo n.º 13
0
def sentence_parsing():
#  tokens = "who do you claim that you like".split()
#  tokens = "you claim that you like cats".split()
  tokens = "rarely do you sing".split()
  from nltk import load_parser
  cp = load_parser("grammars/book_grammars/feat1.fcfg")
  for tree in cp.nbest_parse(tokens):
    print tree
    tree.draw()
Ejemplo n.º 14
0
def semantic(parseTrees, para):
    print "\n"
    print "Step 4: Semantic Analysis\n"
    cp = load_parser('grammars/book_grammars/sql0.fcfg')
    trees = list(cp.parse(para[:len(para) - 1].split()))
    answer = trees[0].label()['SEM']
    answer = [s for s in answer if s]
    q = ' '.join(answer)
    return q
Ejemplo n.º 15
0
def sentence_parsing():
    #  tokens = "who do you claim that you like".split()
    #  tokens = "you claim that you like cats".split()
    tokens = "rarely do you sing".split()
    from nltk import load_parser
    cp = load_parser("grammars/book_grammars/feat1.fcfg")
    for tree in cp.parse(tokens):
        print tree
        tree.draw()
Ejemplo n.º 16
0
 def validateGrammar(self, speakerText):
     try:
         sem = -1
         tokens = speakerText.split()
         cp = load_parser('semRecomendadorApp.fcfg', trace=0)
         tree = cp.parse_one(tokens)
         if tree is not None:
             sem = tree.label()['sem']
         return sem
     except ValueError:
         return sem
	def test_semanticsOfEnglishFifth(self):
		# arrange
		parser = nltk.load_parser('simple-sem.fcfg', trace=0)
		sentence = 'Angus gives a bone to every dog'
		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		self.assertTrue(len(trees) == 1)
		print trees[0].node["SEM"]
Ejemplo n.º 18
0
def english_to_sql():
  nltk.data.show_cfg("grammars/book_grammars/sql0.fcfg")
  from nltk import load_parser
  cp = load_parser("grammars/book_grammars/sql0.fcfg", trace=3)
  query = "What cities are located in China"
  trees = cp.nbest_parse(query.split())
  answer = trees[0].node['SEM']
  q = " ".join(answer)
  print q
  from nltk.sem import chat80
  rows = chat80.sql_query('corpora/city_database/city.db', q)
  for r in rows:
    print r[0],
Ejemplo n.º 19
0
def english_to_sql():
    nltk.data.show_cfg("grammars/book_grammars/sql0.fcfg")
    from nltk import load_parser
    cp = load_parser("grammars/book_grammars/sql0.fcfg", trace=3)
    query = "What cities are located in China"
    trees = cp.nbest_parse(query.split())
    answer = trees[0].node['SEM']
    q = " ".join(answer)
    print q
    from nltk.sem import chat80
    rows = chat80.sql_query('corpora/city_database/city.db', q)
    for r in rows:
        print r[0],
Ejemplo n.º 20
0
 def __init__(self, grammar_url_s: str, print: bool = False, draw: bool = False, save: bool = False):
     """
     constructor
     :param grammar_url_s: grammar file URL
     :param print: whether print the tree on console
     :param draw: whether draw the parse tree on console
     :param save: whether save the parse tree on drive
     """
     self.cp_s = load_parser(grammar_url_s, trace=0, parser=FeatureEarleyChartParser)
     self.print = print
     self.draw = draw
     self.save = save
     self.tree_no = 1
	def test_semanticsSentences_all_students_eat_or_drink(self):
		# arrange
		parser = nltk.load_parser('file:../docs/grammar.fcfg', trace=0)
		sentence = 'all students eat or drink'
		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		expectedResult = '(all x.(student(x) & exists z9.(eat(e3) & eater(e3,z9))) | all x.(student(x) & exists z10.(drink(e4) & drinker(e4,z10))))'
		actualResult = str(trees[0].node["SEM"])
		self.assertTrue(expectedResult == actualResult, actualResult)
def main():
    # const variables
    sem = "SEM"
    sentence = "I had a delicious lunch"

    # create the parseResult builder
    parser = nltk.load_parser("file:grammar.fcfg", trace=0)

    # read in the example sentences
    tokenizedSent = nltk.word_tokenize(sentence)
    trees = parser.nbest_parse(tokenizedSent)

    if len(trees) > 0:
        print trees[0].node[sem].simplify()
	def test_semanticsSentences_john_or_mary_eats(self):
		# arrange
		parser = nltk.load_parser('file:../docs/grammar.fcfg', trace=0)
		sentence = 'John or Mary eats'

		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		expectedResult = '(exists x.(eat(e) & eater(e,x)) | exists x.(eat(e) & eater(e,x)))'
		actualResult = variableNormalizer(str(trees[0].node["SEM"]))
		self.assertTrue(expectedResult == actualResult, actualResult)
Ejemplo n.º 24
0
def main(argv):
    if len(argv) == 1:
        parser = load_parser('grammar.fcfg')
        sentence = argv[0]
        try:
            sentence = sentence.lower()
            parsed = parser.parse(sentence.split())
            print(next(parsed))
            print("Sentence parsed succesfully!")
        except Exception:
            print("Couldn't parse \"{}\"".format(sentence))
    else:
        print(data.load('grammar.fcfg'))
        print("usage: python parsing.py \"this is your captain speaking\"")
	def test_semanticsSentences_john_eats_a_sandwich(self):
		# arrange
		parser = nltk.load_parser('file:../docs/grammar.fcfg', trace=0)
		sentence = 'John eats a sandwich'
		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		self.assertTrue(len(trees) == 1)
		expectedResult = 'exists {z}.(sandwich({z}) & eat(john,{z}))'
		actualResult = variableNormalizer(str(trees[0].node["SEM"]))
		self.assertTrue(expectedResult == actualResult, actualResult)
Ejemplo n.º 26
0
 def __init__(self,
              grammar_url: str,
              pprint: bool = False,
              save: bool = False):
     """
     constructor
     :param grammar_url: grammar file URL
     """
     self.cp = load_parser(grammar_url,
                           trace=0,
                           parser=FeatureEarleyChartParser)
     self.print = pprint
     self.save = save
     self.tree_no = 1
	def test_semanticsSentences_no_student_eats_a_bagel(self):
		# arrange
		parser = nltk.load_parser('file:../docs/grammar.fcfg', trace=0)
		sentence = 'no student eats a bagel'

		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		expectedResult = '-all x.(student(x) & exists {z}.(bagel({z}) & eat(x,{z})))'
		actualResult = variableNormalizer(str(trees[0].node["SEM"]))

		self.assertTrue(expectedResult == actualResult, actualResult)
Ejemplo n.º 28
0
 def __init__(self):
     self.gateway = JavaGateway()
     self.italian_lexicon = self.gateway.jvm.simplenlg.lexicon.italian.ITXMLLexicon(
     )
     self.italian_factory = self.gateway.jvm.simplenlg.framework.NLGFactory(
         self.italian_lexicon)
     self.realiser = self.gateway.jvm.simplenlg.realiser.Realiser()
     self.word_translator = WordTanslator(language_from=Languages.ENGLISH,
                                          language_to=Languages.ITALIAN)
     self.features = self.gateway.jvm.simplenlg.features
     self.Feature = self.gateway.jvm.simplenlg.features.Feature
     self.Tense = self.gateway.jvm.simplenlg.features.Tense
     self.Gender = self.gateway.jvm.simplenlg.features.Gender
     self.reasoner = Reasoner()
     self.parser = load_parser('simple-sem.fcfg', trace=0)
	def test_semanticsSentences_a_person_does_not_eat(self):
		# arrange
		parser = nltk.load_parser('file:../docs/grammar.fcfg', trace=0)
		sentence = 'a person does not eat'

		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		expectedResult = 'exists x.(person(x) & -exists {z}.(eat(e) & eater(e,{z})))'
		actualResult = variableNormalizer(str(trees[0].node["SEM"]))

		self.assertTrue(expectedResult == actualResult, actualResult)
	def test_semanticsSentences_every_student_eats_a_sandwich_or_drinks_a_soda(self):
		# arrange
		parser = nltk.load_parser('file:../docs/grammar.fcfg', trace=0)
		sentence = 'every student eats a sandwich or drinks a soda'

		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		expectedResult = '(all x.(student(x) & exists {z}.(sandwich({z}) & eat(x,{z}))) | all x.(student(x) & exists {z}.(soda({z}) & drink(x,{z}))))'
		actualResult = variableNormalizer(str(trees[0].node["SEM"]))
		
		self.assertTrue(expectedResult == actualResult, actualResult)
	def test_semanticsSentences_a_student_writes_an_essay_or_eats(self):
		# arrange
		parser = nltk.load_parser('file:../docs/grammar.fcfg', trace=0)
		sentence = 'a student writes an essay or eats'

		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		expectedResult = '(exists x.(student(x) & exists {z}.(essay({z}) & write(x,{z}))) | exists x.(student(x) & exists {z}.(eat(e) & eater(e,{z}))))'
		actualResult = variableNormalizer(str(trees[0].node["SEM"]))
		
		self.assertTrue(expectedResult == actualResult, actualResult)
	def test_semanticsSentences_jack_does_not_eat_or_drink(self):
		# arrange
		parser = nltk.load_parser('file:../docs/grammar.fcfg', trace=0)
		sentence = 'Jack does not eat or drink'

		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		expectedResult = '(-exists x.(eat(e) & eater(e,x)) & -exists x.(drink(e) & drinker(e,x)))'
		actualResult = variableNormalizer(str(trees[0].node["SEM"]))
		
		self.assertTrue(expectedResult == actualResult, actualResult)
	def test_semanticsSentences_john_eats_every_sandwich_or_bagel(self):
		# arrange
		parser = nltk.load_parser('file:../docs/grammar.fcfg', trace=0)
		sentence = 'John eats every sandwich or bagel'

		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		expectedResult = '(all {z}.(sandwich({z}) & eat(john,{z})) | all {z}.(bagel({z}) & eat(john,{z})))'
		actualResult = variableNormalizer(str(trees[0].node["SEM"]))
		
		self.assertTrue(expectedResult == actualResult, actualResult)
Ejemplo n.º 34
0
def template_test():
    del ret_value[:]
    namee = ""
    if request.form.get('searchbox') is "":
        return render_template('index.html', doodle=doodle_value)
    else:
        query = request.form.get('searchbox')
        query = str(query).lower()
        cp = load_parser('sub.fcfg')

        try:
            trees = list(cp.parse(query.split()))
            answer = trees[0].label()['SEM']
            answer = [s for s in answer if s]

            q = ' '.join(answer)
            print('question : ', q)

            jso = json.loads('{' + q + '}')

            client = pymongo.MongoClient()
            db = client.test
            data = db.capital.find(jso)
            # print(data[0]["Capital"])
            namee = data[0]["Capital"]
            #ret_value.append(namee)
            for i in lst:
                print(i)

                z = similar(namee, i.lower())
                print(z)
                if (z > 0.5):
                    ret_value.append(i)

        except ValueError as e:

            for i in lst:
                print(i)

                z = similar(query, i.lower())
                print(z)
                if (z > 0.7):
                    ret_value.append(i)
            print(ret_value)

    return render_template('index.html',
                           results=ret_value,
                           doodle=doodle_value)
Ejemplo n.º 35
0
    def get_tree(self, query):

        original_tokens = query.split()
        pos_tags = nltk.pos_tag(original_tokens)

        # lemmatize verbs and replace some words by placeholders
        lemmatizer = WordNetLemmatizer()
        simplified_tokens = []
        for i in range(len(original_tokens)):
            t = original_tokens[i]
            pos = pos_tags[i][1]
            if pos.startswith('VB'):
                simplified_tokens.append('#V#')
            elif t.isdigit():
                simplified_tokens.append('#NUM#')
            elif pos in ('NN', 'NNS'):
                simplified_tokens.append('#NN#')
            elif pos in ('JJS', 'JJ'):
                simplified_tokens.append('#JJ#')
            else:
                simplified_tokens.append('#' + pos + '#')

        print(pos_tags)
        print(simplified_tokens)

        # The grammar uses those '#*#' placeholders
        cp = load_parser('hal.fcfg')
        trees = cp.parse(simplified_tokens)
        for tree in trees:
            # Put back the lemmas and numbers in the tree leaves
            i = 0
            for leafPos in tree.treepositions('leaves'):
                if simplified_tokens[i] in ('#V#'):
                    #verbs -> base form
                    tree[leafPos] = lemmatizer.lemmatize(
                        original_tokens[i], 'v')
                elif simplified_tokens[i] in ('#NN#'):
                    tree[leafPos] = lemmatizer.lemmatize(original_tokens[i])
                elif simplified_tokens[i] == '#JJ#':
                    tree[leafPos] = lemmatizer.lemmatize(
                        original_tokens[i], 'a')
                elif simplified_tokens[i] in ('#NUM#', '#PDT#', '#JJR#',
                                              '#CC#'):
                    tree[leafPos] = original_tokens[i]
                i = i + 1
            return tree

        return None
	def test_semanticsOfEnglishFirst(self):
		# arrange
		parser = nltk.load_parser('file:../docs/simple-sem.fcfg', trace=0)
		sentence = 'Angus gives a bone to every dog'
		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		self.assertTrue(len(trees) == 1)
		expectedResult = 'all {z}.(dog({z}) -> exists {z}.(bone({z}) & give(angus,{z},{z})))'
		
		#print expectedResult
		#print trees[0].node["SEM"]
		self.assertTrue(expectedResult == variableNormalizer(str(trees[0].node["SEM"])))
	def test_semanticsSentences_john_eats(self):
		# arrange
		parser = nltk.load_parser('file:../docs/grammar.fcfg', trace=0)
		sentence = 'John eats'
		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		# what I want...
		# eats(john)
		self.assertTrue(len(trees) == 1)
		expectedResult = 'exists x.(eat(e3) & eater(e3,x))'
		actualResult = str(trees[0].node["SEM"])
		self.assertTrue(actualResult == expectedResult, actualResult)
	def test_semanticsSentences_all_students_eat(self):
		# arrange
		parser = nltk.load_parser('file:../docs/grammar.fcfg', trace=0)
		sentence = 'all students eat'
		tokens = sentence.split()
		
		# act
		trees = parser.nbest_parse(tokens)

		# assert
		# what I want...
		# eats(john)
		self.assertTrue(len(trees) == 1)
		expectedResult = 'all x.(student(x) & exists z7.(eat(e3) & eater(e3,z7)))'
		actualResult = str(trees[0].node["SEM"])
		self.assertTrue(expectedResult == actualResult, actualResult)
Ejemplo n.º 39
0
def main():
    parser = nltk.load_parser(GRAMMAR)
    counter = 0
    c=0
    with open(CORPUS) as F:
        for line in F:
            sent = line.split()
            if sent:
                trees = list(parser.parse(sent))
                print(counter)
                counter += 1
                print('{0}:  {1}\n'.format(len(trees)," ".join(sent)))
                if(draw_trees):
                    nltk.draw.tree.draw_trees(*trees)
                for tree in trees:
                   print(tree)
                   print('\n')
Ejemplo n.º 40
0
def parse_sentence(sentence, trace=0):
    """
    Using the chart parser from NLTK, parse a given sentence and return an RRG parse tree.

    Input: Sentence, trace (if 0, return only the parse tree, otherwise return intermediate steps)
    Output: Parse tree showing the various constituents of a sentence.
    """

    hline = 30 * '='
    print("\n{0}\n{1}\n{0}".format(hline, sentence))

    tokens = sentence.lower().split()

    cp = nltk.load_parser("faroese.fcfg", trace=trace)

    for tree in cp.parse(tokens):
        print(tree)
Ejemplo n.º 41
0
def demo_parse(string):

    #tokenise the input string
    tokens = string.split()

    #set trace=2 if you want to see what the parser is doing, step-by-step..
    # - N.B. NLTK caches grammars after they're loaded, unless told not to.
    #   I've told it not to, so that the grammar can be changed during a session.
    cp = load_parser(
        'file://home/cshome/b/bmillar/343/lab12/feature_grammar.fcfg',
        trace=0,
        cache=False)

    #run the chart parser on the tokens
    for tree in cp.parse(tokens):
        print(tree)
        tree.draw()
Ejemplo n.º 42
0
def demo_parse(string):

    #tokenise the input string
    tokens = string.split()

    #set trace=2 if you want to see what the parser is doing, step-by-step..
    # - N.B. NLTK caches grammars after they're loaded, unless told not to.
    #   I've told it not to, so that the grammar can be changed during a session.
    cp = load_parser(
        'file:///Users/zw/Documents/Otago/COSC343_Artificial_Intielligence/tutorial_12/feature_grammar.fcfg',
        trace=0,
        cache=False)

    #run the chart parser on the tokens
    for tree in cp.parse(tokens):
        print(tree)
        tree.draw()
Ejemplo n.º 43
0
def parseInput(text):
	#Line below removes any ? marks since my grammar can't deal with those.
	#Also, it's much easier to remove question marks in code than to
	#create a grammar that understands them!
	text = filter(lambda c: c not in "?", text)
	if text == 'Goodbye':
		#conn.close()  --not used yet, was used in earlier builds but now I need to call this later if at all
		exit() #Exits the system
	else:
		grammar1 = load_parser('file:C:\Users\Hobo\Desktop\FinalYearProj/projGrammar.fcfg')
		try:
			query = text
			trees = grammar1.nbest_parse(query.split())
			answer = trees[0].node['SEM']
			q = ' '.join(answer)
			print q
			getData(q)
		except:
			print 'parseInput exception thrown'
			parseAgain('I do not understand that, sorry!')
Ejemplo n.º 44
0
def load_grammar(grammar_file, trace=2, cache=False):
    """ loads a grammar parser from the given grammar file """

    # get grammar extension to pass to temp file name
    ext = os.path.splitext(grammar_file)[-1]

    # make temp file from ref parsed files and pass it load_parse
    temp_file, temp_file_path = tempfile.mkstemp(suffix=ext,text=True)

    temp_file = open(temp_file_path, "w")
    temp_file.write(load_file(grammar_file))
    temp_file.close()

    file = GRAMMAR_URL % {'url': os.path.join(ROOT, temp_file_path)}
    parser = load_parser(file, trace=trace, cache=cache)

    # cheekily remove temp file
    os.unlink(temp_file_path)

    return parser
Ejemplo n.º 45
0
    def __init__(self, pairs, reflections={}):
        """
        Initialize the chatbot.  Pairs is a list of patterns and responses.  Each
        pattern is a regular expression matching the user's statement or question,
        e.g. r'I like (.*)'.  For each such pattern a list of possible responses
        is given, e.g. ['Why do you like %1', 'Did you ever dislike %1'].  Material
        which is matched by parenthesized sections of the patterns (e.g. .*) is mapped to
        the numbered positions in the responses, e.g. %1.

        @type pairs: C{list} of C{tuple}
        @param pairs: The patterns and responses
        @type reflections: C{dict}
        @param reflections: A mapping between first and second person expressions
        @rtype: C{None}
        """

        self._pairs = [(re.compile(x, re.IGNORECASE), y) for (x, y) in pairs]
        self._reflections = reflections
        self._interactive = True
        self._parser = load_parser('grammars/restaurant.fcfg')
        self.message = ''
def GrammaticalFeatures():

    kim = {'CAT': 'NP', 'ORTH': 'Kim', 'REF': 'k'}
    chase = {'CAT': 'V', 'ORTH': 'chased', 'REL': 'chase'}

    chase['AGT'] = 'sbj'
    chase['PAT'] = 'obj'

    sent = "Kim chased Lee"
    tokens = sent.split()

    lee = {'CAT': 'NP', 'ORTH': 'Lee', 'REF': 'l'}

    def lex2fs(word):
        for fs in [kim, lee, chase]:
            if fs['ORTH'] == word:
                return fs

    subj, verb, obj = lex2fs(tokens[0]), lex2fs(tokens[1]), lex2fs(tokens[2])
    verb['AGT'] = subj['REF']
    verb['PAT'] = obj['REF']

    for k in ['ORTH', 'REL', 'AGT', 'PAT']:
        print "%-5s => %s" % (k, verb[k])

    surprise = {
        'CAT': 'V',
        'ORTH': 'surprised',
        'REL': 'surprise',
        'SRC': 'sbj',
        'EXP': 'obj'
    }

    nltk.data.show_cfg('grammars/book_grammars/feat0.fcfg')

    tokens = 'Kim likes children'.split()
    from nltk import load_parser
    cp = load_parser('grammars/book_grammars/feat0.fcfg', trace=2)
    trees = cp.nbest_parse(tokens)
Ejemplo n.º 47
0
def askUser(param):
	asklist = []
	asklist = param #bad idea, what happens if the user asks another Q? the list grows, not gonna work, overwrite
	#asklist[0] = row.DisplayScore
	#asklist[1] = row.DisplayName
	#asklist[2] = row.ID
	#asklist[3] = row.PID
	response = enterbox(image="SQLCA.gif", msg='I think you are trying to get to the ' + asklist[1] + ' display.\nAm I right in thinking so?', title='Sqelca', default='', strip=True, root=None)
	
	response = filter(lambda c: c not in "?", response)
	#print 'askUser() >> user said: ' + response
	if response == 'Goodbye':
		exit() #Exits the system
	else:
		#print 'askUser() >> Parsing in AskUser at the moment...'
		grammar1 = load_parser('file:C:\Users\Hobo\Desktop\FinalYearProj\projGrammar.fcfg')
		try:
			query = response
			trees = grammar1.nbest_parse(query.split())
			answer = trees[0].node['SEM']
			q = ' '.join(answer)
			
			if q == 'Rejection':
				print 'rejection area reached'
				#idInt = asklist[2]
				updateScoreDec(asklist[0], asklist[2])
				getNextBest(asklist)

			else:
				#q == 'Affirmation':
				idInt = asklist[2]
				conn = pyodbc.connect('DRIVER={SQL Server}; SERVER=HOBO-PC; DATABASE=DisplayDB') #Connection to the database
				cursor = conn.cursor() #defines a new cursor
				cursor.execute("SELECT Description FROM KnowledgeBase WHERE ID = ?", idInt)
				rows = cursor.fetchone()
				updateScoreInc(asklist[0], asklist[2])
				parseAgain(rows)
		except:
			parseAgain('I do not understand that, sorry!')
Ejemplo n.º 48
0
    def evalSemantics(self, text):
        sems = ""
        resp = []

        cp = load_parser('src/semantics/semantics.fcfg', trace=0)
        tokens = text.split()

        try:
            for tree in cp.parse(tokens):
                sems = str(tree.label()['sem'])

            print(sems)

            verb = sems.split("(")[0]
            nouns = sems.split("(")[1].replace(")", "").split(",")
            resp.append(verb)

            for n in nouns:
                resp.append(n)

            return resp
        except:
            return "NOSEMANTICS"
def main():
	# const variables
	sem = "SEM"

	# arg variables
	grammarFile = sys.argv[1]
	sentenceFile = sys.argv[2]

	# create the parseResult builder
	parser = nltk.load_parser('file:' + grammarFile, trace=0)

	# read in the example sentences
	sentenceFileStream = open(sentenceFile)
	sentence = sentenceFileStream.readline()

	# print out each sentence
	while sentence:
		tokenizedSent = nltk.word_tokenize(sentence)
		trees = parser.nbest_parse(tokenizedSent)

		if len(trees) > 0:
			print trees[0].node[sem].simplify()
			
		sentence = sentenceFileStream.readline()
Ejemplo n.º 50
0
since category N has a grammartical feature called NUM,
the productions above can be replaced by NUM feature:
    Det[NUM=sg] -> 'this'
    Det[NUM=pl] -> 'these'
    N[NUM=sg] -> 'dog'
    N[NUM=pl] -> 'dogs'
    V[NUM=sg] -> 'runs'
    V[NUM=pl] -> 'run'
It seems like not help too much, but we can add variables over feature values
and use variables to state constraints:
    S -> NP[NUM=?n] VP[NUM=?n]
    NP[NUM=?n] -> Det[NUM=?n] N[NUM=?n]
    VP[NUM=?n] -> V[NUM=?n]

?n as a variable over values of NUM, it can be instantiated either to sg or pl, within a given production.



"""

import nltk
# nltk.download('book_grammars')
nltk.data.show_cfg('grammars/book_grammars/feat0.fcfg')
print('-------------------------------------------------')

tokens = 'Kim likes children'.split()
from nltk import load_parser
cp = load_parser('grammars/book_grammars/feat0.fcfg', trace=2)
for tree in cp.parse(tokens):
    print(tree)
Ejemplo n.º 51
0
from nltk import load_parser
feature_cfg = load_parser('grammar_017.fcfg', trace=2)
nlquery = 'drama about'

trees = list(feature_cfg.parse(nlquery.split()))  # Put all tuples into list
print("trees")
print(trees)
print("")
top_tree_semantics = trees[0].label()['SEM']  # get first list entry with semantics
top_tree_semantics = [s for s in top_tree_semantics if s]  # first SEM entry from tuple to list
print("top tree semantics")
print(top_tree_semantics)
print("")
sqlquery = ''
try:
    sqlquery = ' '.join(top_tree_semantics)
except TypeError:
    for items in top_tree_semantics:
        for item in items:
            sqlquery += item
            sqlquery += ' '

print("sql query")
print(sqlquery)
Ejemplo n.º 52
0
import nltk
from nltk import load_parser
fs1 = nltk.FeatStruct(TENSE = 'past',NUM = 'sg')
print fs1

fs2 = nltk.FeatStruct(POS='N', AGR=fs1)
print fs2

cp = load_parser('grammars/book_grammars/sql0.fcfg')

query = 'What cities are located in China'

tokens = query.split()

for tree in cp.parse(tokens):
    print tree
# Programa base

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from nltk import load_parser
cp = load_parser('gramatica_base.fcfg')
infile = open('materialpracticasintaxis/textos.txt', encoding='utf-8')
# Mostramos por pantalla lo que leemos desde el fichero
# Si pinta es verdader pinta el arbol
pinta = False
with open('resutado.txt', 'w') as f:
    f.write('')
for i, line in enumerate(infile):
    print(line)
    with open('resutado.txt', 'a') as f:
        f.write(line)
    tokens = line.split()
    trees = cp.parse(tokens)
    for tree in trees:
        print(tree)
        with open('resutado.txt', 'a') as f:
            f.write(tree.pformat() + "\n")
        if pinta:
            tree.draw()
    with open('resutado.txt', 'a') as f:
        f.write("\n")
        f.write("=" * 50)
        f.write("\n")
infile.close()
Ejemplo n.º 54
0
#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import processing
from nltk import load_parser, draw

cp = load_parser('./grammaire.fcfg', trace=0)

with open("scenario.txt", "r") as text:
    scenario = text.read()
open("facts.clp", 'w')  # Clean Jess file

facts = []

for sentence in scenario.split("\n"):
    try:
        if not sentence: continue

        if sentence[0] == "#":
            continue  #IGNORE THOSE LINES, FOR THEY ARE COMMENTS
        if "." in sentence:
            sentence = sentence[:-1]  #REMOVE "." at end of line
        print("\nSENTENCE: {0}".format(sentence))

        sentence = processing.clean_negation(sentence)
        tokens = sentence.split()
        trees = list(
            cp.parse(tokens))  # arbre base sur les mots de la grammaire

        if (len(trees) > 1):
            print('Is Ambiguous !!!')
Ejemplo n.º 55
0
start,grammar = createGrammar(finalquery)
grammarFile.write("%"+" start "+start+"\n")
grammarFile.write(grammar)
grammarFile.close()
# nerList = list(nerTagger.tag((finalquery.split())))
# for i in nerList:
# 	if i[1] == "DATE":
# 		year = i[0]
# 		finalquery = finalquery.replace(year,"year")
# 	if i[1] in ["CITY","STATE OR PROVINCE","LOCATION","COUNTRY"]:
# 		location = i[0]
# 		finalquery =finalquery.replace(location,"location")
# print(finalquery)
# print(location)

cp = load_parser("newGrammar.fcfg",trace=3)
trees = list(cp.parse(finalquery.strip('?').split()))
for tree in trees:
	print("\n")
	print(tree)
answer = trees[0].label()['SEM']
print(answer)
answer = [s for s in answer if s]
q = ' '.join(answer)
insertlambda=q
# for i in insertlambda:
# 	if i!="select":
# 		for i,k in enumerateinsertlambda:
# 			if k=='%s':
# 				insertlambda[k]=i
# print(insertlambda)
Ejemplo n.º 56
0
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 25 14:13:47 2018

@author: VeNoMzZxHD
"""

import nltk

grammar1 = nltk.CFG.fromstring("""
 S -> NP VP
 VP -> V NP | V NP PP | V NP VP
 PP -> P NP
 V -> "saw" | "ate" | "walked" | "eat" |
 NP -> "John" | "Mary" | "Bob" | "She" | "who" | "Did" | Det N | Det N PP
 Det -> "a" | "an" | "the" | "my" 
 N -> "man" | "dog" | "cat" | "fish" | "fork" | "telescope" | "park"
 P -> "in" | "on" | "by" | "with"
 """)

sentence = "the cat ate the fish".split()
rd_parser = nltk.RecursiveDescentParser(grammar1)
for tree in rd_parser.parse(sentence):
    print(tree)
    
tokens = "Kim likes children".split()
from nltk import load_parser
cp = load_parser('CFG.fcfg', trace=2)
for tree in cp.parse(tokens):
    print(tree)
Ejemplo n.º 57
0
Archivo: demo.py Proyecto: t2y/learnnlp
# -*- coding: utf-8 -*-

"""
素性ベース文法の定義と確認

* 文法ファイルに定義を記述してトークンを与えて構文解析してみる
"""

from nltk import load_parser

token1 = 'I am happy'.split()

cp8 = load_parser('grammer8.fcfg')
cp20 = load_parser('grammer20.fcfg', trace=1)

for i in cp8.parse(token1):
    print(i)

for i in cp20.parse(token1):
    print(i)
Ejemplo n.º 58
0
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgi, cgitb 
cgitb.enable()

form = cgi.FieldStorage()

qu = form.getvalue('question')
from json import dumps
import nltk
from nltk import load_parser
cp=load_parser('grammars/book_grammars/best_actress.fcfg')
query=qu.lower()
#import capital_question
#from capital_question import check
#query=check(query)
import quebestmovie 
from quebestmovie import match
w=match(query)
if w==None:
	print """
		<script type="text/javascript">
		alert("Entered Question is wrong, Please enter a valid Question");
		window.location="../shubham/AwesomeGames/QAhtml/best_actress.html"
		</script>
	"""
	

else:
Ejemplo n.º 59
0
def sample_grammar():
    nltk.data.show_cfg("grammars/book_grammars/feat0.fcfg")
    tokens = "Kim likes children".split()
    from nltk import load_parser
    cp = load_parser("grammars/book_grammars/feat0.fcfg", trace=2)
    trees = cp.nbest_parse(tokens)
Ejemplo n.º 60
0
# -*- coding: utf-8 -*
import nltk
sent = "les filles parlent à un ami".split()
parser = nltk.load_parser("file:3_gramm_traits.fcfg", trace=1)
for tree in parser.parse(sent):
  print(tree)
  tree.draw()