This commit is contained in:
HOME-VM-TOSCHOOL 2023-11-28 00:45:11 +01:00
parent 18b3ef7788
commit 1d3aecc49a
2 changed files with 59 additions and 48 deletions

BIN
TaskC05/engine.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 560 KiB

View File

@ -17,9 +17,9 @@ def print_debug(*args, **kwargs):
if len(sys.argv) == 1: if len(sys.argv) == 1:
print("Default arguments parsed\n") print("Default arguments parsed\n")
sys.argv.append("elem.arg") sys.argv.append("medium.arg")
sys.argv.append("elem.in") sys.argv.append("medium.in")
sys.argv.append("elem.exp") sys.argv.append("medium.exp")
with open(sys.argv[1], mode="r", newline="", encoding="utf8") as csvfile: with open(sys.argv[1], mode="r", newline="", encoding="utf8") as csvfile:
filereader = csv.reader(csvfile, delimiter="\t", quotechar="|") filereader = csv.reader(csvfile, delimiter="\t", quotechar="|")
@ -30,19 +30,16 @@ with open(sys.argv[1], mode="r", newline="", encoding="utf8") as csvfile:
fsa_description = fsa_description[1:] fsa_description = fsa_description[1:]
# get accepting states # get accepting states
accepting_state = [] accepting_states = []
for item in reversed(fsa_description):
if len(item) == 1:
accepting_state.append(item[0])
else:
break
fsa_description = fsa_description[: -len(accepting_state)]
fsa_description_map: dict[tuple[str, str], list[str]] = {} fsa_description_map: dict[tuple[str, str], list[str]] = {}
fsa_description_direction: dict[str, list[tuple[str, str]]] = {} fsa_description_direction: dict[str, list[tuple[str, str]]] = {}
is_error = False is_error = False
for num, item in enumerate(fsa_description): for num, item in enumerate(fsa_description):
if len(item) == 1:
accepting_states.append(item[0])
continue
for letter in item[2]: for letter in item[2]:
if letter not in alfabet: if letter not in alfabet:
print_debug( print_debug(
@ -57,7 +54,9 @@ with open(sys.argv[1], mode="r", newline="", encoding="utf8") as csvfile:
fsa_description_map[tupleItems] = fsa_description_map.get( fsa_description_map[tupleItems] = fsa_description_map.get(
tupleItems, [] tupleItems, []
) + [item[1]] ) + [item[1]]
fsa_description_direction[item[0]].append((item[1], letter)) fsa_description_direction[item[0]] = fsa_description_direction.get(
item[0], []
) + [(item[1], letter)]
if is_error: if is_error:
exit(-1) exit(-1)
@ -101,60 +100,72 @@ with open(sys.argv[3], mode="r", newline="", encoding="utf8") as file:
def is_correct(current_state: str) -> bool: def is_correct(current_state: str) -> bool:
if current_state in accepting_state: if current_state in accepting_states:
return True return True
return False return False
def find_description(current_state: str) -> tuple[str, str]: def traverse_to_description(current_state: str) -> list[str]:
if current_state not in fsa_description_direction: if current_state not in fsa_description_direction:
return current_state, "-1" # unspecified next state return [""] # the end
good_end_states: list[str] = []
for next_state, description in fsa_description_direction[current_state]: for next_state, description in fsa_description_direction[current_state]:
if is_correct(next_state): new_good_end_states = traverse_to_description(next_state)
return next_state, description for new_good_end_state in new_good_end_states:
if is_correct(next_state) or new_good_end_state != "":
good_end_states.append(description + new_good_end_state)
end_state, description = find_description(next_state) return good_end_states
def traverse_states(current_state: str, word: str) -> str: def find_descriptions(current_state: str, word: str) -> list[str]:
state_after_word = traverse_word(current_state, word)
if state_after_word == "-1":
return [word + ";OOV"]
words_with_description: list[str] = []
for next_state in fsa_description_map[(state_after_word, SEMICOLON)]:
good_endings = traverse_to_description(next_state)
for good_ending in good_endings:
words_with_description.append(word + ";" + good_ending)
sorted_descriptions = sorted(words_with_description, key=lambda x: x.split(";")[1])
return sorted_descriptions
def traverse_word(current_state: str, word: str) -> str:
if len(word) == 0:
return current_state
ending_state = "" ending_state = ""
for letter in word: letter = word[0]
if (current_state, letter) in fsa_description_map: if (current_state, letter) in fsa_description_map:
next_states = fsa_description_map[(current_state, letter)] next_states = fsa_description_map[(current_state, letter)]
for next_state in next_states: for next_state in next_states:
ending_state = traverse_states(next_state, word[1:]) ending_state = traverse_word(next_state, word[1:])
if is_correct(ending_state): return ending_state
return ending_state # found a correct state
return "-2" # no correct state found
else:
return "-1" # unspecified next state
if (ending_state, SEMICOLON) not in fsa_description_map: return "-1" # unspecified next state
return current_state # end of the word
for next_state in fsa_description_map[(ending_state, SEMICOLON)]:
ending_state, description = find_description(next_state)
is_difference = [] is_difference = []
for i, word in enumerate(test_in): test_out_numerator = 0
for word in test_in:
current_state = "0" current_state = "0"
descriptions = []
if len(word) != 0: if len(word) != 0:
current_state = traverse_states(current_state, word) descriptions = find_descriptions(current_state, word)
print(str(i + 1) + "\t", end="") for description in descriptions:
if current_state in accepting_state: print(str(test_out_numerator + 1) + "\t", end="")
print("YES\t", end="") if description != test_out[test_out_numerator]:
if "YES" != test_out[i]: print(f"ERROR\t{word} | {description}")
print("ERROR", word, end="") is_difference.append(test_out_numerator + 1)
is_difference.append(i + 1) else:
else: print(f"OK\t{description}")
print("NO\t", end="")
if "NO" != test_out[i]: test_out_numerator += 1
print("ERROR", word, end="")
is_difference.append(i + 1)
print()
if len(is_difference) != 0: if len(is_difference) != 0:
print(is_difference) print(is_difference)