-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port USFM code from Machine up to commit a9058ce (#111)
* port commit 436a67d that moved logic to parallel text corpus * port commit 436a67d that adds test cases for scripture text corpus * port commit 29c9799 that adds support for mixed source corpora * port commit fa65835, default to major biblical terms * port commit a9058ce, support for non-verse text segments in Scripture corpora * handle overloading, update __init__.py, use is_scripture, move files to corpora folder, use top level constant for empty ScriptureRef, change __eq__ back to exact_equals, keep test files consistent
- Loading branch information
1 parent
2f7f44f
commit 953f203
Showing
35 changed files
with
1,397 additions
and
461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from __future__ import annotations | ||
|
||
from functools import total_ordering | ||
from typing import Optional | ||
|
||
from ..utils.comparable import Comparable | ||
|
||
|
||
@total_ordering | ||
class ScriptureElement(Comparable): | ||
def __init__(self, position: int, name: str) -> None: | ||
self._position = position | ||
self._name = name | ||
|
||
@property | ||
def position(self) -> int: | ||
return self._position | ||
|
||
@property | ||
def name(self) -> str: | ||
return self._name | ||
|
||
def compare_to(self, other: object, strict: Optional[bool] = True) -> int: | ||
if not isinstance(other, ScriptureElement): | ||
raise (TypeError("other is not a ScriptureElement object.")) | ||
if self is other: | ||
return 0 | ||
|
||
if strict: | ||
res = self.position - other.position | ||
if res != 0: | ||
return res | ||
|
||
return (self.name > other.name) - (self.name < other.name) | ||
|
||
def __eq__(self, other: ScriptureElement) -> bool: | ||
if not isinstance(other, ScriptureElement): | ||
return NotImplemented | ||
|
||
return self.position == other.position and self.name == other.name | ||
|
||
def __lt__(self, other: ScriptureElement) -> bool: | ||
if not isinstance(other, ScriptureElement): | ||
return NotImplemented | ||
|
||
return self.compare_to(other) < 0 | ||
|
||
def __hash__(self) -> int: | ||
return hash((self.position, self.name)) | ||
|
||
def __repr__(self): | ||
if self.position == 0: | ||
return self.name | ||
return f"{self.position}:{self.name}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
from __future__ import annotations | ||
|
||
from functools import total_ordering | ||
from typing import List, Optional | ||
|
||
from ..scripture.constants import ENGLISH_VERSIFICATION | ||
from ..scripture.verse_ref import VerseRef, Versification, are_overlapping_verse_ranges | ||
from ..utils.comparable import Comparable | ||
from .scripture_element import ScriptureElement | ||
|
||
|
||
@total_ordering | ||
class ScriptureRef(Comparable): | ||
def __init__(self, ref: Optional[VerseRef] = None, path: Optional[List[ScriptureElement]] = None) -> None: | ||
self._verse_ref: VerseRef = ref if ref is not None else VerseRef() | ||
self._path: List[ScriptureElement] = path if path is not None else [] | ||
|
||
_empty: Optional[ScriptureRef] = None | ||
|
||
@classmethod | ||
def parse(cls, selection: str, versification: Optional[Versification] = None) -> ScriptureRef: | ||
parts: List[str] = selection.split("/") | ||
if len(parts) == 1: | ||
return cls( | ||
VerseRef.from_string(parts[0], versification if versification is not None else ENGLISH_VERSIFICATION) | ||
) | ||
vref: str = parts[0] | ||
path: List[ScriptureElement] = [] | ||
for part in parts[1:]: | ||
elem: List[str] = part.split(":") | ||
if len(elem) == 1: | ||
path.append(ScriptureElement(0, elem[0])) | ||
else: | ||
path.append(ScriptureElement(int(elem[0]), elem[1])) | ||
|
||
return cls( | ||
VerseRef.from_string(vref, versification if versification is not None else ENGLISH_VERSIFICATION), path | ||
) | ||
|
||
@property | ||
def verse_ref(self) -> VerseRef: | ||
return self._verse_ref | ||
|
||
@property | ||
def path(self) -> List[ScriptureElement]: | ||
return self._path | ||
|
||
@property | ||
def book_num(self) -> int: | ||
return self.verse_ref.book_num | ||
|
||
@property | ||
def chapter_num(self) -> int: | ||
return self.verse_ref.chapter_num | ||
|
||
@property | ||
def verse_num(self) -> int: | ||
return self.verse_ref.verse_num | ||
|
||
@property | ||
def book(self) -> str: | ||
return self.verse_ref.book | ||
|
||
@property | ||
def chapter(self) -> str: | ||
return self.verse_ref.chapter | ||
|
||
@property | ||
def verse(self) -> str: | ||
return self.verse_ref.verse | ||
|
||
@property | ||
def versification(self) -> Versification: | ||
return self.verse_ref.versification | ||
|
||
@property | ||
def is_empty(self) -> bool: | ||
return self.verse_ref.is_default | ||
|
||
@property | ||
def is_verse(self) -> bool: | ||
return VerseRef.verse_num != 0 and len(self.path) == 0 | ||
|
||
def change_versification(self, versification: Versification) -> ScriptureRef: | ||
vr: VerseRef = self.verse_ref.copy() | ||
vr.change_versification(versification) | ||
return ScriptureRef(vr, self.path) | ||
|
||
def overlaps(self, other: ScriptureRef) -> bool: | ||
if not are_overlapping_verse_ranges(self.verse_ref, other.verse_ref): | ||
return False | ||
return self.path == other.path | ||
|
||
def compare_to(self, other: object, compare_segments: bool = True, strict: bool = True): | ||
if not isinstance(other, ScriptureRef): | ||
raise TypeError("other is not a ScriptureRef object.") | ||
if self is other: | ||
return 0 | ||
|
||
res = self.verse_ref.compare_to(other.verse_ref, compare_segments=compare_segments) | ||
if res != 0: | ||
return res | ||
|
||
for se1, se2 in zip(self.path, other.path): | ||
res = se1.compare_to(se2, strict=strict) | ||
if res != 0: | ||
return res | ||
|
||
return len(self.path) - len(other.path) | ||
|
||
def __eq__(self, other: object) -> bool: | ||
if not isinstance(other, ScriptureRef): | ||
return NotImplemented | ||
return self.verse_ref == other.verse_ref and self.path == other.path | ||
|
||
def __lt__(self, other: object) -> bool: | ||
if not isinstance(other, ScriptureRef): | ||
return NotImplemented | ||
return self.compare_to(other) < 0 | ||
|
||
def __hash__(self) -> int: | ||
return hash((self.verse_ref, tuple(self.path))) | ||
|
||
def __repr__(self) -> str: | ||
return f"{self.verse_ref}/{'/'.join(str(se) for se in self.path)}" | ||
|
||
|
||
EMPTY_SCRIPTURE_REF = ScriptureRef() |
Oops, something went wrong.