def test_no_context(self):
     result = ContextWindow.find_window("test1.txt",
                                        Position_with_lines(5, 7, 0), 0)
     self.assertEqual(result.position, [Position_with_lines(5, 7, 0)])
     self.assertEqual(result.start, 5)
     self.assertEqual(result.end, 7)
     self.assertEqual(result.line, test1)
Exemple #2
0
 def test_simple(self):
     result = ContextWindow.get_from_file("test1.txt",
                                          Position_with_lines(8, 10, 0), 1)
     self.assertEqual(result.positions, [Position_with_lines(8, 10, 0)])
     self.assertEqual(result.beginning, 4)
     self.assertEqual(result.end, 19)
     self.assertEqual(result.line, test1)
Exemple #3
0
 def test_cross_sentence_boundary(self):
     result = ContextWindow.get_from_file("test1.txt",
                                          Position_with_lines(5, 7, 0), 2)
     self.assertEqual(result.positions, [Position_with_lines(5, 7, 0)])
     self.assertEqual(result.beginning, 0)
     self.assertEqual(result.end, 19)
     self.assertEqual(result.line, test1)
Exemple #4
0
 def test_search_many_one(self):
     result = self.engine.multiple_tokens_search('testing')
     self.assertEqual(
         result, {
             'test1.txt': [Position_with_lines(11, 18, 0)],
             'test2.txt': [Position_with_lines(0, 7, 0)]
         })
 def test_search_many_one(self):
     result = self.engine.search_many('test')
     self.assertEqual(
         result, {
             'test1.txt': [Position_with_lines(11, 15, 0)],
             'test2.txt': [Position_with_lines(3, 7, 0)]
         })
Exemple #6
0
 def test_search_multiple_one_token(self):
     """test if engine for multiple word search
     is working correctly when searching for one word
     
     """
     result = self.s.search_multiple('test')
     self.assertEqual(result, {'test.txt': [Position_with_lines(0, 4, 1)],
                               'tst.txt': [Position_with_lines(0, 4, 0)]})
Exemple #7
0
 def test_searching(self):
     """test if the program is working correctly
     when searching for one word
     
     """
     result = self.s.search('test')
     self.assertEqual(result, {'test.txt': [Position_with_lines(0, 4, 1)],
                               'tst.txt': [Position_with_lines(0, 4, 0)]})
Exemple #8
0
 def test_limit_search_multiple_one_token(self):
     """test if engine for multiple word search
     is working correctly when searching for one word
     
     """
     result = self.s.limit_search_multiple('test', 1, 0)
     self.assertEqual(result, {'test.txt': [Position_with_lines(10, 14, 0),
                                            Position_with_lines(54, 58, 0),
                                            Position_with_lines(31, 35, 1)]})
Exemple #9
0
 def test_search_multiple(self):
     """test if engine for multiple word search
     is working correctly when searching
     for more than one word
     
     """
     result = self.s.search_multiple('this is')
     self.assertEqual(result, {'test.txt': [Position_with_lines(0, 4, 0),
                                            Position_with_lines(5, 7, 0)]})
Exemple #10
0
 def test_limit_scw(self):
     """test if the program is working correctly
     when searching for context windows extended to
     the sentence boundaries
     """
     result = self.s.limit_search_extended_context('test required', 2, 1, 0)
     output = {'test.txt': [Context_Window([Position_with_lines(10, 14, 0), Position_with_lines(15, 23, 0),
                                            Position_with_lines(54, 58, 0)], 0, 58, 'this is a test required for helping students create a test'),
                            Context_Window([Position_with_lines(11, 19, 1), Position_with_lines(31, 35, 1)], 0, 41, ' professor required to write a test first')]}
     self.assertEqual(result, output)
 def test_search_many_limit_offset_all(self):
     result = self.engine.search_many_limit_offset('test',
                                                   limit=2,
                                                   offset=0,
                                                   limits=[2, 2],
                                                   offsets=[0, 0])
     self.assertEqual(
         result, {
             'test1.txt': [Position_with_lines(11, 15, 0)],
             'test2.txt': [Position_with_lines(3, 7, 0)]
         })
Exemple #12
0
 def test_cw_one_out_of_range(self):
     """test if the program is working correctly
     when searching for context of a word with window > text length
     
     """
     result = self.s.search_one_context('test.txt', Position_with_lines(10, 14, 0), 10)
     self.window = Context_Window([Position_with_lines(10, 14, 0)], 0, 58, 'this is a test required for helping students create a test')
     self.assertEqual(result.start, self.window.start)
     self.assertEqual(result.end, self.window.end)
     self.assertEqual(result.position, self.window.position)
     self.assertEqual(result.string, self.window.string)
Exemple #13
0
 def test_cw_no_right_one_token(self):
     """test if the program is working correctly
     when searching for context of a word with no right context
     
     """
     result = self.s.search_one_context('test.txt', Position_with_lines(54, 58, 0), 3)
     self.window = Context_Window([Position_with_lines(54, 58, 0)], 36, 58, 'this is a test required for helping students create a test')
     self.assertEqual(result.start, self.window.start)
     self.assertEqual(result.end, self.window.end)
     self.assertEqual(result.position, self.window.position)
     self.assertEqual(result.string, self.window.string)
Exemple #14
0
 def test_search_many_two(self):
     result = self.engine.multiple_tokens_search('testing ground')
     self.assertEqual(
         result, {
             'test1.txt': [
                 Position_with_lines(11, 18, 0),
                 Position_with_lines(19, 24, 0)
             ],
             'test2.txt':
             [Position_with_lines(0, 7, 0),
              Position_with_lines(8, 14, 0)]
         })
Exemple #15
0
 def test_mcw_singleword_one(self):
     """test if the program is working correctly
     when searching for contexts of a word with window = 1
     
     """
     query = self.s.search_multiple('test')
     result = self.s.search_multiple_contexts(query, 1)
     output = {'test.txt': [Context_Window([Position_with_lines(10, 14, 0)], 8, 23, 'this is a test required for helping students create a test'),
                            Context_Window([Position_with_lines(54, 58, 0)], 52, 58, 'this is a test required for helping students create a test'),
                            Context_Window([Position_with_lines(31, 35, 1)], 29, 41, ' professor required to write a test first')],
               'tst.txt': [Context_Window([Position_with_lines(0, 4, 0)], 0, 7, 'test is required. On the other hand...')]}
     self.assertEqual(result, output)
Exemple #16
0
 def test_join_contexts(self):
     result1 = ContextWindow.get_from_file("test1.txt",
                                           Position_with_lines(5, 7, 0), 2)
     result2 = ContextWindow.get_from_file("test1.txt",
                                           Position_with_lines(8, 10, 0), 2)
     self.con = ContextWindow(
         [Position_with_lines(5, 7, 0),
          Position_with_lines(8, 10, 0)], 'this is my testing ground', 0,
         25)
     self.assertEqual(result1.beginning, self.con.beginning)
     self.assertEqual(result2.end, self.con.end)
     self.assertEqual(result1.line, self.con.line)
     os.remove('test1.txt')
 def test_join(self):
     query1 = ContextWindow.find_window('test1.txt',
                                        Position_with_lines(5, 7, 0), 1)
     query2 = ContextWindow.find_window('test1.txt',
                                        Position_with_lines(11, 15, 0), 1)
     result = query1.join_cont(query2)
     self.wnd = ContextWindow(
         'this is my test',
         [Position_with_lines(5, 7, 0),
          Position_with_lines(11, 15, 0)], 0, 15)
     self.assertEqual(query1.start, self.wnd.start)
     self.assertEqual(query1.end, self.wnd.end)
     self.assertEqual(query1.line, self.wnd.line)
     os.remove('test1.txt')
Exemple #18
0
 def test_join_contexts_sent_bound2(self):
     result1 = ContextWindow.get_from_file("test2.txt",
                                           Position_with_lines(15, 18, 0),
                                           1)
     result2 = ContextWindow.get_from_file("test2.txt",
                                           Position_with_lines(34, 38, 0),
                                           1)
     self.con = ContextWindow(
         [Position_with_lines(15, 18, 0),
          Position_with_lines(34, 38, 0)],
         'testing ground for search. Engine blah bla', 7, 42)
     self.assertEqual(result1.beginning, self.con.beginning)
     self.assertEqual(result2.end, self.con.end)
     self.assertEqual(result1.line, self.con.line)
     os.remove('test2.txt')
Exemple #19
0
 def test_cw_no_word_error(self):
     """test if the program is working correctly
     when searching for context of a position that does not correspond to any word
     
     """
     with self.assertRaises(TypeError):
         self.s.search_one_context('test.txt', Position_with_lines(3, 8, 0), 1)
Exemple #20
0
 def test_cw_line_neg_error(self):
     """test if the program is working correctly
     when searching for context of a word with a negative line number
     
     """
     with self.assertRaises(ValueError):
         self.s.search_one_context('test.txt', Position_with_lines(10, 14, -1), 1)
Exemple #21
0
 def test_cw_filename_error(self):
     """test if the program is working correctly
     when searching for context of a word with error in its filename
     
     """
     with self.assertRaises(TypeError):
         self.s.search_one_context(567, Position_with_lines(10, 14, 0), 1)
Exemple #22
0
 def test_cw_end_start_error(self):
     """test if the program is working correctly
     when searching for context of a word with start position > end position
     
     """
     with self.assertRaises(ValueError):
         self.s.search_one_context('test.txt', Position_with_lines(14, 10, 0), 1)
Exemple #23
0
 def test_indexing_with_lines(self):
     """test if the program is working correctly
     when indexing a single file
     
     """       
     f = open('test.txt', 'w')
     f.write('this is\n a test\n test')
     f.close()        
     self.i.indexing_with_lines('test.txt')        
     db_dict = dict(shelve.open('dbase'))
     dictionary = {
         'this': {'test.txt': [Position_with_lines(0, 4, 0)]},
         'is': {'test.txt': [Position_with_lines(5, 7, 0)]},
         'a': {'test.txt': [Position_with_lines(1, 2, 1)]},
         'test': {'test.txt': [Position_with_lines(3, 7, 1), Position_with_lines(1, 5, 2)]}
     }
     self.assertEqual(db_dict, dictionary)
 def test_search_many_limit_offset_gen_shift(self):
     result = self.engine.search_many_limit_offset_gen('test',
                                                       limit=1,
                                                       offset=1,
                                                       limits=[2, 2],
                                                       offsets=[0, 0])
     result_keys = list(result.keys())
     self.assertEqual(result_keys, ['test2.txt'])
     for key in result.keys():
         for data in result[key]:
             self.assertEqual(data, Position_with_lines(3, 7, 0))
 def test_generator(self):
     result = self.engine.generator(
         [[Position_with_lines(12, 13, 1),
           Position_with_lines(3, 7, 0)],
          [Position_with_lines(11, 15, 0),
           Position_with_lines(3, 7, 0)], []])
     a = []
     for r in result:
         a.append(r)
     self.assertEqual(a, [
         Position_with_lines(11, 15, 0),
         Position_with_lines(3, 7, 0),
         Position_with_lines(12, 13, 1),
         Position_with_lines(3, 7, 0)
     ])
Exemple #26
0
 def test_mcw_two(self):
     """test if the program is working correctly
     when searching for contexts of two words with window = 2
     
     """
     query = self.s.search_multiple('test required')
     result = self.s.search_multiple_contexts(query, 2)
     output = {'test.txt': [Context_Window([Position_with_lines(10, 14, 0), Position_with_lines(15, 23, 0)], 5, 35, 'this is a test required for helping students create a test'),
                            Context_Window([Position_with_lines(54, 58, 0)], 45, 58, 'this is a test required for helping students create a test'),
                            Context_Window([Position_with_lines(11, 19, 1), Position_with_lines(31, 35, 1)], 0, 41, ' professor required to write a test first')],
               'tst.txt': [Context_Window([Position_with_lines(0, 4, 0), Position_with_lines(8, 16, 0)], 0, 24, 'test is required. On the other hand...')]}
     self.assertEqual(result, output)
Exemple #27
0
 def test_scw_single(self):
     """test if the program is working correctly
     when searching for context windows extended to
     the sentence boundaries for a single word
     """
     k = open('newtest.txt', 'w')
     k.write('What is your name? My name is test.')
     k.close()
     ind = indexer.Indexer('newdb')
     ind.indexing_with_lines('newtest.txt')
     del ind
     self.k = SearchEngine('newdb')
     result = self.k.search_extended_context('test', 1)
     output = {'newtest.txt': [Context_Window([Position_with_lines(30, 34, 0)], 19, 35, 'What is your name? My name is test.')]}
     self.assertEqual(result, output)
     del self.k
     for filename in os.listdir(os.getcwd()):            
         if filename == 'newdb' or filename.startswith('newdb.'):
             os.remove(filename)        
     os.remove('newtest.txt')
Exemple #28
0
 def test_highlight(self):
     query = ContextWindow.get_from_file("test1.txt",
                                         Position_with_lines(5, 7, 0), 1)
     query = query.highlight()
     text = 'this <B>is</B> my '
     self.assertEqual(str(query), text)
Exemple #29
0
 def test_expand_contexts_sent_bound1(self):
     result = ContextWindow.get_from_file("test2.txt",
                                          Position_with_lines(19, 25, 0), 2)
     result.expand_context()
     text = 'testing ground for search.'
     self.assertEqual(str(result), text)
Exemple #30
0
 def test_expand_context(self):
     query = ContextWindow.get_from_file("test2.txt",
                                         Position_with_lines(0, 7, 0), 2)
     query.expand_context()
     text = 'testing ground for search.'
     self.assertEqual(str(query), text)