<commit_before>import os
import unittest
from pprint import pprint

import anago
from anago.models import BiLSTMCRF
from anago.preprocess import StaticPreprocessor, DynamicPreprocessor

DATA_ROOT = os.path.join(os.path.dirname(__file__), '../data/conll2003/en/ner')
SAVE_ROOT = os.path.join(os.path.dirname(__file__), 'models')


class TaggerTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        weights_file = os.path.join(SAVE_ROOT, 'weights.h5')
        params_file = os.path.join(SAVE_ROOT, 'params.json')
        preprocessor_file = os.path.join(SAVE_ROOT, 'preprocessor.pickle')

        # Load preprocessor
        p = StaticPreprocessor.load(preprocessor_file)
        dp = DynamicPreprocessor(len(p.label_dic))

        # Load the model.
        model = BiLSTMCRF.load(weights_file, params_file)

        # Build a tagger
        cls.tagger = anago.Tagger(model, preprocessor=p, dynamic_preprocessor=dp)

        cls.sent = 'President Obama is speaking at the White House.'

    def test_predict(self):
        res = self.tagger.predict(self.sent)
        pprint(res)

    def test_analyze(self):
        res = self.tagger.analyze(self.sent)
        pprint(res)<commit_msg>Add test case<commit_after>import os
import unittest
from pprint import pprint

import anago
from anago.models import BiLSTMCRF
from anago.preprocess import StaticPreprocessor, DynamicPreprocessor

DATA_ROOT = os.path.join(os.path.dirname(__file__), '../data/conll2003/en/ner')
SAVE_ROOT = os.path.join(os.path.dirname(__file__), 'models')


class TaggerTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        weights_file = os.path.join(SAVE_ROOT, 'weights.h5')
        params_file = os.path.join(SAVE_ROOT, 'params.json')
        preprocessor_file = os.path.join(SAVE_ROOT, 'preprocessor.pickle')

        # Load preprocessor
        p = StaticPreprocessor.load(preprocessor_file)
        dp = DynamicPreprocessor(len(p.label_dic))

        # Load the model.
        model = BiLSTMCRF.load(weights_file, params_file)

        # Build a tagger
        cls.tagger = anago.Tagger(model, preprocessor=p, dynamic_preprocessor=dp)

        cls.sent = 'President Obama is speaking at the White House.'

    def test_predict(self):
        res = self.tagger.predict(self.sent)
        pprint(res)
        res = self.tagger.predict('Obama')
        pprint(res)
        res = self.tagger.predict('')
        pprint(res)

    def test_analyze(self):
        res = self.tagger.analyze(self.sent)
        pprint(res)
{'fraction_non_alphanumeric': 0.10676156583629894, 'fraction_numerical': 0.0039541320680110716, 'mean_word_length': 3.195688225538972, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5176855', 'n_tokens_mistral': 843, 'n_tokens_neox': 814, 'n_words': 161}