示例#1
0
 def test_get_ents_returns_dictionary_given_empty_string_causes_empty_spacy_doc_ents(
         self):
     model = NerModelTestDouble('eng')
     model.returns_doc_ents([])
     ner = NamedEntityClient(model)
     ents = ner.get_ents("")
     self.assertIsInstance(ents, dict)
示例#2
0
 def test_get_ents_returns_list_given_nonempty_string_causes_empty_spacy_doc_ents(
         self):
     model = NerModelTestDouble('eng')
     model.returns_doc_ents([])
     ner = NamedEntityClient(model)
     ents = ner.get_ents("Madison is a city in Wisconin")
     self.assertIsInstance(ents, dict)
示例#3
0
 def test_get_ents_given_multiple_ents_serializes_all(self):
     model = NerModelTestDouble('eng')
     doc_ents = [{'text': 'Denmark', 'label_': 'GPE'},{'text': 'Jerry Springer', 'label_': 'PERSON'}]
     model.returns_doc_ents(doc_ents)
     ner = NamedEntityClient(model)
     result = ner.get_ents('xxxxxxx')
     expected_result = {'ents': [{'ent': 'Denmark', 'label': 'Location'},{'ent': 'Jerry Springer', 'label': 'Person'}], 'html': ""}
     self.assertListEqual(result['ents'], expected_result['ents'])
示例#4
0
 def test_get_ents_given_spacy_GPE_is_returned_serializes_to_Location(self):
     model = NerModelTestDouble('eng')
     doc_ents = [{'text': 'Denmark', 'label_': 'GPE'}]
     model.returns_doc_ents(doc_ents)
     ner = NamedEntityClient(model)
     result = ner.get_ents('xxxxxxx')
     expected_result = {'ents': [{'ent': 'Denmark', 'label': 'Location'}], 'html': ""}
     self.assertListEqual(result['ents'], expected_result['ents'])
示例#5
0
 def test_get_ents_given_spacy_NORP_is_returned_serializes_to_Group(self):
     model = NerModelTestDouble('eng')
     doc_ents = [{'text': 'Iceland', 'label_': 'NORP'}]
     model.returns_doc_ents(doc_ents)
     ner = NamedEntityClient(model)
     result = ner.get_ents('xxxxxxx')
     expected_result = {'ents': [{'ent': 'Iceland', 'label': 'Group'}], 'html': ""}
     self.assertListEqual(result['ents'], expected_result['ents'])
示例#6
0
 def test_get_ents_given_spacy_PERSON_is_returned_serializes_to_Person(self):
     model = NerModelTestDouble('eng')
     doc_ents = [{'text': 'Paul Graham', 'label_': 'PERSON'}]
     model.returns_doc_ents(doc_ents)
     ner = NamedEntityClient(model)
     result = ner.get_ents('xxxxxxx')
     expected_result = {'ents': [{'ent': 'Paul Graham', 'label': 'Person'}], 'html': ""}
     self.assertListEqual(result['ents'], expected_result['ents'])
示例#7
0
 def test_get_ents_given_spacy_LANGUAGE_is_returned_serializes_to_Language(self):
     model = NerModelTestDouble('eng')
     doc_ents = [{'text': 'ASL', 'label_': 'LANGUAGE'}]
     model.returns_doc_ents(doc_ents)
     ner = NamedEntityClient(model)
     ents = ner.get_ents('...')
     expected_result = {'ents': [{'ent': 'ASL', 'label': 'Language'}], 'html': ""}
     self.assertListEqual(ents['ents'], expected_result['ents'])
示例#8
0
 def test_get_ents_given_spacy_LOC_is_returned_serializes_to_Location(self):
     model = NerModelTestDouble('eng')
     doc_ents = [{'text': 'the ocean', 'label_': 'LOC'}]
     model.returns_doc_ents(doc_ents)
     ner = NamedEntityClient(model)
     ents = ner.get_ents('...')
     expected_result = {'ents': [{'ent': 'the ocean', 'label': 'Location'}], 'html': ""}
     self.assertListEqual(ents['ents'], expected_result['ents'])
示例#9
0
 def test_get_ents_given_multiple_ents_serializes_all(self):
     model = NerModelTestDouble('eng')
     doc_ents = [{'text': 'Australia', 'label_': 'GPE'},
                 {'text': 'Judith Polgar', 'label_': 'PERSON'}]
     model.returns_doc_ents(doc_ents)
     ner = NamedEntityClient(model)
     ents = ner.get_ents('...')
     expected_result = {'ents':
                            [{'ent': 'Australia', 'label': 'Location'},
                             {'ent': 'Judith Polgar', 'label': 'Person'}], 'html': ""}
     self.assertListEqual(ents['ents'], expected_result['ents'])
示例#10
0
 def test_get_ents_given_multiple_ents_serializes_all(self):
     model = NerModelTestDouble('eng')
     doc_ents = [{
         'text': 'Australia',
         'label_': 'GPE'
     }, {
         'text': 'ASL',
         'label_': 'LANGUAGE'
     }]
     model.return_doc_ents(doc_ents)
     ner = NamedEntityClient(model)
     result = ner.get_ents('...')
     expected_result = {
         'ents': [{
             'ent': 'Australia',
             'label': 'Location'
         }, {
             'ent': 'ASL',
             'label': 'Language'
         }],
         'html':
         ""
     }
     self.assertEqual(result['ents'], expected_result['ents'])
示例#11
0
from flask import Flask, render_template, request
import json
import spacy
from ner_client import NamedEntityClient

ner = spacy.load("en_core_web_sm")
ner = NamedEntityClient(ner)

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/ner', methods=["POST"])
def get_named_ents():
    data = request.get_json()
    result = ner.get_ents(data["sentence"])
    response = {"entities": result.get('ents'), "html": result.get('html')}
    return json.dumps(response)


if __name__ == '__main__':
    app.run(debug=True)
示例#12
0
 def test_get_entity_returns_dictionary_given_empty_string(self):
     model = NerModelTestDouble()
     new_client = NamedEntityClient()
     entities = new_client.get_entities("")
     self.assertIsInstance(entities, dict)
示例#13
0
 def test_get_entity_returns_list_given_nonempty_string(self):
     new_client = NamedEntityClient()
     entities = new_client.get_entities("Madison is a city in Wisconsin")
     self.assertIsInstance(entities, dict)
示例#14
0
from flask import Flask, render_template, request
import json
import spacy
from spacy import displacy
from ner_client import NamedEntityClient

app = Flask(__name__)

ner = spacy.load("en_core_web_sm")
ner = NamedEntityClient(ner, displacy)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/ner', methods=['POST'])
def get_named_ents():
    data = request.get_json()
    result = ner.get_ents(data['sentence'])
    response = {"entities": result.get('ents'), "html": result.get('html')}
    return json.dumps(response)


if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0')