From 197b814ea6ffcd5dcf99790eb2a1d1fcff60ee5a Mon Sep 17 00:00:00 2001 From: FredTheNoob <43958385+FredTheNoob@users.noreply.github.com> Date: Mon, 27 Nov 2023 08:52:29 +0100 Subject: [PATCH] entity_mentions.json not found error fixed? --- components/GetSpacyData.py | 3 +++ main.py | 3 --- tests/integration/test_GetJSON.py | 18 ++++++++++++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/components/GetSpacyData.py b/components/GetSpacyData.py index 1e29a1b..774a8eb 100644 --- a/components/GetSpacyData.py +++ b/components/GetSpacyData.py @@ -46,6 +46,9 @@ def DetectLang(text: str): # Method to fully extract entity mentions, find the sentences and calculate indexes and finally create a final JSON def BuildJSONFromEntities(entities: List[EntityLinked], doc, fileName: str) -> JSONEntityOutput: + if not os.path.exists("entity_mentions.json"): + open("entity_mentions.json", "w").close() + # Create a list of sentences with their entities in the desired JSON format currentJson = open("./entity_mentions.json", "r") currentJson.seek(0, os.SEEK_END) diff --git a/main.py b/main.py index b8dd5a1..7b5b000 100644 --- a/main.py +++ b/main.py @@ -76,9 +76,6 @@ async def checklang(request: Request): async def processInput(file_path: str = "Artikel.txt"): - if not os.path.exists("entity_mentions.json"): - open("entity_mentions.json", "w").close() - text = GetSpacyData.GetText( file_path ) # Takes in title of article. Gets article text in string format diff --git a/tests/integration/test_GetJSON.py b/tests/integration/test_GetJSON.py index 2f59832..3021951 100644 --- a/tests/integration/test_GetJSON.py +++ b/tests/integration/test_GetJSON.py @@ -2,14 +2,28 @@ from fastapi.testclient import TestClient import sys from unittest.mock import patch +import os sys.path.append(".") from main import app, DIRECTORY_TO_WATCH +import os + +directory_path = "data_from_A/" +file_path = "data_from_A/test.txt" +text_to_write = "Since the sudden exit of the controversial CEO Martin Kjær last week, both he and the executive board in Region North Jutland have been in hiding." + +# Create the directory if it doesn't exist +os.makedirs(directory_path, exist_ok=True) + +# Create the file if it doesn't exist and write text into it +if not os.path.exists(file_path): + with open(file_path, 'w') as file: + file.write(text_to_write) @pytest.mark.asyncio async def test_SlashEntityMentionsIsUp(): - with patch('main.DIRECTORY_TO_WATCH', 'data_from_A/'): + with patch('main.DIRECTORY_TO_WATCH', directory_path): with TestClient(app) as client: res = client.get("/entitymentions?article=test.txt") assert res.status_code == 200 @@ -29,7 +43,7 @@ async def test_SlashEntityMentionsAllReturnsJsonArray(): @pytest.mark.asyncio async def test_SlashEntityMentionsReturnsJson(): - with patch('main.DIRECTORY_TO_WATCH', 'data_from_A/'): + with patch('main.DIRECTORY_TO_WATCH', directory_path): with TestClient(app) as client: res = client.get("/entitymentions?article=test.txt") assert type(res.json()) == dict