Skip to content

Commit

Permalink
entity_mentions.json not found error fixed?
Browse files Browse the repository at this point in the history
  • Loading branch information
FredTheNoob committed Nov 27, 2023
1 parent 7ea46fe commit 197b814
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 3 additions & 0 deletions components/GetSpacyData.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions tests/integration/test_GetJSON.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 197b814

Please sign in to comment.