|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
import unittest |
|
5
|
|
|
from processors import * |
|
6
|
|
|
|
|
7
|
|
|
port = 8886 |
|
8
|
|
|
# initialize the server |
|
9
|
|
|
API = ProcessorsAPI(port) |
|
10
|
|
|
|
|
11
|
|
|
class ProcessorsAPITests(unittest.TestCase): |
|
12
|
|
|
|
|
13
|
|
|
def test_api(self): |
|
14
|
|
|
"ProcessorsAPI instance should remember its port" |
|
15
|
|
|
|
|
16
|
|
|
self.assertEqual(API.port, port, "Port was not {}".format(port)) |
|
17
|
|
|
|
|
18
|
|
|
def test_annotate(self): |
|
19
|
|
|
"API.annotate should produce a Document when given text" |
|
20
|
|
|
|
|
21
|
|
|
text = "This is sentence 1. This is sentence 2." |
|
22
|
|
|
# .annotate should be successful |
|
23
|
|
|
doc = API.annotate(text) |
|
24
|
|
|
self.assertNotEqual(doc, None, ".annotate failed to produce a Document") |
|
25
|
|
|
# should have two sentences |
|
26
|
|
|
num_sentences = 2 |
|
27
|
|
|
self.assertEqual(len(doc.sentences), num_sentences, ".annotate did not produce a Document with {} Sentences for text \"{}\"".format(num_sentences, text)) |
|
28
|
|
|
|
|
29
|
|
|
def test_fastnlp(self): |
|
30
|
|
|
"API.fastnlp.annotate should produce a Document when given text" |
|
31
|
|
|
|
|
32
|
|
|
text = "This is sentence 1. This is sentence 2." |
|
33
|
|
|
# .annotate should be successful |
|
34
|
|
|
doc = API.fastnlp.annotate(text) |
|
35
|
|
|
self.assertNotEqual(doc, None, "fastnlp.annotate failed to produce a Document") |
|
36
|
|
|
# should have two sentences |
|
37
|
|
|
num_sentences = 2 |
|
38
|
|
|
self.assertEqual(len(doc.sentences), num_sentences, "fastnlp.annotate did not produce a Document with {} Sentences for text \"{}\"".format(num_sentences, text)) |
|
39
|
|
|
|
|
40
|
|
|
def test_bionlp(self): |
|
41
|
|
|
"API.bionlp.annotate should produce a Document when given text" |
|
42
|
|
|
|
|
43
|
|
|
text = "Ras phosphorylated Mek." |
|
44
|
|
|
# .annotate should be successful |
|
45
|
|
|
doc = API.bionlp.annotate(text) |
|
46
|
|
|
self.assertNotEqual(doc, None, "bionlp.annotate failed to produce a Document") |
|
47
|
|
|
# should have two sentences |
|
48
|
|
|
num_sentences = 1 |
|
49
|
|
|
self.assertEqual(len(doc.sentences), num_sentences, "bionlp.annotate did not produce a Document with {} Sentences for text \"{}\"".format(num_sentences, text)) |
|
50
|
|
|
|
|
51
|
|
|
def test_sentiment_analysis_of_text(self): |
|
52
|
|
|
"API.sentiment.corenlp.score_text should return scores for text" |
|
53
|
|
|
|
|
54
|
|
|
scores = API.sentiment.corenlp.score_text("This is a very sad sentence.") |
|
55
|
|
|
self.assertTrue(len(scores) > 0, "there were no sentiment scores returned for the text") |
|
56
|
|
|
|
|
57
|
|
|
def test_sentiment_analysis_of_document(self): |
|
58
|
|
|
"API.sentiment.corenlp.score_document should return scores for Document" |
|
59
|
|
|
|
|
60
|
|
|
text = "This is a terribly sad sentence." |
|
61
|
|
|
doc = API.annotate(text) |
|
62
|
|
|
scores = API.sentiment.corenlp.score_document(doc) |
|
63
|
|
|
self.assertTrue(len(scores) > 0, "there were no sentiment scores returned for the Document") |
|
64
|
|
|
|
|
65
|
|
|
def test_sentiment_analysis_of_sentence(self): |
|
66
|
|
|
"API.sentiment.corenlp.score_sentence should return a score for a Sentence" |
|
67
|
|
|
|
|
68
|
|
|
text = "This is a terribly sad sentence." |
|
69
|
|
|
doc = API.annotate(text) |
|
70
|
|
|
s = doc.sentences[0] |
|
71
|
|
|
score = API.sentiment.corenlp.score_sentence(s) |
|
72
|
|
|
self.assertIsInstance(score, int, "score for Sentence should be of type int, but was of type {}".format(type(score))) |
|
73
|
|
|
|
|
74
|
|
|
def test_sentiment_analysis_score_method(self): |
|
75
|
|
|
"API.sentiment.corenlp.score should be able to determine the appropriate API endpoint for the given parameter" |
|
76
|
|
|
# test with text |
|
77
|
|
|
text = "This is a terribly sad sentence." |
|
78
|
|
|
scores = API.sentiment.corenlp.score(text) |
|
79
|
|
|
self.assertTrue(len(scores) > 0, "there were no sentiment scores returned for the text") |
|
80
|
|
|
# test with Document |
|
81
|
|
|
doc = API.annotate(text) |
|
82
|
|
|
scores = API.sentiment.corenlp.score(doc) |
|
83
|
|
|
self.assertTrue(len(scores) > 0, "there were no sentiment scores returned for the Document") |
|
84
|
|
|
# test with Sentence |
|
85
|
|
|
s = doc.sentences[0] |
|
86
|
|
|
score = API.sentiment.corenlp.score(s) |
|
87
|
|
|
self.assertIsInstance(score, int, "score for Sentence should be of type int, but was of type {}".format(type(score))) |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
def test_shutdown(self): |
|
91
|
|
|
"api.stop_server() should stop processors-server.jar" |
|
92
|
|
|
|
|
93
|
|
|
self.assertTrue(API.stop_server(), "Failed to shut down processors-server.jar") |
|
94
|
|
|
|
|
95
|
|
|
if __name__ == "__main__": |
|
96
|
|
|
unittest.main() |
|
97
|
|
|
|