Commit 5e4708ee authored by Christof Walther's avatar Christof Walther Committed by Patrick Schlindwein
Browse files

feat: If applied, this commit will add basic intent similarity analysis structures and tests.

GitLab issue #3 "SpaCy - Ähnlichkeit der generierten ID"
parent 5443f286
...@@ -8,15 +8,32 @@ app = FastAPI( ...@@ -8,15 +8,32 @@ app = FastAPI(
description="Based on spacy offer several nlp stuff such as summary, intent-id-generation as it is needed by the IntentFinder" description="Based on spacy offer several nlp stuff such as summary, intent-id-generation as it is needed by the IntentFinder"
) )
strategies = [] # strategies = [strat1(), strat2(), ...]
@app.get("/") @app.get("/")
async def root(): async def root():
return {"message": "nlp server is available"} return {"message": "nlp server is available"}
@app.post("/summarize-text", summary="Generate a summary of the given text.") @app.get("/strategies")
async def get_summary(text: str): async def api_strategies():
return summary.get_summary(text) res = []
for strategy in strategies:
res.append(strategy.id)
return res
@app.post("/summarize/{strategy_id}", summary="Generate a summary of the given text.")
async def summarize(strategy_id: str, req: Request):
for strategy in strategies:
if strategy.id == strategy_id:
quality = 0.5
req_json = await req.json()
summary = strategy.summarize(req_json["text"])
return {"strategy": strategy_id, "quality": quality, "summary": summary}
return {"error": "invalid id"}
@app.post("/intentid", summary="Generate an intent id from a given intent text") @app.post("/intentid", summary="Generate an intent id from a given intent text")
......
from unittest import TestCase from unittest import TestCase
from app.nlp_server import app import app.nlp_server
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from app.summary.summary_strategy_interface import ISummaryStrategy
import json
class TestStrategy1(ISummaryStrategy):
id = "test1"
def summarize(self, text: str) -> str:
return text
class TestStrategy2(ISummaryStrategy):
id = "test2"
def summarize(self, text: str) -> str:
return "result text"
class TestNlpApi(TestCase): class TestNlpApi(TestCase):
def test_root(self): def test_root(self):
client = TestClient(app) client = TestClient(app.nlp_server.app)
response = client.get("/") response = client.get("/")
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == {"message": "nlp server is available"} assert response.json() == {"message": "nlp server is available"}
def test_strategies(self):
# init test client
app.nlp_server.strategies = [TestStrategy1(), TestStrategy2()]
client = TestClient(app.nlp_server.app)
# run test
response = client.get("/strategies")
#assert results
assert response.status_code == 200
assert response.json() == ["test1", "test2"]
def test_summarize(self):
# init test client
app.nlp_server.strategies = [TestStrategy1(), TestStrategy2()]
client = TestClient(app.nlp_server.app)
# run test
response1 = client.post(
"/summarize/test1", json.dumps({"text": "test text"}))
response2 = client.post(
"/summarize/test2", json.dumps({"text": "test text"}))
#assert result
assert response1.status_code == 200
assert response1.json() == {"strategy": "test1",
"quality": 0.5, "summary": "test text"}
assert response2.status_code == 200
assert response2.json() == {"strategy": "test2",
"quality": 0.5, "summary": "result text"}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment