Skip to content

Commit 73504b3

Browse files
authored
Merge pull request #439 from OpenPecha/feat-get_span_text
feat: added get_span_text
2 parents 968ddf7 + 0f2b56c commit 73504b3

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/openpecha/pecha/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import json
2+
from multiprocessing import reduction
23
import shutil
34
from pathlib import Path
45
from typing import Dict, List, Optional
56

67
from stam import AnnotationStore, Offset, Selector
8+
from openpecha.pecha.annotations import span
79

810
from openpecha.exceptions import StamAddAnnotationError, FileNotFoundError, MetaDataValidationError
911
from openpecha.ids import (
@@ -27,6 +29,7 @@ def __init__(self, pecha_id: str, pecha_path: Path) -> None:
2729
self.bases = self.load_bases()
2830
self.annotations = []
2931

32+
3033
@classmethod
3134
def from_path(cls, pecha_path: Path) -> "Pecha":
3235
# Validate that the path exists
@@ -252,6 +255,20 @@ def update_annotation(self, annotation_id: str, annotation:List[BaseAnnotation],
252255
self.add(annotation_id, annotation)
253256
return self
254257

258+
def get_base_text(self) -> str:
259+
base_dir = self.pecha_path / "base"
260+
for base_file in base_dir.glob("*.txt"):
261+
base_text = base_file.read_text(encoding="utf-8")
262+
return base_text
263+
264+
def get_span_text(self, span: Optional[span] = None) -> str:
265+
base_text = self.get_base_text()
266+
if span is None:
267+
return base_text
268+
else:
269+
return base_text[span.start:span.end]
270+
271+
255272

256273
def get_anns(ann_store: AnnotationStore, include_span: bool = False):
257274
anns = []

tests/pecha/test_get_span_text.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from openpecha.pecha import Pecha
2+
from openpecha.pecha.annotations import span
3+
from pathlib import Path
4+
5+
def test_get_span_text():
6+
pecha = Pecha.from_path(Path("tests/pecha/data/I5003D420"))
7+
text = pecha.get_span_text(span(start=0, end=12))
8+
print(text)
9+
assert text == "In Sanskrit:"
10+
11+
def test_get_span_text_without_span():
12+
pecha = Pecha.from_path(Path("tests/pecha/data/I5003D420"))
13+
text = pecha.get_span_text()
14+
assert text == pecha.get_base_text()
15+
16+
if __name__ == "__main__":
17+
test_get_span_text()
18+
test_get_span_text_without_span()

0 commit comments

Comments
 (0)