Skip to content

Commit

Permalink
Use pylatenxenc to decode LaTeX from Author DB
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansick committed Feb 29, 2024
1 parent cc55156 commit c89bd3a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions src/templatebotaide/storage/authordb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from __future__ import annotations

import re
from dataclasses import dataclass
from typing import Dict

import aiohttp
import yaml
from pydantic import BaseModel, Field, RootModel
from pylatexenc.latex2text import LatexNodes2Text


class AuthorDbAuthor(BaseModel):
Expand Down Expand Up @@ -94,17 +96,15 @@ def create_from_db(
affiliation_id = ""
affiliation_name = ""
affiliation_address = ""
# Remove common LaTeX encodings
affiliation_address = affiliation_address.replace(r".\ ", ". ")

return cls(
author_id=author_id,
given_name=db_author.initials,
family_name=db_author.name,
given_name=Latex(db_author.initials).to_text(),
family_name=Latex(db_author.name).to_text(),
orcid=orcid,
affiliation_name=affiliation_name,
affiliation_name=Latex(affiliation_name).to_text(),
affiliation_id=affiliation_id,
affiliation_address=affiliation_address,
affiliation_address=Latex(affiliation_address).to_text(),
)


Expand Down Expand Up @@ -141,3 +141,16 @@ def get_author(self, author_id: str) -> AuthorInfo:
k: self._data.affiliations[k] for k in db_author.affil
}
return AuthorInfo.create_from_db(author_id, db_author, db_affiliations)


class Latex:
"""A class for handling LaTeX text content."""

def __init__(self, tex: str) -> None:
self.tex = tex

def to_text(self) -> str:
"""Convert LaTeX to text."""
text = LatexNodes2Text().latex_to_text(self.tex.strip())
# Remove running spaces inside the content
return re.sub(" +", " ", text)
2 changes: 1 addition & 1 deletion tests/test_authordb.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ def test_from_yaml(author_db_yaml: str) -> None:
assert author.author_id == "sickj"
assert (
author.affiliation_address
== "950 N. Cherry Ave., Tucson, AZ 85719, USA"
== "950 N. Cherry Ave., Tucson, AZ 85719, USA"
)
assert author.affiliation_name == "Rubin Observatory Project Office"

0 comments on commit c89bd3a

Please sign in to comment.