"""Tests from Lab 01. Run: pytest test_wer_practice.py"""
import math

from wer_practice import corpus_wer, edit_counts, wer


def test_identical_text_is_zero():
    assert wer("turn left at the light", "turn left at the light") == 0.0


def test_misheard_name_and_extra_word():
    # REF: schedule a meeting with john tomorrow
    # HYP: schedule a meeting with jon  tomorrow please   -> 1 sub + 1 ins
    ref, hyp = "schedule a meeting with john tomorrow", "schedule a meeting with jon tomorrow please"
    assert edit_counts(ref.split(), hyp.split()) == (1, 0, 1)
    assert math.isclose(wer(ref, hyp), 2 / 6)


def test_swapped_words_cost_two_errors():
    # WER has no idea two words just swapped places
    assert math.isclose(wer("turn left at the next light", "turn left at next the light"), 2 / 6)


def test_deletions_are_counted():
    assert edit_counts("call me back tomorrow".split(), "call back".split()) == (0, 2, 0)


def test_wer_can_pass_100_percent():
    # Lab 01, Exercise 3: one invented sentence on a one-word clip
    assert math.isclose(wer("yes", "yes thanks for watching and please subscribe"), 6.0)


def test_wer_is_not_symmetric():
    assert math.isclose(wer("the cat sat", "the cat sat on the mat"), 1.0)
    assert math.isclose(wer("the cat sat on the mat", "the cat sat"), 0.5)


def test_empty_reference():
    assert wer("", "") == 0.0
    assert wer("", "thanks for watching") == float("inf")


def test_corpus_wer_is_not_the_average():
    # A 2-word clip and a 40-word clip, one error each (Lab 01, Exercise 4)
    short = ("hello there", "hello where")
    long_ref = " ".join(f"w{i}" for i in range(40))
    long_hyp = long_ref.replace("w7", "x7")
    average = (wer(*short) + wer(long_ref, long_hyp)) / 2
    assert math.isclose(average, 0.2625)                       # 26.2%
    assert math.isclose(corpus_wer([short, (long_ref, long_hyp)]), 2 / 42)   # 4.8%
