-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnnotation.py
54 lines (44 loc) · 1.6 KB
/
Annotation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from typing import Optional
class Annotation:
"""
The Annotation object contains information regarding a text annotation.
Attributes
----------
file_name (str): Name of the file (with file extension) where the annotation is stored in,
id (str): ID of the annotation,
type (str): Type of the annotation,
begin (int): Start index of annotated string,
end (int): End index of annotated string,
excerpt (str): Excerpt of annotated string
"""
def __init__(
self, file_name: str, type: str, begin: int, end: int, excerpt: str, id: Optional[str] = None
) -> None:
self.file_name = file_name
self.id = id
self.type = type
self.begin = begin
self.end = end
self.excerpt = excerpt
def to_string(self, usage: str = "annotation") -> str:
"""
Create string with all attributes of the Annotation object.
Arguments
---------
usage (str): Formats return string depending on usage.
Returns
-------
str: Annotation object as string.
Raises
------
ValueError: If 'usage' is neither "annotation" nor "info".
"""
if usage == "info":
string = f"Annotation({self.file_name}, {self.id}, {self.type}, {self.begin}, {self.end}, {self.excerpt})"
elif usage == "annotation":
string = (
f"{self.id}\t{self.type} {self.begin} {self.end}\t{self.excerpt}\n"
)
else:
raise ValueError("Invalid usage argument.")
return string