WUT_Computer_Science/Programming/NLP/compare_chunks.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

99 lines
3.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""Compare kuhy's GPT chunkings against the SemEval gold standard.
Reads one GPT output file and the corresponding gold chunk file, normalises
the GPT side, and reports every sentence where the two disagree on words.
"""
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
logging.basicConfig(format="%(message)s", level=logging.INFO)
def reformat_sentence(sentence: str) -> str:
"""Reformats a sentence by replacing slashes '/' with ' ]'."""
return sentence.replace("/", " ]")
def insert_commas(sentence: str, reference_sentence: str) -> str:
"""Insert commas into *sentence* where *reference_sentence* has them."""
# Splitting the sentences into chunks
sentence_chunks = sentence.split(" ] [ ")
reference_chunks = reference_sentence.split(" ] [ ")
# Insert commas into the original sentence based on the reference sentence
for i, chunk in enumerate(reference_chunks):
if "," in chunk and i < len(sentence_chunks):
sentence_chunks[i] += ","
# Reconstruct the sentence with inserted commas
return " ] [ ".join(sentence_chunks)
def process_sentences(
sentences1: list[str], sentences2: list[str]
) -> list[dict[str, object]]:
"""Compare each GPT chunking against its gold counterpart."""
processed_sentences = []
for sentence1, sentence2 in zip(sentences1, sentences2, strict=False):
# Reformat the first sentence
reformatted_sentence1 = reformat_sentence(sentence1)
reformatted_sentence1 = insert_commas(reformatted_sentence1, sentence2)
# Splitting the sentences into words, ignoring the square brackets
words1 = set(
" ".join(reformatted_sentence1.strip().split("] ["))
.replace("[", "")
.replace("]", "")
.lower()
.split()
)
words2 = set(
" ".join(sentence2.strip().split("] ["))
.replace("[", "")
.replace("]", "")
.lower()
.split()
)
# Finding differences in words
diff = words2 - words1
# Preparing the output sentence
output_sentence = reformatted_sentence1.strip()
if "[ . ]" in sentence2:
output_sentence += " [ . ]"
# Record the differences alongside the merged sentence.
processed_sentences.append(
{
"sentence1": reformatted_sentence1.strip(),
"sentence2": sentence2.strip(),
"differences": diff,
"merged_sentence": output_sentence,
}
)
if diff and diff != {"."}:
logger.info("Difference found!")
logger.info("Sentence 1: %s", reformatted_sentence1.strip())
logger.info("Sentence 2: %s", sentence2.strip())
logger.info("Differences: %s", diff)
logger.info("\n")
return processed_sentences
# The GPT output and the gold chunking for the same corpus half.
file_path_1 = "test_goldStandard/images/images-chunks-gpt-one.txt"
file_path_2 = "test_goldStandard/images/STSint.testinput.images.sent1.chunk.txt"
# Reading the files
with Path(file_path_1).open() as file1, Path(file_path_2).open() as file2:
sentences_file_1 = file1.readlines()
sentences_file_2 = file2.readlines()
# Process the sentences
processed_sentences = process_sentences(sentences_file_1, sentences_file_2)