text
stringlengths 1
93.6k
|
|---|
)
|
def process_entity(text: str, lang: str, len_limit: int) -> str | None:
|
if re.search(r"https?:|www\.", text, re.IGNORECASE):
|
return None
|
text = re.sub(r"^\W+", "", text)
|
text = re.sub(r"\W+$", "", text)
|
if lang == "en":
|
# ignore chapter title(chapter 1) and page number reference(pp. 1-10)
|
if re.match(r"c?hapter|p{1,2}[\W\d]{2,}", text, re.IGNORECASE):
|
return None
|
text = re.sub(r"\W+[sd]$|\s+of$", "", text)
|
text = re.sub(r"^(?:the|an?)\s", "", text, flags=re.IGNORECASE)
|
text = re.sub(r"^\W+", "", text)
|
if text.lower() in DIRECTIONS:
|
return None
|
elif lang == "es":
|
# https://en.wikipedia.org/wiki/Spanish_determiners#Articles
|
text = re.sub(
|
r"^(?:el|los?|las?|un|unos?|unas?)\s", "", text, flags=re.IGNORECASE
|
)
|
text = re.sub(r"^\W+", "", text)
|
# TODO https://en.wikipedia.org/wiki/Article_(grammar)#Tables
|
if len(text) < len_limit or re.fullmatch(r"[\W\d]+", text):
|
return None
|
return text
|
def find_named_entity(
|
start: int,
|
x_ray: X_Ray | EPUB,
|
doc: Any,
|
mobi_codec: str,
|
lang: str,
|
escaped_text: str | None,
|
custom_x_ray: CustomXDict,
|
xhtml_path: Path | None = None,
|
end: int = 0,
|
) -> list[Interval]:
|
len_limit = 2 if lang in CJK_LANGS else 3
|
starts = set()
|
intervals = []
|
for ent in filter(lambda x: x.label_ in NER_LABELS, doc.ents):
|
text = (
|
ent.ent_id_ # customized X-Ray
|
if ent.ent_id_
|
else process_entity(ent.text, lang, len_limit)
|
)
|
if text is None or (ent.ent_id_ and custom_x_ray.get(ent.ent_id_).omit):
|
continue
|
ent_text = ent.text if ent.ent_id_ else text
|
if escaped_text is not None:
|
result = index_in_escaped_text(ent_text, escaped_text, ent.start_char)
|
if result is None:
|
continue
|
start_char, end_char = result
|
if start_char is None:
|
continue
|
elif not ent.ent_id_:
|
start_char = ent.start_char + ent.text.index(ent_text)
|
end_char = start_char + len(ent_text)
|
else:
|
start_char = ent.start_char
|
end_char = ent.end_char
|
book_text = escaped_text if escaped_text else doc.text
|
selectable_text = book_text[start_char:end_char]
|
if start_char in starts:
|
continue
|
else:
|
starts.add(start_char)
|
if isinstance(x_ray, EPUB):
|
x_ray.add_entity(
|
text,
|
ent.label_,
|
ent.sent.text.strip(),
|
start,
|
end,
|
start_char,
|
end_char,
|
xhtml_path,
|
)
|
intervals.append(Interval(start_char, end_char - 1))
|
continue
|
# Include the next punctuation so the word can be selected on Kindle
|
if re.match(r"[^\w\s]", book_text[end_char : end_char + 1]):
|
selectable_text = book_text[start_char : end_char + 1]
|
if mobi_codec is not None and escaped_text is not None:
|
ent_start = start + len(escaped_text[:start_char].encode(mobi_codec))
|
ent_len = len(selectable_text.encode(mobi_codec))
|
else:
|
ent_start = start + start_char
|
ent_len = len(selectable_text)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.