# tests: lint, mypy

import tempfile
import subprocess
from neuralmonkey.logging import log

# pylint: disable=too-few-public-methods
# to be further refactored


class BLEUReferenceImplWrapper(object):
    """Wrapper for TectoMT's wrapper for reference NIST and BLEU scorer"""

    def __init__(self, wrapper, name="BLEU", encoding="utf-8"):
        log("Reference BLEU wrapper is deprecated", color="red")
        self.wrapper = wrapper
        self.encoding = encoding
        self.name = name

    def serialize_to_bytes(self, sentences):
        # type: (List[List[str]]) -> bytes
        joined = [" ".join(r) for r in sentences]
        string = "\n".join(joined) + "\n"
        return string.encode(self.encoding)

    def __call__(self, decoded, references):
        # type: (List[List[str]], List[List[str]]) -> float

        ref_bytes = self.serialize_to_bytes(references)
        dec_bytes = self.serialize_to_bytes(decoded)

        reffile = tempfile.NamedTemporaryFile()
        reffile.write(ref_bytes)
        reffile.flush()

        output_proc = subprocess.run(["perl", self.wrapper, reffile.name],
                                     input=dec_bytes,
                                     stderr=subprocess.PIPE,
                                     stdout=subprocess.PIPE)

        proc_stdout = output_proc.stdout.decode("utf-8")
        lines = proc_stdout.splitlines()

        try:
            bleu_score = float(lines[0])
            return bleu_score
        except IndexError:
            log("Error: Malformed output from BLEU wrapper:", color="red")
            log(print(proc_stdout), color="red")
            log(print("======="), color="red")
            return 0.0
        except ValueError:
            log("Value error - bleu '{}' is not a number.".format(lines[0]),
                color="red")
            return 0.0

@@ -1346,16 +1346,32 @@
 %22utf-8%22)
+  # type: ignore
 %0A       
@@ -1607,22 +1607,16 @@
     log(
-print(
 proc_std
@@ -1618,17 +1618,16 @@
 c_stdout
-)
 , color=
@@ -1653,14 +1653,8 @@
 log(
-print(
 %22===
@@ -1658,17 +1658,16 @@
 =======%22
-)
 , color=

fix bleu_ref