File size: 2,112 Bytes
6f7cc69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Report how closely rewrite output matches the input paste."""

from __future__ import annotations

import sys
from difflib import SequenceMatcher
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))

from app.pipeline.grammar_fix import correct_text
from app.pipeline.orchestrator import rewrite_text

SAMPLES = {
    "LIBRARIES": (
        "Public libraries offer free books and quiet spaces for study. "
        "If cities don't fund them properly, many young people lose access "
        "to learning resources."
    ),
    "ELECTRIC": (
        "Electric cars produce less air pollution than petrol cars. "
        "However, charging stations are still rare in rural areas, so long "
        "trips remain difficult for drivers."
    ),
    "HOMEMADE": (
        "Homemade meals are usually healthier than fast food. Families who "
        "cooks together also spend more quality time and talk about daily problems."
    ),
    "GROUP": (
        "Group projects helps students learn teamwork, but some members don't "
        "contribute equally. Teachers should check progress regularly instead "
        "of grading only the final report."
    ),
}


def _sim(a: str, b: str) -> float:
    return SequenceMatcher(None, a.lower(), b.lower()).ratio()


def main() -> None:
    print("LOCAL similarity report")
    print("sim_input = vs original paste | sim_grammar = vs grammar-corrected source")
    print("-" * 72)
    for name, text in SAMPLES.items():
        r = rewrite_text(text, tone="Neutral", strength=1, ml_polish=False)
        g = correct_text(text)
        sim_i = _sim(text, r.text)
        sim_g = _sim(g, r.text)
        preview = r.text.replace("\n", " / ")[:140]
        print(
            f"{name}: sim_input={sim_i:.3f} sim_grammar={sim_g:.3f} "
            f"api_similarity={r.similarity:.3f} changed={r.changed} "
            f"words={r.input_words}->{r.output_words}"
        )
        print(f"  OUT: {preview}")
        print()


if __name__ == "__main__":
    main()