Skip to content

Instantly share code, notes, and snippets.

@erip
Last active February 4, 2018 22:23
Show Gist options
  • Save erip/796b3b88bd208043a021cef996f472c8 to your computer and use it in GitHub Desktop.
Save erip/796b3b88bd208043a021cef996f472c8 to your computer and use it in GitHub Desktop.
Parsing tests for Python.
from unittest import TestCase
from nltk import FeatureEarleyChartParser, load, word_tokenize
class ParserTestCase(TestCase):
def setUp(self):
self.grammar = load("file:{0}".format("./grammar.fcfg"))
self.parser = FeatureEarleyChartParser(self.grammar, trace=0)
def __should_parse(self, sentence):
try:
return len(list(self.parser.parse(word_tokenize(sentence)))) > 0
except ValueError:
return False
def test_zero(self):
sent = "the dogs bark ."
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_one(self):
sent = "the dog barks ."
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_two(self):
sent = "the dog bark ."
can_parse = self.__should_parse(sent)
self.assertFalse(can_parse, "Shouldn't've been able to parse '{0}'".format(sent))
def test_three(self):
sent = "the dogs barks ."
can_parse = self.__should_parse(sent)
self.assertFalse(can_parse, "Shouldn't've been able to parse '{0}'".format(sent))
def test_four(self):
sent = "dog bark ."
can_parse = self.__should_parse(sent)
self.assertFalse(can_parse, "Shouldn't've been able to parse '{0}'".format(sent))
def test_five(self):
sent = "dogs bark ."
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_six(self):
sent = "the dogs bark the cats ."
can_parse = self.__should_parse(sent)
self.assertFalse(can_parse, "Shouldn't've been able to parse '{0}'".format(sent))
def test_seven(self):
sent = "John thought that the book was interesting ."
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_eight(self):
sent = "John thought the book ."
can_parse = self.__should_parse(sent)
self.assertFalse(can_parse, "Shouldn't've been able to parse '{0}'".format(sent))
def test_nine(self):
sent = "Mary put the book ."
can_parse = self.__should_parse(sent)
self.assertFalse(can_parse, "Shouldn't've been able to parse '{0}'".format(sent))
def test_ten(self):
sent = "Mary put the book on the shelf ."
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_eleven(self):
sent = "did Mary put the book on the shelf ?"
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_twelve(self):
sent = "put Mary the book on the shelf ?"
can_parse = self.__should_parse(sent)
self.assertFalse(can_parse, "Shouldn't've been able to parse '{0}'".format(sent))
def test_thirteen(self):
sent = "what did Mary put on the shelf ?"
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_fourteen(self):
sent = "what did Mary put ?"
can_parse = self.__should_parse(sent)
self.assertFalse(can_parse, "Shouldn't've been able to parse '{0}'".format(sent))
def test_fifteen(self):
sent = "what does John know ?"
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_sixteen(self):
sent = "what does Mary think John knows ?"
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_seventeen(self):
sent = "Mary saw herself ."
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_eighteen(self):
sent = "Mary saw himself ."
can_parse = self.__should_parse(sent)
self.assertFalse(can_parse, "Shouldn't've been able to parse '{0}'".format(sent))
def test_nineteen(self):
sent = "John saw himself ."
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_twenty(self):
sent = "John reached the summit on Tuesday ."
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_twenty_one(self):
sent = "Mary reached the summit for five minutes ."
can_parse = self.__should_parse(sent)
self.assertFalse(can_parse, "Shouldn't've been able to parse '{0}'".format(sent))
def test_twenty_two(self):
sent = "John walked on Tuesday ."
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_twenty_three(self):
sent = "Mary walked for five minutes ."
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
def test_twenty_four(self):
sent = "what does ?"
can_parse = self.__should_parse(sent)
self.assertTrue(can_parse, "Should've been able to parse '{0}'".format(sent))
@erip
Copy link
Author

erip commented Feb 4, 2018

Can be invoked from CLI with nosetest test_parses.py. Assumes your grammar is in the same directory as this test file and is called grammar.fcfg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment