File size: 2,012 Bytes
2b54e33
ef3301e
2b54e33
 
ef3301e
2b54e33
 
 
ef3301e
 
 
 
 
2b54e33
ef3301e
2b54e33
 
 
 
ef3301e
2b54e33
 
ef3301e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b54e33
ef3301e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b54e33
 
ef3301e
 
 
 
 
 
 
 
 
 
 
 
 
2b54e33
ef3301e
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
import requests
import xml.etree.ElementTree as ET

GROBID_URL = (
    "https://grobid.science-miner.com/api/processFulltextDocument"
)


def extract_metadata_grobid(pdf_file):
    """
    Extract title, authors and abstract using GROBID.
    Returns a dictionary.
    """

    pdf_file.seek(0)

    response = requests.post(
        GROBID_URL,
        files={
            "input": pdf_file
        },
        headers={
            "Accept": "application/xml"
        },
        timeout=60
    )

    if response.status_code != 200:
        raise Exception("GROBID request failed")

    root = ET.fromstring(response.text)

    ns = {
        "tei": "http://www.tei-c.org/ns/1.0"
    }

    # ------------------------
    # Title
    # ------------------------

    title = ""

    title_node = root.find(
        ".//tei:titleStmt/tei:title",
        ns
    )

    if title_node is not None and title_node.text:
        title = title_node.text.strip()

    # ------------------------
    # Authors
    # ------------------------

    authors = []

    for author in root.findall(".//tei:author", ns):

        first = author.find(
            ".//tei:forename",
            ns
        )

        last = author.find(
            ".//tei:surname",
            ns
        )

        first_name = first.text.strip() if (
            first is not None and first.text
        ) else ""

        last_name = last.text.strip() if (
            last is not None and last.text
        ) else ""

        full_name = (
            first_name + " " + last_name
        ).strip()

        if full_name:
            authors.append(full_name)

    # ------------------------
    # Abstract
    # ------------------------

    abstract = ""

    abs_node = root.find(
        ".//tei:abstract",
        ns
    )

    if abs_node is not None:

        abstract = " ".join(
            abs_node.itertext()
        ).strip()

    return {

        "title": title,

        "authors": authors,

        "abstract": abstract

    }