Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import nltk
- import nltk as nltk
- grammar = nltk.CFG.fromstring(open("atis/atis-grammar-cnf.cfg").read())
- sentences = open("atis/atis-test-sentences.txt").read()
- testSentences = nltk.parse.util.extract_test_sentences(sentences)
- # initialize the parser
- parser = nltk.parse.BottomUpChartParser(grammar)
- # parse all test sentences
- sentence = testSentences[12]
- def isRecognized(sentence):
- return len(getParseTrees(sentence)) > 0
- def getParseTrees(sentence):
- n = len(sentence)
- nonterminals = []
- trees = []
- for rule in grammar.productions():
- for word in sentence:
- if rule.rhs() == word:
- nonterminals.append(rule)
- for b in range(2,n):
- for i in range(1,n-b+1):
- for k in range(1, b-1):
- print("ahoj")
- return trees
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement