jfz-2023-s473579/TaskC05/run.py

87 lines
3.3 KiB
Python
Raw Normal View History

2023-11-16 21:40:35 +01:00
import sys
2023-11-21 17:14:36 +01:00
def write_answer(row, answer):
with open(output_file, 'a', encoding='utf-8') as file:
file.write(row.strip()+';'+answer+'\n')
2023-11-16 21:40:35 +01:00
def line_checking(row, position):
2023-11-21 17:14:36 +01:00
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)):
2023-11-16 21:40:35 +01:00
2023-11-21 17:14:36 +01:00
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
2023-11-16 21:40:35 +01:00
2023-11-21 17:14:36 +01:00
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
2023-11-16 21:40:35 +01:00
def find_next_position(position, character):
2023-11-21 17:14:36 +01:00
with open(used_table, 'r', encoding='utf-8') as readed_used_table:
2023-11-16 21:40:35 +01:00
take_all_possible_positions = []
for row_used_table in readed_used_table:
2023-11-21 17:14:36 +01:00
line = row_used_table.strip().split('\t')
2023-11-16 21:40:35 +01:00
if len(line) != 1:
2023-11-21 17:14:36 +01:00
2023-11-16 21:40:35 +01:00
if position == line[0] and character == line[2]:
take_all_possible_positions.append(line[1])
return take_all_possible_positions
2023-11-21 17:14:36 +01:00
2023-11-16 21:40:35 +01:00
def what_is_the_ended_positions():
array = []
with open(used_table, 'r') as file:
for row in file:
line = row.strip().split('\t')
2023-11-21 17:14:36 +01:00
if len(line) == 1:
2023-11-16 21:40:35 +01:00
array += line
return array
2023-11-21 17:14:36 +01:00
2023-11-21 17:15:47 +01:00
# 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]
2023-11-21 17:14:36 +01:00
with open(output_file, 'w', encoding='utf-8') as readed_output_file:
with open(input_file, 'r', encoding='utf-8') as readed_input_file:
2023-11-16 21:40:35 +01:00
for row_input_file in readed_input_file:
2023-11-21 17:14:36 +01:00
line_checking(row_input_file, '0')