import sys def write_answer(row, answer): with open(output_file, 'a', encoding='utf-8') as file: file.write(row.strip()+';'+answer+'\n') def line_checking(row, position): stack = [(row, position)] help_row, help_position=row,position while stack: help_row, help_position = stack.pop() if help_row == '': help_row = '\n' next_character_array = find_next_position(help_position, help_row[0]) if next_character_array: for element_in_next_position_array in next_character_array: stack.append((help_row[1:], element_in_next_position_array)) if help_row == '\n': next_character_array = find_next_position(help_position, ';') if next_character_array: help_position_array, character_array = find_character_after_semicolon(next_character_array[0]) character_array = sorted(character_array) help_position_array = sorted(help_position_array) for i in range(len(character_array)): ended_answer = find_position_to_print_the_answer(help_position_array[i], character_array[i]) write_answer(row,ended_answer) else: write_answer(row, 'OOV') # return False def find_position_to_print_the_answer(position, character): with open(used_table, 'r', encoding='utf-8') as readed_used_table: last_pos = position word = character # if last_pos == what_is_the_ended_positions(): # return word for row_used_table in readed_used_table: line = row_used_table.strip().split('\t') if len(line) != 1: if last_pos == line[0]: word+=line[2] last_pos = line[1] return word def find_character_after_semicolon(position): with open(used_table, 'r', encoding='utf-8') as readed_used_table: last_position = [] take_all_possible_characters = [] for row_used_table in readed_used_table: line = row_used_table.strip().split('\t') if len(line) != 1: if position == line[0]: take_all_possible_characters.append(line[2]) last_position.append((line[1])) return last_position,take_all_possible_characters def find_next_position(position, character): with open(used_table, 'r', encoding='utf-8') as readed_used_table: take_all_possible_positions = [] for row_used_table in readed_used_table: line = row_used_table.strip().split('\t') if len(line) != 1: if position == line[0] and character == line[2]: take_all_possible_positions.append(line[1]) return take_all_possible_positions def what_is_the_ended_positions(): array = [] with open(used_table, 'r') as file: for row in file: line = row.strip().split('\t') if len(line) == 1: array += line return array # used_table = 'multi.arg' # input_file = 'multi.in' # output_file = 'multi.out' used_table = sys.argv[1] input_file = sys.argv[2] output_file = sys.argv[3] with open(output_file, 'w', encoding='utf-8') as readed_output_file: with open(input_file, 'r', encoding='utf-8') as readed_input_file: for row_input_file in readed_input_file: line_checking(row_input_file, '0')