def test_sequenced_number_tagging(averaged, perceptron): """Tests that GenerativePerceptron with a feature extractor doing combinatorial feature explosion is really equivalent to CombinatorialPerceptron. """ p = _train_sequenced_number_tagging(averaged, perceptron, ContextualFeatureGenerator()) sequences = [["0", "1", "2"], ["1", "0", "2", "1", "2", "2", "2"], ["2", "1", "1", "2", "0"]] expected = [["ZERO", "ONE", "TWELVE"], ["ONE", "ZERO", "TWO", "ONE", "TWELVE", "TWO", "TWO"], ["TWO", "ONE", "ONE", "TWELVE", "ZERO"]] for s, e in zip(sequences, expected): assert p.predict(s) == e assert p.predict_all(sequences) == expected
def test_sequenced_number_tagging_after_pickling_and_unpickling(averaged, perceptron): """Dumb sequence tagging example: Perceptron learns to tag numbers with their respective string, except for 2 following 1 which is tagged 'twelve'. """ import pickle p = _train_sequenced_number_tagging(averaged, perceptron, ContextualFeatureExtractor()) # serialization/unserialization serialized = pickle.dumps(p) del p p = pickle.loads(serialized) # test if everything still works sequences = [["0", "1", "2"], ["1", "0", "2", "1", "2", "2", "2"], ["2", "1", "1", "2", "0"]] expected = [["ZERO", "ONE", "TWELVE"], ["ONE", "ZERO", "TWO", "ONE", "TWELVE", "TWO", "TWO"], ["TWO", "ONE", "ONE", "TWELVE", "ZERO"]] for s, e in zip(sequences, expected): assert p.predict(s) == e assert p.predict_all(sequences) == expected