def test_extend_window(self): self.indexator = Indexer('database') test_file_one = open('test_extend_window.txt', 'w') test_file_one.write('Alina Zakharova is a student!!') test_file_one.close() self.indexator.get_index_with_line('test_extend_window.txt') del self.indexator self.search = SearchEngine('database') window = windows.Context_Window.get_window('test_extend_window.txt', Position_Plus(0, 6, 15), 1) window.extend_window() extended_window = Context_Window('Alina Zakharova is a student!!', [Position_Plus(0, 6, 15)], 0, 30) self.assertEqual(window, extended_window) os.remove('test_extend_window.txt')
def test_extend_window_rus_one(self): self.indexator = Indexer('database') test_file_one = open('test_extend_window_rus.txt', 'w') test_file_one.write('Пьер с грустью слышал над собою насмешки.') test_file_one.close() self.indexator.get_index_with_line('test_extend_window_rus.txt') del self.indexator self.search = SearchEngine('database') window = windows.Context_Window.get_window( 'test_extend_window_rus.txt', Position_Plus(0, 0, 4), 1) window.extend_window() extended_window = Context_Window( 'Пьер с грустью слышал над собою насмешки.', [Position_Plus(0, 0, 4)], 0, 41) self.assertEqual(window, extended_window)
def test_not_crossed(self): self.indexator = Indexer('database') test_file_one = open('test_not_crossed_window.txt', 'w') test_file_one.write('The girl named Alina Zakharova is a student') test_file_one.close() self.indexator.get_index_with_line('test_not_crossed_window.txt') del self.indexator self.search = SearchEngine('database') window_A = windows.Context_Window.get_window( 'test_not_crossed_window.txt', Position_Plus(0, 31, 33), 1) window_B = windows.Context_Window.get_window( 'test_not_crossed_window.txt', Position_Plus(0, 8, 14), 1) crossed_AB = window_A.is_crossed(window_B) self.assertEqual(False, crossed_AB) os.remove('test_not_crossed_window.txt')
def test_extend_window_rus(self): self.indexator = Indexer('database') test_file_one = open('test_extend_window_rus.txt', 'w') test_file_one.write( 'Прогать очень сложно! Алина Захарова студент лингвист!! Аня любит немецкий. В Петербурге идет дождь.' ) test_file_one.close() self.indexator.get_index_with_line('test_extend_window_rus.txt') del self.indexator self.search = SearchEngine('database') window = windows.Context_Window.get_window( 'test_extend_window_rus.txt', Position_Plus(0, 28, 36), 1) window.extend_window() extended_window = Context_Window( 'Прогать очень сложно! Алина Захарова студент лингвист!! Аня любит немецкий. В Петербурге идет дождь.', [Position_Plus(0, 28, 36)], 22, 55) self.assertEqual(window, extended_window)
def test_extend_window_rus_two(self): self.indexator = Indexer('database') test_file_one = open('test_extend_window_rus.txt', 'w') test_file_one.write( 'С разных сторон виднелись пожары. Пьер тогда еще не понимал значения сожженной Москвы и с ужасом смотрел на эти пожары.' ) test_file_one.close() self.indexator.get_index_with_line('test_extend_window_rus.txt') del self.indexator self.search = SearchEngine('database') window = windows.Context_Window.get_window( 'test_extend_window_rus.txt', Position_Plus(0, 34, 38), 1) window.extend_window() extended_window = Context_Window( 'С разных сторон виднелись пожары. Пьер тогда еще не понимал значения сожженной Москвы и с ужасом смотрел на эти пожары.', [Position_Plus(0, 34, 38)], 0, 119) self.assertEqual(window, extended_window)
def test_lines(self): file_line = open('testfile_line.txt', 'w') file_line.write( 'Ф 12 !!! @ # Alina is a student))) \n Alina likes apples 1997\n\n' ) file_line.close() self.indexator.get_index_with_line('testfile_line.txt') del self.indexator base_dict = dict(shelve.open('database')) cool_result = { 'Ф': { 'testfile_line.txt': [Position_Plus(0, 0, 1)] }, '12': { 'testfile_line.txt': [Position_Plus(0, 2, 4)] }, 'Alina': { 'testfile_line.txt': [Position_Plus(0, 13, 18), Position_Plus(1, 1, 6)] }, 'is': { 'testfile_line.txt': [Position_Plus(0, 19, 21)] }, 'a': { 'testfile_line.txt': [Position_Plus(0, 22, 23)] }, 'student': { 'testfile_line.txt': [Position_Plus(0, 24, 31)] }, 'likes': { 'testfile_line.txt': [Position_Plus(1, 7, 12)] }, 'apples': { 'testfile_line.txt': [Position_Plus(1, 13, 19)] }, '1997': { 'testfile_line.txt': [Position_Plus(1, 20, 24)] } } self.assertEqual(base_dict, cool_result) os.remove('testfile_line.txt')
def test_already_extended_window(self): self.indexator = Indexer('database') test_file_one = open('test_already_extended_window.txt', 'w') test_file_one.write('Alina Zakharova is a student!!') test_file_one.close() self.indexator.get_index_with_line('test_already_extended_window.txt') del self.indexator self.search = SearchEngine('database') window = windows.Context_Window.get_window( 'test_already_extended_window.txt', Position_Plus(0, 16, 18), 2) os.remove('test_already_extended_window.txt')
def test_myError_str_not_found(self): self.indexator = Indexer('database') test_file_one = open('test_window_five.txt', 'w') test_file_one.write('Alina Zakharova is a student') test_file_one.close() self.indexator.get_index_with_line('test_window_five.txt') del self.indexator self.search = SearchEngine('database') with self.assertRaises(TypeError): result = windows.Context_Window.get_window( 'test_window_five.txt', Position_Plus(3, 21, 28), 3) os.remove('test_window_five.txt')
def test_get_window_begin(self): self.indexator = Indexer('database') test_file_one = open('test_window_three.txt', 'w') test_file_one.write('Alina Zakharova is a student') test_file_one.close() self.indexator.get_index_with_line('test_window_three.txt') del self.indexator self.search = SearchEngine('database') result = windows.Context_Window.get_window('test_window_three.txt', Position_Plus(0, 0, 5), 1) self.win = Context_Window('string', 'positions', 'win_start', 'win_end') self.win.string = 'Alina Zakharova is a student' self.win.positions = [Position_Plus(0, 0, 5)] self.win.win_start = 0 self.win.win_end = 15 self.assertEqual(result.string, self.win.string) self.assertEqual(result.positions, self.win.positions) self.assertEqual(result.win_start, self.win.win_start) self.assertEqual(result.win_end, self.win.win_end) self.assertEqual(result, self.win) os.remove('test_window_three.txt')
def test_get_window_simple_plus(self): self.indexator = Indexer('database') test_file_one = open('test_window_two.txt', 'w') test_file_one.write('Little Alina Zakharova is a linguist student)))') test_file_one.close() self.indexator.get_index_with_line('test_window_two.txt') del self.indexator self.search = SearchEngine('database') result = windows.Context_Window.get_window('test_window_two.txt', Position_Plus(0, 23, 25), 2) self.win = Context_Window('string', 'positions', 'win_start', 'win_end') self.win.string = 'Little Alina Zakharova is a linguist student)))' self.win.positions = [Position_Plus(0, 23, 25)] self.win.win_start = 7 self.win.win_end = 36 self.assertEqual(result.string, self.win.string) self.assertEqual(result.positions, self.win.positions) self.assertEqual(result.win_start, self.win.win_start) self.assertEqual(result.win_end, self.win.win_end) self.assertEqual(result, self.win) os.remove('test_window_two.txt')
def test_united_window(self): self.indexator = Indexer('database') test_file_one = open('test_united_window.txt', 'w') test_file_one.write('The girl named Alina Zakharova is a student') test_file_one.close() self.indexator.get_index_with_line('test_united_window.txt') del self.indexator self.search = SearchEngine('database') window_A = windows.Context_Window.get_window('test_united_window.txt', Position_Plus(0, 4, 20), 1) window_B = windows.Context_Window.get_window('test_united_window.txt', Position_Plus(0, 9, 30), 1) window_A.get_united_window(window_B) self.win = windows.Context_Window( 'The girl named Alina Zakharova is a student', [Position_Plus(0, 4, 20), Position_Plus(0, 9, 30)], 9, 20) self.assertEqual(window_A.string, self.win.string) self.assertEqual(window_A.win_start, self.win.win_start) self.assertEqual(window_A.win_end, self.win.win_end) os.remove('test_united_window.txt')
def test_highlight_window_one(self): self.indexator = Indexer('database') test_file_one = open('test_highlight_window.txt', 'w') test_file_one.write('Alina Zakharova is a student') test_file_one.close() self.indexator.get_index_with_line('test_highlight_window.txt') del self.indexator self.search = SearchEngine('database') window = windows.Context_Window.get_window('test_highlight_window.txt', Position_Plus(0, 6, 15), 1) result = window.highlight_window() output_string = 'Alina <b>Zakharova</b> is' self.assertEqual(result, output_string) os.remove('test_highlight_window.txt')
def test_lt_diff_lines_two(self): pos = Position_Plus(1, 2, 4) other_pos = Position_Plus(2, 0, 1) result = other_pos.__lt__(pos) self.assertEqual(result, False)
def test_lt_diff_lines_one(self): pos = Position_Plus(1, 2, 4) other_pos = Position_Plus(0, 0, 1) result = other_pos.__lt__(pos) self.assertEqual(result, True)
def test__lt__same_line_two(self): pos = Position_Plus(0, 0, 4) other_pos = Position_Plus(0, 5, 8) result = other_pos.__lt__(pos) self.assertEqual(result, False)
left = PATTERN_LEFT.search(to_left) right = PATTERN_RIGHT.search(to_right) if left is None: self.win_start = 0 else: self.win_start -= left.start() if right is None: self.win_end = len(self.string) else: self.win_end += right.start() + 1 def highlight_window(self): ''' This function takes a substring of window string, which corresponds to the window size and highlights it ''' win_string = self.string[self.win_start:self.win_end] fin = '</b>' st = '<b>' for position in reversed(self.positions): end = position.end - self.win_start begin = position.start - self.win_start win_string_one = win_string[:end] + fin + win_string[end:] win_string = win_string_one[:begin] + st + win_string_one[begin:] return win_string if __name__ == '__main__': window_X = window_A.get_window('test.txt', Position_Plus(0, 4, 20), 1) window_Y = window_B.get_window('test.txt', Position_Plus(0, 9, 30), 1)
def setUp(self): self.maxDiff = None self.window = Context_Window( 'The girl named Alina Zakharova is a student', [Position_Plus(0, 4, 20), Position_Plus(0, 9, 30)], 8, 20)