File size: 4,764 Bytes
9ee83a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# code modified from https://github.com/IBM/document2slides/blob/main/sciduet-build/extract_papers.py
import os
from os import path
import json
from dataclasses import dataclass
from multiprocessing.pool import Pool
import pandas as pd
from pathlib import Path
from bs4 import BeautifulSoup
import nltk
from dataclasses import dataclass


@dataclass
class Person:
    firstname: str
    middlename: str
    surname: str


def read_tei(tei_file):
    with open(tei_file, 'r', encoding='utf-8') as tei:
        soup = BeautifulSoup(tei, 'xml')
        return soup


def elem_to_text(elem, default=''):
    if elem:
        return elem.getText()
    else:
        return default


class TEIFile(object):

    def __init__(self, xml_pth: str = None, fig_json_pth: str = ''):
        # self.filename = filename
        # self.dir = os.path.abspath(paper_dir)
        self.xml_pth = os.path.abspath(xml_pth)
        self.fig_json_pth = os.path.abspath(fig_json_pth)
        # self.soup = read_tei(os.path.join(self.dir, 'paper.tei.xml'))
        self.soup = read_tei(self.xml_pth)
        self._text = None
        self._title = ''
        self._abstract = ''
        self._headers = None
        self._figures = None

    @property
    def doi(self):
        idno_elem = self.soup.find('idno', type='DOI')
        if not idno_elem:
            return ''
        else:
            return idno_elem.getText()

    @property
    def title(self):
        if not self._title:
            self._title = self.soup.title.getText()
        return self._title

    @property
    def abstract(self):
        if not self._abstract:
            abstract = self.soup.abstract.getText(separator=' ', strip=True)
            self._abstract = abstract
        return self._abstract

    @property
    def authors(self):
        authors_in_header = self.soup.analytic.find_all('author')

        # print(authors_in_header)

        result = []
        for author in authors_in_header:
            persname = author.persName
            if not persname:
                persname = author.persname
            if not persname:
                continue
            firstname = elem_to_text(persname.find("forename", type="first"))
            middlename = elem_to_text(persname.find("forename", type="middle"))
            surname = elem_to_text(persname.surname)
            person = Person(firstname, middlename, surname)
            result.append(person)

        return result

    @property
    def text(self):
        if not self._text:
            self._headers = []
            headerlist = self.soup.body.find_all("head")
            sections = []
            print(headerlist)
            for head in headerlist:
                if head.parent.name == 'div':
                    txt = head.parent.get_text(separator=' ', strip=True)
                    # the following is only valid for arabic numerals...
                    if head.get("n"):
                        sections.append([head.text, head.get('n'), txt])
                    else:
                        if len(sections) == 0:
                            print("Grobid processing error.")
                        sections[-1][2] += txt
                    # sections.append([head.text, 'invalid n', txt])
            start = 0
            for i in sections:
                sent = nltk.tokenize.sent_tokenize(i[2])
                sec_dic = {'section': i[0], 'n': i[1], 'start': start, 'end': start + len(sent) - 1}
                self._headers.append(sec_dic)
                start += len(sent)
            plain_text = " ".join([i[2] for i in sections])
            self._text = [{'id': i, 'string': s} for i, s in enumerate(nltk.tokenize.sent_tokenize(plain_text))]
        return self._text

    @property
    def headers(self):
        if not self._headers:
            self.text()
        return self._headers

    @property
    def figures(self):
        if not self._figures:
            # base_name = basename_without_ext(self.filename)
            self._figures = []
            # fn = 'figures/{}.json'.format(base_name)  # link to figures dir
            # fn = os.path.join(self.dir, 'paper_figure/paper.json')
            fn = self.fig_json_pth
            if not path.isfile(fn):
                return []
            with open(fn) as f:
                data = json.load(f)
            for i in data:
                elem = {'filename': i['renderURL'], 'caption': i['caption'], 'page': i['page'], 'bbox': i['regionBoundary']}
                self._figures.append(elem)
        return self._figures


def single_entry(uuid: str, xml_pth: str, fig_json_pth: str):
    tei = TEIFile(xml_pth=xml_pth, fig_json_pth=fig_json_pth)
    return uuid, tei.title, tei.abstract, tei.text, tei.headers, tei.figures