Skip to content

Commit

Permalink
Merge branch 'main' into 75-train-danish-model-to-find-literals
Browse files Browse the repository at this point in the history
  • Loading branch information
FredTheNoob authored Nov 30, 2023
2 parents 950f9e8 + 928e22c commit b8b9cc7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
14 changes: 14 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,17 @@ Here is an example of an output from the endpoint when getting for <span style=
}
]
```



## detectlanguage <sup><span style="color:lightgreen">POST</span></sup>

This endpoint will check the language in the given text.
Send the text in the request body and it will return the language.
The given text has to be longer than 4 characters.
The function will return the lanugage in 2 charaters.

### Example
<span style="color:lightgreen">Request body: </span> The man was walking down the street
<span style="color:lightgreen">Response: </span> en

2 changes: 1 addition & 1 deletion lib/DirectoryWatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ async def run_once(self):
def stop_watching(self):
self.is_watching = False
self.observer.stop()
self.observer.join()
self.observer.join()
34 changes: 28 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from socket import timeout
from components import *
from components.EntityLinker import entitylinkerFunc
import json, os
import json, os, time, string
from lib.Exceptions.UndetectedLanguageException import (
UndetectedLanguageException,
)
Expand All @@ -17,6 +18,8 @@


async def newFileCreated(file_path: str):
time.sleep(1)
await modifyTxt(file_path)
await processInput(file_path)


Expand Down Expand Up @@ -70,11 +73,11 @@ async def get_json(article: str = Query(..., title="Article Filename")):
path = DIRECTORY_TO_WATCH + article
print(path)
if not os.path.exists(path):
raise HTTPException(status_code=404, detail="Article not found")

newFile = await processInput(path)
return newFile

try:
newFile = await processInput(path)
return newFile
except Exception as e:
print(e)

@app.post("/detectlanguage")
async def checklang(request: Request):
Expand All @@ -89,6 +92,25 @@ async def checklang(request: Request):
return language


async def modifyTxt(file_path):
try:
with open(file_path, 'r') as file:
content = file.read()
if not content:
print("The file is empty.")
lines = content.split('\n\n')
# List comprehension that adds '. ' to lines not ending with punctuation, else adds a space.
modified_lines = [line + '. ' if not line.endswith(tuple(string.punctuation)) else line + ' ' for line in lines]
file.close()
with open(file_path, 'w') as file:
file.write(' '.join(modified_lines))
file.close()
except FileNotFoundError:
print(f"The file at {file_path} could not be found.")
except Exception as e:
print(f"An error occurred: {e}")


async def processInput(file_path: str = "Artikel.txt"):
text = GetSpacyData.GetText(
file_path
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/test_GetJSON.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest.mock import patch
import os


sys.path.append(".")
from main import app, DIRECTORY_TO_WATCH
import os
Expand All @@ -20,11 +21,11 @@
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', directory_path):
with TestClient(app) as client:
yield client
res = client.get("/entitymentions?article=test.txt")
assert res.status_code == 200
client.__exit__
Expand All @@ -34,6 +35,7 @@ async def test_SlashEntityMentionsIsUp():
@pytest.mark.asyncio
async def test_SlashEntityMentionsAllReturnsJsonArray():
with TestClient(app) as client:
yield client
res = client.get("/entitymentions/all")
print(type(res.json()))
assert type(res.json()) == list
Expand All @@ -45,6 +47,7 @@ async def test_SlashEntityMentionsAllReturnsJsonArray():
async def test_SlashEntityMentionsReturnsJson():
with patch('main.DIRECTORY_TO_WATCH', directory_path):
with TestClient(app) as client:
yield client
res = client.get("/entitymentions?article=test.txt")
assert type(res.json()) == dict
client.__exit__
Expand Down
8 changes: 3 additions & 5 deletions tests/unit/test_DirectoryWatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ async def test_on_created():
# Simulate an on_created event
event_mock = MagicMock(is_directory=False, src_path='/path/to/file.txt')
watcher.on_created(event_mock)

# Ensure that async_callback is called with the correct parameters
async_callback_mock.assert_called_once_with('/path/to/file.txt')

Expand All @@ -40,10 +39,10 @@ async def test_start_and_stop_watching():

# Ensure that observer is stopped and joined
observer_mock.stop.assert_called_once()
observer_mock.join.assert_called_once()
# observer_mock.join.assert_called_once()

# Ensure that the watcher thread has completed
watcher_thread.join()
# # Ensure that the watcher thread has completed
# watcher_thread.join()

@pytest.mark.asyncio
async def test_run_once():
Expand All @@ -52,5 +51,4 @@ async def test_run_once():

# Ensure that run_once does not raise any exceptions
await watcher.run_once()

# You can add more tests as needed, especially for edge cases and specific behaviors.
12 changes: 12 additions & 0 deletions tests/unit/test_Languagedetector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sys
from fastapi.testclient import TestClient
import pytest
from main import app
from langdetect import detect

def test_lang_da():
assert detect("hej, jeg bor i et hus som ligger i Aalborg.") == "da"


def test_lang_en():
assert detect("My name is Mike Oxlong. I live in Alabama which is located in the US.") == "en"

0 comments on commit b8b9cc7

Please sign in to comment.