2023-08-28 08:28:06 +00:00
|
|
|
import math
|
|
|
|
from dataclasses import dataclass, field
|
|
|
|
|
|
|
|
import papis.config
|
|
|
|
from papis.document import Document
|
2023-08-29 11:49:22 +00:00
|
|
|
import chevron
|
2023-08-28 08:28:06 +00:00
|
|
|
|
2023-09-19 15:35:39 +00:00
|
|
|
from papis_extract.templating import Templating
|
|
|
|
|
2023-08-28 08:28:06 +00:00
|
|
|
TEXT_SIMILARITY_MINIMUM = 0.75
|
|
|
|
COLOR_SIMILARITY_MINIMUM = 0.833
|
|
|
|
|
|
|
|
COLORS = {
|
|
|
|
"red": (1, 0, 0),
|
|
|
|
"green": (0, 1, 0),
|
|
|
|
"blue": (0, 0, 1),
|
|
|
|
"yellow": (1, 1, 0),
|
|
|
|
"purple": (0.5, 0, 0.5),
|
|
|
|
"orange": (1, 0.65, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Annotation:
|
2023-09-19 15:52:45 +00:00
|
|
|
"""A PDF annotation object.
|
|
|
|
|
|
|
|
Contains all information necessary for the annotation itself, content and metadata.
|
|
|
|
"""
|
2023-08-28 08:28:06 +00:00
|
|
|
|
|
|
|
file: str
|
2023-08-29 20:23:52 +00:00
|
|
|
colors: tuple[float, float, float] = field(default_factory=lambda: (0.0, 0.0, 0.0))
|
2023-08-29 11:49:22 +00:00
|
|
|
content: str = ""
|
|
|
|
page: int = 0
|
2023-08-28 08:28:06 +00:00
|
|
|
tag: str = ""
|
2023-08-29 11:49:22 +00:00
|
|
|
text: str = ""
|
|
|
|
type: str = "Highlight"
|
|
|
|
minimum_similarity_color: float = 1.0
|
2023-08-28 08:28:06 +00:00
|
|
|
|
2023-09-19 15:35:39 +00:00
|
|
|
def format(self, template: Templating):
|
2023-08-28 08:28:06 +00:00
|
|
|
"""Return a formatted string of the annotation.
|
|
|
|
|
|
|
|
Given a provided formatting pattern, this method returns the annotation
|
|
|
|
formatted with the correct marker replacements and removals, ready
|
|
|
|
for display or writing.
|
|
|
|
"""
|
2023-08-29 11:49:22 +00:00
|
|
|
data = {
|
|
|
|
"file": self.file,
|
|
|
|
"quote": self.text,
|
|
|
|
"note": self.content,
|
|
|
|
"page": self.page,
|
|
|
|
"tag": self.tag,
|
|
|
|
"type": self.type,
|
2023-08-28 08:28:06 +00:00
|
|
|
}
|
2023-09-19 15:35:39 +00:00
|
|
|
return chevron.render(template.string, data)
|
2023-08-28 08:28:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def colorname(self):
|
|
|
|
"""Return the stringified version of the annotation color.
|
|
|
|
|
|
|
|
Finds the closest named color to the annotation and returns it,
|
|
|
|
using euclidian distance between the two color vectors.
|
|
|
|
"""
|
2023-08-31 19:32:24 +00:00
|
|
|
annot_colors = self.colors or (0.0, 0.0, 0.0)
|
2023-08-28 08:28:06 +00:00
|
|
|
nearest = None
|
|
|
|
minimum_similarity = (
|
2023-08-28 10:55:01 +00:00
|
|
|
papis.config.getfloat("minimum_similarity_color", "plugins.extract") or 1.0
|
2023-08-28 08:28:06 +00:00
|
|
|
)
|
2023-08-29 11:49:22 +00:00
|
|
|
minimum_similarity = self.minimum_similarity_color
|
2023-08-28 08:28:06 +00:00
|
|
|
for name, values in COLORS.items():
|
|
|
|
similarity_ratio = self._color_similarity_ratio(values, annot_colors)
|
2023-08-29 11:49:22 +00:00
|
|
|
if similarity_ratio >= minimum_similarity:
|
2023-08-28 08:28:06 +00:00
|
|
|
minimum_similarity = similarity_ratio
|
|
|
|
nearest = name
|
|
|
|
return nearest
|
|
|
|
|
|
|
|
def _color_similarity_ratio(self, color_one, color_two):
|
|
|
|
"""Return the similarity of two colors between 0 and 1.
|
|
|
|
|
|
|
|
Takes two rgb color tuples made of floats between 0 and 1,
|
|
|
|
e.g. (1, 0.65, 0) for orange, and returns the similarity
|
|
|
|
between them, with 1 being the same color and 0 being the
|
|
|
|
difference between full black and full white, as a float.
|
|
|
|
"""
|
|
|
|
return 1 - (abs(math.dist([*color_one], [*color_two])) / 3)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class AnnotatedDocument:
|
2023-09-19 15:52:45 +00:00
|
|
|
"""Contains all annotations belonging to a single papis document.
|
|
|
|
|
|
|
|
Combines a document with a list of annotations which belong to it."""
|
2023-08-28 08:28:06 +00:00
|
|
|
document: Document
|
|
|
|
annotations: list[Annotation]
|
2023-08-31 19:32:24 +00:00
|
|
|
|
2023-09-19 15:52:45 +00:00
|
|
|
# TODO could implement a from_doc() static method to generate annotation list?
|