class SentenceTestCase(unittest.TestCase): def setUp(self): self.sut = Sentence() self.sut.add_end(Word('you', 'PRP')) self.sut.add_end(Word('are', 'VBP')) self.sut.add_end(Word('conflicted', 'VBN')) def test_sentence_grows(self): self.sut.add_start(Word('word', 'NN')) self.assertEqual(4, len(self.sut.words)) def test_render_case_and_fullstop(self): self.assertEqual("You are conflicted.", self.sut.render()) def test_expand_contractions(self): actual = expand_contractions("can't ain't i'm you're") self.assertEqual("cannot am not I am you are", actual) def test_move_tag_seq(self): self.sut.words = move_tag_seq(self.sut.words, ['PRP', 'VBP'], 'end') self.assertEqual(['VBN', 'PRP', 'VBP'], get_tag_seq(self.sut.words)) def test_mutate_tag_seq(self): self.sut.words = mutate_tag_seq(self.sut.words, ['PRP', 'VBP', 'VBN'], ['VBP', 'VBN']) self.assertEqual(['VBP', 'VBN'], get_tag_seq(self.sut.words)) def test_plurals(self): self.sut.add_end(Word('weapons', 'NNS')) self.sut.words = move_tag_seq(self.sut.words, ['VBN', 'NN'], 'start') self.assertEqual(['VBN', 'NNS', 'PRP', 'VBP'], get_tag_seq(self.sut.words))
def test_size_matters_not(self): sut = Sentence( self.tagger.tag("Size does not matter.") ) apply_yodish_grammar(sut) self.assertEqual( "Size matters not.", sut.render() )
def test_so_certain_are_you(self): sut = Sentence( self.tagger.tag("Are you so certain?") ) apply_yodish_grammar(sut) self.assertEqual( "So certain are you?", sut.render() )
def test_uppercase_i(self): sut = Sentence( self.tagger.tag("ii i i") ) apply_yodish_grammar(sut) self.assertEqual( "Ii I I.", sut.render() )
def test_conflicted_you_are(self): sut = Sentence( self.tagger.tag("You are conflicted.") ) apply_yodish_grammar(sut) self.assertEqual( "Conflicted, you are.", sut.render() )
def test_much_anger(self): sut = Sentence( self.tagger.tag("I sense much anger in him.") ) apply_yodish_grammar(sut) self.assertEqual( "Much anger in him, I sense.", sut.render() )
def test_away_put_weapons(self): sut = Sentence( self.tagger.tag("Put your weapons away.") ) apply_yodish_grammar(sut) self.assertEqual( "Away put your weapons.", sut.render() )
def test_my_home_this_is(self): sut = Sentence( self.tagger.tag("This is my home.") ) apply_yodish_grammar(sut) self.assertEqual( "My home this is.", sut.render() )
def test_long_input(self): source = "One two three. Four five six seven eight nine. Ten eleven twelve thirteen. Fourteen fifteen sixteen seventeen eighteen nineteen twenty" sentences = self.tagger.tag(source).replace('\n', '').split(')(') actual = "" expected = "One two three. Four five six seven eight nine. Ten eleven twelve thirteen. Fourteen fifteen sixteen seventeen eighteen nineteen twenty." for pos_tagged in sentences: s = Sentence(pos_tagged) apply_yodish_grammar(s) actual += s.render() + ' ' actual = actual.strip() self.assertEqual(expected, actual)
def test_multiple_sentences(self): source = "You are conflicted. Put your weapons away." sentences = self.tagger.tag(source).split('\n') actual = "" expected = "Conflicted, you are. Away put your weapons." for pos_tagged in sentences: s = Sentence(pos_tagged) apply_yodish_grammar(s) actual += s.render() + ' ' actual = actual.strip() self.assertEqual(expected, actual)
def translate(source): t = PartOfSpeechTagger('http://text-processing.com/api/tag/', 'text') source = expand_contractions(source) source = t.tag(source) source = source.replace('\n','') logging.warning("Foo: " + source) sentences = source.split(')(') translated = "" for pos_tagged in sentences: s = Sentence(pos_tagged) apply_yodish_grammar(s) translated += s.render() + ' ' return translated.strip()
def translate(source): t = PartOfSpeechTagger('http://text-processing.com/api/tag/', 'text') source = expand_contractions(source) source = t.tag(source) source = source.replace('\n', '') logging.warning("Foo: " + source) sentences = source.split(')(') translated = "" for pos_tagged in sentences: s = Sentence(pos_tagged) apply_yodish_grammar(s) translated += s.render() + ' ' return translated.strip()
def test_long_input(self): source = "One two three. Four five six seven eight nine. Ten eleven twelve thirteen. Fourteen fifteen sixteen seventeen eighteen nineteen twenty" sentences = self.tagger.tag(source).replace('\n','').split(')(') actual = "" expected = "One two three. Four five six seven eight nine. Ten eleven twelve thirteen. Fourteen fifteen sixteen seventeen eighteen nineteen twenty." for pos_tagged in sentences: s = Sentence(pos_tagged) apply_yodish_grammar(s) actual += s.render() + ' ' actual = actual.strip() self.assertEqual( expected, actual )
def test_multiple_sentences(self): source = "You are conflicted. Put your weapons away." sentences = self.tagger.tag(source).split('\n') actual = "" expected = "Conflicted, you are. Away put your weapons." for pos_tagged in sentences: s = Sentence(pos_tagged) apply_yodish_grammar(s) actual += s.render() + ' ' actual = actual.strip() self.assertEqual( expected, actual )
class SentenceTestCase(unittest.TestCase): def setUp(self): self.sut = Sentence() self.sut.add_end(Word('you', 'PRP')) self.sut.add_end(Word('are', 'VBP')) self.sut.add_end(Word('conflicted', 'VBN')) def test_sentence_grows(self): self.sut.add_start(Word('word', 'NN')) self.assertEqual(4, len(self.sut.words)) def test_render_case_and_fullstop(self): self.assertEqual("You are conflicted.", self.sut.render()) def test_expand_contractions(self): actual = expand_contractions("can't ain't i'm you're") self.assertEqual("cannot am not I am you are", actual) def test_move_tag_seq(self): self.sut.words = move_tag_seq( self.sut.words, ['PRP','VBP'], 'end' ) self.assertEqual(['VBN','PRP','VBP'], get_tag_seq(self.sut.words)) def test_mutate_tag_seq(self): self.sut.words = mutate_tag_seq( self.sut.words, ['PRP','VBP','VBN'], ['VBP','VBN'] ) self.assertEqual(['VBP','VBN'], get_tag_seq(self.sut.words)) def test_plurals(self): self.sut.add_end(Word('weapons', 'NNS')) self.sut.words = move_tag_seq( self.sut.words, ['VBN','NN'], 'start' ) self.assertEqual( ['VBN','NNS','PRP','VBP'], get_tag_seq(self.sut.words) )
def test_conflicted_you_are(self): sut = Sentence(self.tagger.tag("You are conflicted.")) apply_yodish_grammar(sut) self.assertEqual("Conflicted, you are.", sut.render())
def test_much_anger(self): sut = Sentence(self.tagger.tag("I sense much anger in him.")) apply_yodish_grammar(sut) self.assertEqual("Much anger in him, I sense.", sut.render())
def test_away_put_weapons(self): sut = Sentence(self.tagger.tag("Put your weapons away.")) apply_yodish_grammar(sut) self.assertEqual("Away put your weapons.", sut.render())
def test_my_home_this_is(self): sut = Sentence(self.tagger.tag("This is my home.")) apply_yodish_grammar(sut) self.assertEqual("My home this is.", sut.render())
def test_size_matters_not(self): sut = Sentence(self.tagger.tag("Size does not matter.")) apply_yodish_grammar(sut) self.assertEqual("Size matters not.", sut.render())
def test_so_certain_are_you(self): sut = Sentence(self.tagger.tag("Are you so certain?")) apply_yodish_grammar(sut) self.assertEqual("So certain are you?", sut.render())
def test_uppercase_i(self): sut = Sentence(self.tagger.tag("ii i i")) apply_yodish_grammar(sut) self.assertEqual("Ii I I.", sut.render())