Skip to content

Commit 71497bf

Browse files
committed
add references in the output
1 parent 3fc43db commit 71497bf

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

grobid_client/format/TEI2LossyJSON.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def convert_tei_file(self, tei_file: Union[Path, BinaryIO], stream: bool = False
6464
document['body_text'] = text_structure
6565
figures_and_tables = []
6666
document['figures_and_tables'] = figures_and_tables
67+
references_structure = []
68+
document['references'] = references_structure
6769

6870
# Populate header and body using the same traversal used by the generator
6971
for child in soup.TEI.children:
@@ -216,6 +218,74 @@ def convert_tei_file(self, tei_file: Union[Path, BinaryIO], stream: bool = False
216218
}
217219
)
218220

221+
# Extract references from listBibl
222+
list_bibl = soup.find("listBibl")
223+
if list_bibl:
224+
for i, bibl_struct in enumerate(list_bibl.find_all("biblStruct"), 1):
225+
ref_data = OrderedDict()
226+
ref_data['id'] = f"b{i}"
227+
228+
# Extract title
229+
title_node = bibl_struct.find("title", attrs={"level": "a"})
230+
if title_node:
231+
ref_data['title'] = title_node.get_text().strip()
232+
233+
# Extract authors
234+
authors = []
235+
for author in bibl_struct.find_all("author"):
236+
forename = author.find('forename')
237+
surname = author.find('surname')
238+
239+
if forename and surname:
240+
author_name = f"{forename.get_text().strip()} {surname.get_text().strip()}"
241+
elif surname:
242+
author_name = surname.get_text().strip()
243+
elif forename:
244+
author_name = forename.get_text().strip()
245+
else:
246+
continue
247+
248+
if author_name.strip():
249+
authors.append(author_name.strip())
250+
251+
if authors:
252+
ref_data['authors'] = authors
253+
254+
# Extract publication info (journal, year, etc.)
255+
# Look for journal title
256+
journal_node = bibl_struct.find("title", attrs={"level": "j"})
257+
if journal_node:
258+
ref_data['journal'] = journal_node.get_text().strip()
259+
260+
# Extract year
261+
date_node = bibl_struct.find("date")
262+
if date_node:
263+
year_text = date_node.get_text().strip()
264+
if year_text and year_text.isdigit():
265+
ref_data['year'] = int(year_text)
266+
else:
267+
# Try to extract year from text
268+
import re
269+
year_match = re.search(r'\b(19|20)\d{2}\b', year_text)
270+
if year_match:
271+
ref_data['year'] = int(year_match.group())
272+
273+
# Extract DOI
274+
idno_doi = bibl_struct.find("idno", type="DOI")
275+
if idno_doi:
276+
ref_data['doi'] = idno_doi.get_text().strip()
277+
278+
# Extract pages
279+
pages_node = bibl_struct.find("biblScope", attrs={"unit": "page"})
280+
if pages_node:
281+
ref_data['pages'] = pages_node.get_text().strip()
282+
283+
# Get raw text as fallback
284+
if not ref_data.get('title'):
285+
ref_data['raw_text'] = bibl_struct.get_text().strip()
286+
287+
references_structure.append(ref_data)
288+
219289
return document
220290

221291
def _iter_passages_from_soup(self, soup: BeautifulSoup, passage_level: str) -> Iterator[Dict[str, Union[str, Dict[str, str]]]]:

0 commit comments

Comments
 (0)