-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
paper_record.py
183 lines (158 loc) · 5.29 KB
/
paper_record.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os
import pandas as pd
from asreview.config import LABEL_NA
from asreview.utils import format_to_str
class PaperRecord():
"""A single record from a paper in a systematic review.
Arguments
---------
record_id: int
Some identifier for this record.
title: str
Paper title.
abstract: str
Paper abstract.
authors: str, list
Authors of the paper.
keywords: str, list
Keywords of the paper.
label: int
Current label of the paper. No label is indicated by
asreview.config.LABEL_NA (== -1).
kwargs: dict
Any extra keyword arguments will be put in self.extra_fields.
"""
def __init__(self, record_id, column_spec={}, **kwargs):
for attr in ["title", "abstract", "authors", "keywords",
"final_included"]:
if attr in column_spec:
col = column_spec[attr]
elif attr in kwargs:
col = attr
else:
col = None
setattr(self, attr, kwargs.pop(col, None))
self.record_id = record_id
if self.final_included is None:
self.final_included = LABEL_NA
else:
self.final_included = int(self.final_included)
self.extra_fields = kwargs
for attr, val in self.extra_fields.items():
if pd.isna(val):
self.extra_fields[attr] = None
def preview(self, w_title=80, w_authors=40, automatic_width=False):
"""Return a single line preview string for record i.
Arguments
---------
w_title: int
Width to be allocated for the title of the paper.
w_authors: int
Width to be allocated for the authors of the paper.
automatic_width: bool
If true, compute w_title, w_authors from the console width.
Returns
-------
str:
A string that previews a paper record.
"""
if automatic_width:
term_width = os.get_terminal_size().columns
width_available = term_width-7
w_title = round((2/3)*width_available)
w_authors = width_available - w_title
title_str = ""
author_str = ""
heading = self.title
if heading is None:
heading = self.abstract
if heading is not None:
if len(heading) > w_title:
title_str = heading[:w_title - 2] + ".."
else:
title_str = heading
if self.authors is not None:
cur_authors = format_to_str(self.authors)
if len(cur_authors) > w_authors:
author_str = cur_authors[:w_authors - 2] + ".."
else:
author_str = cur_authors
format_str = "{0: <" + str(w_title) + "} " + "{1: <" + str(w_authors)
format_str += "}"
prev_str = format_str.format(title_str, author_str)
return prev_str
def format(self, use_cli_colors=True):
"""Format one record for displaying in the CLI.
Arguments
---------
use_cli_colors: bool
Some terminals support colors, set to True to use them.
Returns
-------
str:
A string including title, abstracts and authors.
"""
if self.title is not None:
title = self.title
if use_cli_colors:
title = "\033[95m" + title + "\033[0m"
title += "\n"
else:
title = ""
if self.authors is not None and len(self.authors) > 0:
authors = format_to_str(self.authors) + "\n"
else:
authors = ""
if self.abstract is not None and len(self.abstract) > 0:
abstract = self.abstract
abstract = "\n" + abstract + "\n"
else:
abstract = ""
return ("\n\n----------------------------------"
f"\n{title}{authors}{abstract}"
"----------------------------------\n\n")
def print(self, *args, **kwargs):
"Print a record to the console."
print(self.format(*args, **kwargs))
@property
def text(self):
"""Create a single string from title + abstract.
Returns
-------
str:
Concatenated string from title + abstract.
"""
title = self.title
abstract = self.abstract
if title is None:
title = ""
if abstract is None:
abstract = ""
return title + " " + abstract
@property
def heading(self):
"""Return the title of the paper."""
if self.title is None:
return ""
return self.title
@property
def body(self):
"""Return the abstract of the paper."""
if self.abstract is None:
return ""
return self.abstract
def todict(self):
"""Create dictionary from the record."""
label = self.label
if self.label is LABEL_NA:
label = None
paper_dict = {
"title": self.title,
"abstract": self.abstract,
"authors": self.authors,
"keywords": self.keywords,
"record_id": self.record_id,
"label": label,
}
paper_dict.update(self.extra_fields)
return paper_dict