def test_label(nlp: BertPipeline): # We add the pattern recognizer to the pipeline. pattern_recognizer = AttentionPatternRecognizer() nlp.pattern_recognizer = pattern_recognizer text_1 = ("We are great fans of Slack, but we wish the subscriptions " "were more accessible to small startups.") text_2 = "The Slack often has bugs." text_3 = "best of all is the warm vibe" aspect = "slack" examples = [ Example(text_1, aspect), Example(text_2, aspect), Example(text_3, aspect) ] tokenized_examples = nlp.tokenize(examples) input_batch = nlp.encode(tokenized_examples) output_batch = nlp.predict(input_batch) labeled_examples = nlp.label(tokenized_examples, output_batch) labeled_examples = list(labeled_examples) labeled_1, labeled_2, labeled_3 = labeled_examples assert labeled_1.sentiment == Sentiment.positive assert labeled_2.sentiment == Sentiment.negative assert isinstance(labeled_1.scores, list) assert np.argmax(labeled_1.aspect_representation.look_at) == 5 assert np.argmax(labeled_2.aspect_representation.look_at) == 1 # We need to calibrate the model. The prediction should be neutral. # In fact, the model does not recognize the aspect correctly. assert labeled_3.sentiment == Sentiment.positive assert np.allclose(labeled_3.aspect_representation.look_at, [1.0, 0.16, 0.50, 0.54, 0.34, 0.39, 0.12], atol=0.01)
def test_predict(nlp: BertPipeline): text_1 = ("We are great fans of Slack, but we wish the subscriptions " "were more accessible to small startups.") text_2 = "We are great fans of Slack" aspect = "Slack" examples = [Example(text_1, aspect), Example(text_2, aspect)] tokenized_examples = nlp.tokenize(examples) input_batch = nlp.encode(tokenized_examples) output_batch = nlp.predict(input_batch) assert output_batch.scores.shape == [2, 3] assert output_batch.hidden_states.shape == [2, 13, 23, 768] assert output_batch.attentions.shape == [2, 12, 12, 23, 23] assert output_batch.attention_grads.shape == [2, 12, 12, 23, 23] scores = output_batch.scores.numpy() assert np.argmax(scores, axis=-1).tolist() == [2, 2]
def test_get_completed_task(nlp: BertPipeline): text = ("We are great fans of Slack.\n" "The Slack often has bugs.\n" "best of all is the warm vibe") # Make sure we have defined a text_splitter, even naive. nlp.text_splitter = lambda text: text.split('\n') task = nlp.preprocess(text, aspects=['slack', 'price']) tokenized_examples = task.batch input_batch = nlp.encode(tokenized_examples) output_batch = nlp.predict(input_batch) aspect_span_labeled = nlp.label(tokenized_examples, output_batch) completed_task = nlp.get_completed_task(task, aspect_span_labeled) assert len(completed_task.batch) == 6 assert completed_task.indices == [(0, 3), (3, 6)] slack, price = completed_task assert slack.text == price.text == text # The sentiment among fragments are different. We normalize scores. assert np.allclose(slack.scores, [0.06, 0.46, 0.48], atol=0.01) # Please note once gain that there is a problem # with the neutral sentiment, model is over-fitted. assert np.allclose(price.scores, [0.06, 0.42, 0.52], atol=0.01)