Skip to content

Commit

Permalink
literals in english model
Browse files Browse the repository at this point in the history
- Add type property to mention (if its a literal, iri will be null)
- Add label property to mention
- update tests to reflect Entity and EntityLinked changes
- add easter egg

Co-authored-by: pbaekgaard <pbaekgaard@users.noreply.github.com>
  • Loading branch information
FredTheNoob and pbaekgaard committed Nov 23, 2023
1 parent ea5c025 commit 58a0ebf
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 10 deletions.
5 changes: 5 additions & 0 deletions components/EntityLinker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def GetAllEntities(entityMentions):
sentence=sentence["sentence"],
sentenceStartIndex=sentence["sentenceStartIndex"],
sentenceEndIndex=sentence["sentenceEndIndex"],
label=entity["label"],
type=entity["type"],
)
allEntities.append(newEntity)
return allEntities
Expand All @@ -29,6 +31,9 @@ async def entitylinkerFunc(entities, threshold=80):
linked_entities = []
db_path = "./Database/DB.db"
for entity in entities:
if entity.type is "Literal":
linked_entities.append(EntityLinked(entity, ""))
continue
# Use the Read function to get all entities starting with the same name
potential_matches = await Db.Read(
db_path, "EntityIndex", searchPred=entity.name
Expand Down
9 changes: 8 additions & 1 deletion components/GetSpacyData.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,15 @@ def GetEntities(doc) -> List[Entity]:
sentence=entity.sent.text,
sentenceStartIndex=entity.sent.start_char,
sentenceEndIndex=entity.sent.end_char,
label=entity.label_,
type=isLiteral(entity),
)
)

return entities


def isLiteral(entity):
if entity.label_ == "DATE" or entity.label_ == "TIME" or entity.label_ == "PERCENT" or entity.label_ == "MONEY" or entity.label_ == "QUANTITY" or entity.label_ == "ORDINAL" or entity.label_ == "CARDINAL":
return "Literal"
else:
return "Entity"
6 changes: 4 additions & 2 deletions lib/Entity.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class Entity:
def __init__(self, name: str, startIndex: int, endIndex: int, sentence: str, sentenceStartIndex: int, sentenceEndIndex: int):
def __init__(self, name: str, startIndex: int, endIndex: int, sentence: str, sentenceStartIndex: int, sentenceEndIndex: int, label: str, type: str):
self.name = name
self.startIndex = startIndex
self.endIndex = endIndex
self.sentence = sentence
self.sentenceStartIndex = sentenceStartIndex
self.sentenceEndIndex = sentenceEndIndex
self.sentenceEndIndex = sentenceEndIndex
self.label = label
self.type = type
7 changes: 5 additions & 2 deletions lib/EntityLinked.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
class EntityLinked(Entity):
def __init__(self, entity: Entity, iri: str):
super().__init__(
entity.name, entity.startIndex, entity.endIndex, entity.sentence, entity.sentenceStartIndex, entity.sentenceEndIndex
entity.name, entity.startIndex, entity.endIndex, entity.sentence, entity.sentenceStartIndex, entity.sentenceEndIndex, entity.label, entity.type
)
self.iri = iri.replace(" ", "_")

def getEntityJSON(self):
return {
"name": self.name,
"type": self.type,
"label": self.label,
"type": self.type,
"startIndex": self.startIndex,
"endIndex": self.endIndex,
"iri": "knox-kb01.srv.aau.dk/" + self.iri,
"iri": ("knox-kb01.srv.aau.dk/" + self.iri) if self.type is "Entity" else None
}

15 changes: 15 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,28 @@
from lib.FileWatcher import FileWatcher
from langdetect import detect
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles

app = FastAPI()

app.mount("/public", StaticFiles(directory="public"), name="public")


# @app.on_event("startup")
# async def startEvent():
# await main()

@app.get("/", response_class=HTMLResponse)
async def mainpage():

return """
<link rel="stylesheet" href="/public/style.css">
<html>
<audio id="important" autoplay loop src="/public/boombastic.mp3"></audio>
</html>
"""


@app.get("/entitymentions")
async def getJson():
Expand Down
Binary file added public/boombastic.mp3
Binary file not shown.
Binary file added public/important_images/kurt.webp
Binary file not shown.
9 changes: 9 additions & 0 deletions public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
* {
background-image: url("/public/important_images/kurt.webp");
width: 100%;
height: 100%;
overflow: hidden;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
10 changes: 5 additions & 5 deletions tests/unit/test_EntityLinker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ async def mock_insert(db_path, table, entity_name):
):
# Create some Entity instances
entMentions = [
Entity("Entity1", 0, 6, "Sentence1", 0, 9),
Entity("newEntity3", 0, 6, "Sentence2", 0, 9),
Entity("Entity1", 0, 6, "Sentence1", 0, 9, "PERSON", "Entity"),
Entity("newEntity3", 0, 6, "Sentence2", 0, 9, "PERSON", "Entity"),
]

# Call the entitylinkerFunc
Expand Down Expand Up @@ -62,7 +62,7 @@ async def mock_insert(db_path, table, entity_name):
):
# Create some Entity instances
entMentions = [
Entity("Entity1", 0, 6, "Sentence1", 0, 9),
Entity("Entity1", 0, 6, "Sentence1", 0, 9, "PERSON", "Entity"),
]

# Call the entitylinkerFunc
Expand Down Expand Up @@ -95,7 +95,7 @@ async def mock_insert(db_path, table, entity_name):
):
# Create some Entity instances
entMentions = [
Entity("Entity1", 0, 6, "Sentence1", 0, 9),
Entity("Entity1", 0, 6, "Sentence1", 0, 9, "PERSON", "Entity"),
]

# Call the entitylinkerFunc
Expand Down Expand Up @@ -149,7 +149,7 @@ async def mock_insert(db_path, table, queryInformation):
# Create some Entity instances
TestingDataset = {
"test": [
Entity(name, 0, 6, "Sentence1", 0, 9)
Entity(name, 0, 6, "Sentence1", 0, 9, "PERSON", "Entity")
for name in names_with_duplicates
],
"GoldStandardNames": [
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_GetSpacyData.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def test_GetEntities():
"start_char": 0,
"end_char": 40
}),
"label_": "PERSON",
"text": "Drake",
"start_char": 0,
"end_char": 5
Expand All @@ -58,6 +59,7 @@ def test_GetEntities():
"start_char": 0,
"end_char": 14
}),
"label_": "PERSON",
"text": "Buddyguy",
"start_char": 6,
"end_char": 14
Expand Down

0 comments on commit 58a0ebf

Please sign in to comment.