WUT_Computer_Science/Programming/NLP/format_alignments_with_training.py
Krzysztof kuhy Rudnicki cc06bc0cd8 EARIN: zero findings, verified against byte-identical program output
749 findings across six labs and two projects. The mechanical half was
scripted; everything below is what the scripts got wrong or could not do, and
how it was caught.

Three regressions I introduced and then found by RUNNING the code, not by
reading it:

- Converting print() to logger.info() made the programs SILENT. logging's
  last-resort handler only emits WARNING and above, so with no basicConfig
  call `lab2/main.py -t 1 1` printed nothing at all. Fifteen files across the
  repo now configure logging where they log.
- The same conversion dropped `end=""`. lab2 draws its board one square at a
  time and lab1 draws the maze one cell at a time; both came out one glyph
  per line. Those thirteen call sites are sys.stdout.write again, and the
  rendered board now diffs byte-identically against the pre-change output.
- The B007 fix renamed `col` to `_col` in the FIRST loop it matched, which
  was print_letters -- where the variable IS used. That would have been a
  NameError on the first call.

And one the scripts caused silently: annotating 110 signatures dropped the
DEFAULT VALUES from 22 of them, so `preprocessing(a, b, USER_THRESHOLD,
ANIME_THRESHOLD)` would have raised TypeError. Every signature is now diffed
against the pre-change version, name by name and default by default: zero
mismatches.

Real defects the lint surfaced:

- `alpha_beta` ended with an unreachable `return None` that violated its own
  return type. Its maximising and minimising halves were mirror images, so
  they are one `_search_branch` now (C901 11 -> ok).
- `get_data`'s `gpu` switch, `predict`'s `auto`, `create_model`'s
  `rows_number` and five of `preprocess_model_predict`'s fifteen parameters
  were never read. Gone, with their call sites.
- np.random.* -> Generator throughout (NPY002), `.values` -> `.to_numpy()`,
  `datetime.now()` with a tzinfo, `inplace=True` removed, `except:` given a
  type, `random.*` -> `SystemRandom` (S311).
- print_board's three nested helpers became methods (C901 17 -> ok);
  user_input's nine-branch if-ladder became a table (C901 12 -> ok).

Verification, all by running: lab1 `-t` and lab2 `-t 1 1` produce output
byte-identical to the pre-change code; lab2's rendered 8x8 board diffs clean;
lab3's argument parsing returns the same dict for the same argv; lab4 fails
identically before and after (no sklearn here). Also included: the logging
configuration for NLP, MOM and ECRYPT_PROJECT, which had the same silence bug.

Repo-wide: 2,775 findings -> 1,883.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172dj34FDpao6wsix8ZuCzE
2026-08-28 20:39:53 +02:00

139 lines
4.3 KiB
Python
Executable File

#!/usr/bin/env python3
"""Format the few-shot alignment replies, and score them against gold."""
import copy
import logging
import re
from pathlib import Path
import pandas as pd
import processing
logger = logging.getLogger(__name__)
logging.basicConfig(format="%(message)s", level=logging.INFO)
# paths to students andsewrs database
student_answers1_path = (
"test_goldStandard/student/STSint.testinput.answers-students.sent1.txt"
)
student_answers2_path = (
"test_goldStandard/student/STSint.testinput.answers-students.sent2.txt"
)
student_answers_chunked_path1 = (
"test_goldStandard/student/STSint.testinput.answers-students.sent1.chunk.txt"
)
student_answers_chunked_path2 = (
"test_goldStandard/student/STSint.testinput.answers-students.sent2.chunk.txt"
)
student_answers_alignment_path = (
"test_goldStandard/student/STSint.testinput.answers-students.wa"
)
# load data
student_answers = processing.load_sentences(
student_answers1_path, student_answers1_path
)
goldstandard_chunked = processing.load_chunked(
student_answers_chunked_path1, student_answers_chunked_path2
)
goldstandard_alignment = processing.load_alignment(student_answers_alignment_path)
SHORT_FIELDS = 3
MIN_FIELDS = 2
# get a nice anwser-student table
data = pd.DataFrame.merge(
goldstandard_chunked, goldstandard_alignment, left_index=True, right_index=True
)
file_path = "alignments_with_training_student_K.wa"
responses = processing.load_alignment(file_path)
responses = responses["alignment_text"].to_list()
for i, r in enumerate(responses):
logger.info("%s", "\nresponse number " + str(i))
logger.info("%s", r)
unformatted = copy.deepcopy(responses)
for i, response in enumerate(responses):
temp = response.lstrip("\n")
temp = temp.rstrip("\n")
temp = re.sub(r"(?<==)>", "> ", temp) # add space after >
temp = re.sub(r"(?<!\n) +(?!\n)", " ", temp) # remove double space
temp = temp.replace("]", "")
temp = temp.replace("[", "")
temp = temp.replace(" ==> ", " <==> ")
temp = temp.replace(" => ", " <==> ")
temp = temp.replace(" <=> ", " <==> ")
temp = temp.replace(" <== ", " <==> ")
temp = temp.replace("<==> //", " <==> 0 //")
temp = temp.replace("<==> //", " <==> 0 //")
temp = temp.replace("NOALI <==>", "0 <==>")
temp = temp.replace("<==> NOALI", "<==> 0")
temp = temp.replace("\n// NOALI", "\n0 <==> 0 // NOALI")
temp = re.sub(r"(^|[^<])(==>+)", r" <==>", temp)
temp = re.sub(r"^(<==>)", r"0 \1", temp)
temp = re.sub(r"(?<!\n) +(?!\n)", " ", temp) # remove double space
temp = temp.split("\n")
for k in range(len(temp)):
temp[k] = temp[k].split("<==>")
temp[k][1] = temp[k][1].split("//")
for j, chunk in enumerate(
sorted(data.iloc[i]["chunked_sentance1"], key=len, reverse=True)
):
pattern = re.compile(chunk, re.IGNORECASE)
temp[k][0] = pattern.sub(str(j + 1), temp[k][0])
for j, chunk in enumerate(
sorted(data.iloc[i]["chunked_sentance2"], key=len, reverse=True)
):
pattern = re.compile(chunk, re.IGNORECASE)
temp[k][1][0] = pattern.sub(str(j + 1), temp[k][1][0])
if len(temp[k][1]) >= SHORT_FIELDS:
temp[k] = (
temp[k][0]
+ " <==> "
+ temp[k][1][0]
+ " // "
+ temp[k][1][1]
+ " // "
+ temp[k][1][2]
)
elif len(temp[k][1]) == MIN_FIELDS:
temp[k] = (
temp[k][0] + " <==> " + temp[k][1][0] + " // " + temp[k][1][1] + " // 0"
)
temp[k] = re.sub(r"\s{2,}", " ", temp[k]) # remove double space
responses[i] = "\n".join(temp)
logger.info("\nafter formatting\n")
for i, r in enumerate(responses):
logger.info("%s", "\nresponse number " + str(i))
logger.info("FORMATTED\n")
logger.info("%s", r)
logger.info("\nUNFORMATTED\n")
logger.info("%s", unformatted[i])
# write to file
file_path = "student_fixed_format_with_training.txt"
with Path(file_path).open("w") as file:
for i, r in enumerate(responses):
file.write('<sentence id="' + str(i + 1) + '" status="">\n')
file.write("<alignment>\n")
file.write(r)
file.write("\n</alignment>\n")
file.write("</sentence>\n\n")