Advertisement
Heruberuto

Untitled

Dec 16th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import nltk
  2. import nltk as nltk
  3.  
  4. grammar = nltk.CFG.fromstring(open("atis/atis-grammar-cnf.cfg").read())
  5. sentences = open("atis/atis-test-sentences.txt").read()
  6. testSentences = nltk.parse.util.extract_test_sentences(sentences)
  7.  
  8. # initialize the parser
  9. parser = nltk.parse.BottomUpChartParser(grammar)
  10. # parse all test sentences
  11. sentence = testSentences[12]
  12.    
  13.    
  14. def isRecognized(sentence):
  15.     return len(getParseTrees(sentence)) > 0
  16.  
  17.  
  18. def getParseTrees(sentence):
  19.     n = len(sentence)
  20.     nonterminals = []
  21.     trees = []
  22.     for rule in grammar.productions():
  23.         for word in sentence:
  24.             if rule.rhs() == word:
  25.                 nonterminals.append(rule)
  26.  
  27.     for b in range(2,n):
  28.         for i in range(1,n-b+1):
  29.             for k in range(1, b-1):
  30.                 print("ahoj")
  31.                
  32.     return trees
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement