File size: 2,410 Bytes
62977bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#
# Pyserini: Reproducible IR research with sparse and dense representations
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Implementations of support for specific collections.

import json


class Cord19Article:
    """Wrapper class for a raw JSON article from AI2's COVID-19 Open Research Dataset (CORD-19).

    Parameters
    ----------
    doc : str
        A JSON string of a CORD-19 article.
    """

    def __init__(self, doc):
        self.json = json.loads(doc)
        # Performs some basic error checking, throws an exception if user tries to instantiate with something
        # that isn't from CORD-19.
        if 'cord_uid' in self.json:
            self.full_text = False
        elif 'paper_id' in self.json:
            self.full_text = True
        else:
            raise TypeError

    def is_full_text(self):
        return self.json['has_full_text']

    def cord_uid(self):
        return self.json['cord_uid']

    def bib_entries(self):
        return self.json['bib_entries']

    def title(self):
        try:
            if self.is_full_text():
                return self.json['metadata']['title']
            else:
                return self.json['csv_metadata']['title']
        except KeyError:
            return ''

    def abstract(self):
        try:
            # For a full-text article, we can grab the abstract from two independent sources, the metadata or the
            # actual full text. Here, we make the decision to use the metadata, even for full text.
            return self.json['csv_metadata']['abstract']
        except KeyError:
            return ''

    def metadata(self):
        return self.json['csv_metadata']

    def body(self):
        try:
            if self.is_full_text():
                return [entry['text'] for entry in self.json['body_text']]
            else:
                return []
        except KeyError:
            return ''