Commit f091002d by Steven Bird

fixed demo tests

parent 46eea71f
...@@ -100,7 +100,6 @@ from collocations import * ...@@ -100,7 +100,6 @@ from collocations import *
from decorators import decorator, memoize from decorators import decorator, memoize
from featstruct import * from featstruct import *
from grammar import * from grammar import *
from olac import *
from probability import * from probability import *
from text import * from text import *
from tree import * from tree import *
......
...@@ -236,14 +236,14 @@ def demo(scorer=None, compare_scorer=None): ...@@ -236,14 +236,14 @@ def demo(scorer=None, compare_scorer=None):
if compare_scorer is None: if compare_scorer is None:
compare_scorer = BigramAssocMeasures.raw_freq compare_scorer = BigramAssocMeasures.raw_freq
from nltk import corpus from nltk.corpus import stopwords, webtext
ignored_words = corpus.stopwords.words('english') ignored_words = stopwords.words('english')
word_filter = lambda w: len(w) < 3 or w.lower() in ignored_words word_filter = lambda w: len(w) < 3 or w.lower() in ignored_words
for file in corpus.webtext.files(): for file in webtext.fileids():
words = [word.lower() words = [word.lower()
for word in corpus.webtext.words(file)] for word in webtext.words(file)]
cf = BigramCollocationFinder.from_words(words) cf = BigramCollocationFinder.from_words(words)
cf.apply_freq_filter(3) cf.apply_freq_filter(3)
......
...@@ -1323,10 +1323,6 @@ def cfg_demo(): ...@@ -1323,10 +1323,6 @@ def cfg_demo():
print `grammar.productions()`.replace(',', ',\n'+' '*25) print `grammar.productions()`.replace(',', ',\n'+' '*25)
print print
print 'Coverage of input words by a grammar:'
print grammar.covers(['a','dog'])
print grammar.covers(['a','toy'])
toy_pcfg1 = parse_pcfg(""" toy_pcfg1 = parse_pcfg("""
S -> NP VP [1.0] S -> NP VP [1.0]
NP -> Det N [0.5] | NP PP [0.25] | 'John' [0.1] | 'I' [0.15] NP -> Det N [0.5] | NP PP [0.25] | 'John' [0.1] | 'I' [0.15]
...@@ -1391,10 +1387,6 @@ def pcfg_demo(): ...@@ -1391,10 +1387,6 @@ def pcfg_demo():
print `grammar.productions()`.replace(',', ',\n'+' '*26) print `grammar.productions()`.replace(',', ',\n'+' '*26)
print print
print 'Coverage of input words by a grammar:'
print grammar.covers(['a','boy'])
print grammar.covers(['a','girl'])
# extract productions from three trees and induce the PCFG # extract productions from three trees and induce the PCFG
print "Induce PCFG grammar from treebank data:" print "Induce PCFG grammar from treebank data:"
......
...@@ -217,14 +217,14 @@ class ShiftReduceParser(ParserI): ...@@ -217,14 +217,14 @@ class ShiftReduceParser(ParserI):
stack. This is used with trace level 2 to print 'S' stack. This is used with trace level 2 to print 'S'
before shifted stacks and 'R' before reduced stacks. before shifted stacks and 'R' before reduced stacks.
""" """
str = ' '+marker+' [ ' s = ' '+marker+' [ '
for elt in stack: for elt in stack:
if isinstance(elt, Tree): if isinstance(elt, Tree):
str += `Nonterminal(elt.node)` + ' ' s += `Nonterminal(elt.node)` + ' '
else: else:
str += `elt` + ' ' s += `elt` + ' '
str += '* ' + string.join(remaining_text) + ']' s += '* ' + ' '.join(remaining_text) + ']'
print str print s
def _trace_shift(self, stack, remaining_text): def _trace_shift(self, stack, remaining_text):
""" """
......
...@@ -280,6 +280,8 @@ class FreqDist(dict): ...@@ -280,6 +280,8 @@ class FreqDist(dict):
:rtype: any or None :rtype: any or None
""" """
if self._max_cache is None: if self._max_cache is None:
if len(self) == 0:
raise ValueError('A FreqDist must have at least one sample before max is defined.')
self._max_cache = max([(a,b) for (b,a) in self.items()])[1] self._max_cache = max([(a,b) for (b,a) in self.items()])[1]
return self._max_cache return self._max_cache
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment