Exemple #1
0
def command_markov_text(parsed_args):
    corpus = Corpus()
    markov = corpus.markov_sentence(sentences=parsed_args.sentences,
            start_word=parsed_args.start_word)
    print(markov)
Exemple #2
0
def command_list_text(parsed_args):
    corpus = Corpus()
    print("Texts learned")
    for path in corpus.texts:
        print('{} "{}"'.format(path, corpus.texts[path]))
Exemple #3
0
def command_delete_text(parsed_args):
    corpus = Corpus()
    corpus.delete_text(parsed_args.text_name)
Exemple #4
0
 def setUp(self):
     self.corpus_location = os.path.join(tempfile.gettempdir(), 'temp_corpus')
     os.mkdir(self.corpus_location)
     self.corpus = Corpus(location=self.corpus_location)
     self.corpus.clear_text()
Exemple #5
0
def command_add_text(parsed_args):
    corpus = Corpus()
    corpus.add_text(parsed_args.location)
    print("I have learned {}".format(parsed_args.location.name))
Exemple #6
0
class Corpus_class(unittest.TestCase):
    def setUp(self):
        self.corpus_location = os.path.join(tempfile.gettempdir(), 'temp_corpus')
        os.mkdir(self.corpus_location)
        self.corpus = Corpus(location=self.corpus_location)
        self.corpus.clear_text()

    def tearDown(self):
        self.corpus.clear_text()
        os.rmdir(self.corpus_location)

    def test_can_add_a_work(self):
        self.corpus.add_text(s.StringIO("I am spartacus"))
        self.assertEquals(len(self.corpus.texts), 1)

    def test_can_add_two_works(self):
        self.corpus.add_text(s.StringIO("I am spartacus"))
        self.corpus.add_text(s.StringIO("No I am spartacus"))
        self.assertEquals(len(self.corpus.texts), 2)

    def test_can_translate_unnamed_stream_into_fname(self):
        self.corpus.add_text(s.StringIO("I am spartacus"))
        self.assertEquals(self.corpus.texts['Untitled'], 'I am spartacus')

    def test_can_put_numbers_at_end_of_fname_to_prevent_doubles(self):
        self.corpus.add_text(s.StringIO("I am spartacus"))
        self.corpus.add_text(s.StringIO("No I am spartacus"))
        self.assertEquals(self.corpus.texts['Untitled(1)'], 'No I am spartacus')

    def test_can_generate_markov_triplet(self):
        self.corpus.add_text(s.StringIO("I am spartacus loosely"))
        self.corpus.add_text(s.StringIO("No I am spartacus splendidly"))
        triplet = self.corpus.get_triplet("am", "spartacus")
        self.assertIn('loosely', triplet)
        self.assertIn('splendidly', triplet)

    def test_can_generate_markov_triplet_from_file(self):
        self.corpus.add_text(file('test_data/test_corpus.txt', 'r'))
        triplet = self.corpus.get_triplet("Pontefract", "Miserabilis")
        self.assertIn('--', triplet)

    def test_can_generate_markov_sentence_from_file(self):
        self.corpus.add_text(file('test_data/test_corpus.txt', 'r'))
        markov = self.corpus.markov_sentence(sentences=1, start_word='Pontefract')
        self.assertIn('Miserabilis', markov)

    def test_can_generate_markov_sentence(self):
        self.corpus.add_text(s.StringIO("I am spartacus loosely."))
        self.corpus.add_text(s.StringIO("No I am spartacus splendidly."))
        markov = self.corpus.markov_sentence(sentences=1)
        self.assert_(markov.endswith('.'))
 def setUp(self):
     self.corpus_location = os.path.join(tempfile.gettempdir(),
                                         'temp_corpus')
     os.mkdir(self.corpus_location)
     self.corpus = Corpus(location=self.corpus_location)
     self.corpus.clear_text()
class Corpus_class(unittest.TestCase):
    def setUp(self):
        self.corpus_location = os.path.join(tempfile.gettempdir(),
                                            'temp_corpus')
        os.mkdir(self.corpus_location)
        self.corpus = Corpus(location=self.corpus_location)
        self.corpus.clear_text()

    def tearDown(self):
        self.corpus.clear_text()
        os.rmdir(self.corpus_location)

    def test_can_add_a_work(self):
        self.corpus.add_text(s.StringIO("I am spartacus"))
        self.assertEquals(len(self.corpus.texts), 1)

    def test_can_add_two_works(self):
        self.corpus.add_text(s.StringIO("I am spartacus"))
        self.corpus.add_text(s.StringIO("No I am spartacus"))
        self.assertEquals(len(self.corpus.texts), 2)

    def test_can_translate_unnamed_stream_into_fname(self):
        self.corpus.add_text(s.StringIO("I am spartacus"))
        self.assertEquals(self.corpus.texts['Untitled'], 'I am spartacus')

    def test_can_put_numbers_at_end_of_fname_to_prevent_doubles(self):
        self.corpus.add_text(s.StringIO("I am spartacus"))
        self.corpus.add_text(s.StringIO("No I am spartacus"))
        self.assertEquals(self.corpus.texts['Untitled(1)'],
                          'No I am spartacus')

    def test_can_generate_markov_triplet(self):
        self.corpus.add_text(s.StringIO("I am spartacus loosely"))
        self.corpus.add_text(s.StringIO("No I am spartacus splendidly"))
        triplet = self.corpus.get_triplet("am", "spartacus")
        self.assertIn('loosely', triplet)
        self.assertIn('splendidly', triplet)

    def test_can_generate_markov_triplet_from_file(self):
        self.corpus.add_text(file('test_data/test_corpus.txt', 'r'))
        triplet = self.corpus.get_triplet("Pontefract", "Miserabilis")
        self.assertIn('--', triplet)

    def test_can_generate_markov_sentence_from_file(self):
        self.corpus.add_text(file('test_data/test_corpus.txt', 'r'))
        markov = self.corpus.markov_sentence(sentences=1,
                                             start_word='Pontefract')
        self.assertIn('Miserabilis', markov)

    def test_can_generate_markov_sentence(self):
        self.corpus.add_text(s.StringIO("I am spartacus loosely."))
        self.corpus.add_text(s.StringIO("No I am spartacus splendidly."))
        markov = self.corpus.markov_sentence(sentences=1)
        self.assert_(markov.endswith('.'))
Exemple #9
0
def step(context, num=0):
    corpus = Corpus()
    assert (len(corpus.texts) == num)
Exemple #10
0
def step(context):
    corpus = Corpus()
    corpus.clear_text()
    assert (len(corpus.texts) == 0)