# Path: test/conftest.py
# def assert_no_expr_in_log(flog, expr='error'):
#     """Raise if `expr` is found in flog"""
#     assert os.path.isfile(flog)
# 
#     matched_lines = [line for line in open(flog, 'r')
#                      if re.search(expr, line.lower())]
# 
#     if matched_lines:
#         print(matched_lines)
#         assert len(matched_lines) == 0


import os
import pytest
import abkhazia.language.language_model as language_model
import abkhazia.utils as utils
import abkhazia.kaldi as kaldi
from .conftest import assert_no_expr_in_log
# along with abkhazia. If not, see <http://www.gnu.org/licenses/>.
"""Test of the abkhazia.language.language_model module"""




levels = ['phone', 'word']
orders = range(1, 4)
params = [(l, o) for l in levels for o in orders]


def test_srilm_path():
    # test we can reach the ngram binary from SRILM in the Kaldi
    # environment (problematic because it's change from Linux to Mac)
    # This raises RuntimeError if failing
    utils.jobs.run('which ngram', env=kaldi.path.kaldi_path())


@pytest.mark.parametrize('level, order', params)
def test_lm(level, order, corpus, tmpdir):
    output_dir = str(tmpdir.mkdir('lang'))
    flog = os.path.join(output_dir, 'language.log')
    log = utils.logger.get_log(flog)
    lm = language_model.LanguageModel(corpus, output_dir, log=log)
    lm.level = level
    lm.order = order
    lm.create()
    lm.run()
    lm.export()
    language_model.check_language_model(output_dir)

# Path: test/conftest.py
# def assert_no_expr_in_log(flog, expr='error'):
#     """Raise if `expr` is found in flog"""
#     assert os.path.isfile(flog)
# 
#     matched_lines = [line for line in open(flog, 'r')
#                      if re.search(expr, line.lower())]
# 
#     if matched_lines:
#         print(matched_lines)
#         assert len(matched_lines) == 0


# Path: test/test_language.py
import os
import pytest
import abkhazia.language.language_model as language_model
import abkhazia.utils as utils
import abkhazia.kaldi as kaldi
from .conftest import assert_no_expr_in_log

# along with abkhazia. If not, see <http://www.gnu.org/licenses/>.
"""Test of the abkhazia.language.language_model module"""




levels = ['phone', 'word']
orders = range(1, 4)
params = [(l, o) for l in levels for o in orders]


def test_srilm_path():
    # test we can reach the ngram binary from SRILM in the Kaldi
    # environment (problematic because it's change from Linux to Mac)
    # This raises RuntimeError if failing
    utils.jobs.run('which ngram', env=kaldi.path.kaldi_path())


@pytest.mark.parametrize('level, order', params)
def test_lm(level, order, corpus, tmpdir):
    output_dir = str(tmpdir.mkdir('lang'))
    flog = os.path.join(output_dir, 'language.log')
    log = utils.logger.get_log(flog)
    lm = language_model.LanguageModel(corpus, output_dir, log=log)
    lm.level = level
    lm.order = order
    lm.create()
    lm.run()
    lm.export()
    language_model.check_language_model(output_dir)

    assert_no_expr_in_log(flog, 'error')