forked from r-three/common-pile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace bs4 with lxml. Also using polars for the main computation.
- Loading branch information
Showing
4 changed files
with
111 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,27 @@ | ||
import bs4 | ||
from typing import Literal | ||
|
||
import lxml.html as html | ||
import requests | ||
|
||
|
||
def convert_mathml_to_latex(url: str, mathml_string: str) -> dict[str, str]: | ||
def convert_mathml_to_latex(url: str, mathml_string: str) -> str: | ||
"""Function to convert MathML to LaTeX using a REST server running https://github.com/asnunes/mathml-to-latex.""" | ||
if not mathml_string: | ||
return "" | ||
response = requests.post(url, json={"mathml": mathml_string}) | ||
result = response.json() | ||
return result | ||
|
||
if response.status_code in [400, 500]: | ||
return str(mathml_string) | ||
else: | ||
result = response.json() | ||
return result.get("latex", mathml_string) | ||
|
||
def format_text(url: str, example: dict) -> dict[str, str]: | ||
""" | ||
Formats each row to: | ||
Title\n\n | ||
ABSTRACT\n\n | ||
abstract_text\n\n | ||
description | ||
claims | ||
|
||
Returned fields: text, date, app_number | ||
""" | ||
output = "" | ||
if title := example.get("title_text"): | ||
output += title + "\n\n" | ||
if abstract := example.get("abstract_text"): | ||
output += ( | ||
"ABSTRACT" | ||
+ "\n\n" | ||
+ bs4.BeautifulSoup(abstract, "html.parser").get_text().strip() | ||
+ "\n\n" | ||
) | ||
if description := example.get("description_html"): | ||
description = bs4.BeautifulSoup(description, "html.parser") | ||
equations: list[bs4.element.Tag] = description.find_all("maths") | ||
if equations: | ||
for i, eq in enumerate(equations): | ||
new_equation = convert_mathml_to_latex(url, str(eq))["latex"] | ||
eq.string = new_equation | ||
output += description.get_text() | ||
if claims := example.get("claims_text"): | ||
claims = bs4.BeautifulSoup(claims, "html.parser") | ||
equations = claims.find_all("maths") | ||
if equations: | ||
for i, eq in enumerate(equations): | ||
new_equation = convert_mathml_to_latex(url, str(eq))["latex"] | ||
eq.string = new_equation | ||
output += claims.get_text().strip() | ||
return { | ||
"text": output, | ||
"date": str(example.get("publication_date")), | ||
"app_number": example.get("application_number"), | ||
} | ||
def parse_html(url, html_string: str) -> str: | ||
html_string = html.fromstring(html_string) | ||
equations: list[html.HtmlElement] = html_string.xpath("//maths") | ||
if equations: | ||
for i, eq in enumerate(equations): | ||
new_equation = convert_mathml_to_latex(url, str(eq)) | ||
eq.clear() | ||
eq.text = new_equation | ||
return html_string.text_content() |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
datasets | ||
bs4 | ||
polars | ||
lxml |
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