mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-11-10 22:27:08 +01:00
384 findings to none across ten scripts. Most of it is annotation and
docstring work, but the lint surfaced two defects that had nothing to do with
style:
- `gpt_chunks.py` stripped its GPT preamble with
`input_string.strip("iSTS chunks:")`. str.strip takes a SET OF CHARACTERS,
not a prefix, so it also ate any leading i/S/T/s/c/h/u/n/k/space/colon:
"iSTS chunks: sentence one" came out as "entence one", silently losing the
first letter of every chunked sentence. Now removeprefix. ruff found it as
B005 + PLE1310.
- `is_numbered_line_format` had `for string in string` in its list branch,
which raises NameError the moment a list is passed (F821).
- `processing.test_XML()` read a module-level `alignments_data` that does not
exist at module scope; it only ever ran when pasted into a live session. It
takes the data as an argument now and is renamed `log_first_alignment` so
pytest stops collecting a non-test as a test.
Also real rather than cosmetic:
- The OpenAI key was a placeholder string literal in three files, one of them
carrying a "Replace me with os.environ['API_KEY']" comment. All three read
OPENAI_API_KEY now and say so when it is missing -- as committed, none of
these scripts could actually run.
- `rating.rate_similarity` was submitted unfinished: it built a client, never
touched its two arguments and returned None despite `-> str`. It now makes
the same call the rest of the package makes.
- The two 2 kB alignment prompts and one 344-character chunking prompt moved
to prompts/*.txt. Wrapping them in source would have put newlines into the
middle of the prompt.
- `format_alignments` parsed GPT's replies with `eval()`; the file holds one
repr()'d string per line, so ast.literal_eval is both sufficient and safe.
- NLP/__init__.py deleted -- it made the course directory a package named
"NLP", which is an invalid module name (N999) and which nothing imports;
the scripts use plain `import processing`.
INP001 is settled the same way throughout: a coursework file run as
`python x.py` gets a shebang and the executable bit, which is what it is,
rather than an __init__.py that would make a course code into a module name.
Verified: every module compiles and `./run.sh run Programming/NLP` is clean.
The scripts themselves need the SemEval corpus and a paid API key, so they
are not executed here.
Repo-wide: 3,159 findings -> 2,775.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172dj34FDpao6wsix8ZuCzE
98 lines
3.3 KiB
Python
Executable File
98 lines
3.3 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__)
|
|
|
|
|
|
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)
|