Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
5db389f
1
Parent(s):
cdb1b8c
Redo format_response to be more serial
Browse files
app.py
CHANGED
@@ -133,59 +133,59 @@ def execute_request(ids: list[str]) -> list[Work]:
|
|
133 |
return [response[id_] for id_ in ids]
|
134 |
|
135 |
|
|
|
|
|
|
|
|
|
136 |
def format_response(neighbors: list[Work], distances: list[float]) -> str:
|
137 |
-
result_string =
|
138 |
for work, distance in zip(neighbors, distances):
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
if work.abstract:
|
150 |
-
abstract = work.abstract.replace('\n', '\\n').replace('\r', '\\r')
|
151 |
-
if len(abstract) > 2000:
|
152 |
-
abstract = abstract[:2000] + '...'
|
153 |
-
else: # edge case: no abstract
|
154 |
-
abstract = "No abstract"
|
155 |
-
|
156 |
-
# authors: truncate to 3 authors if necessary
|
157 |
-
if len(work.authors) >= 3:
|
158 |
-
authors_str = ', '.join(work.authors[:3]) + ', ...'
|
159 |
-
elif work.authors:
|
160 |
-
authors_str = ', '.join(work.authors)
|
161 |
-
else: # edge case: no authors
|
162 |
-
authors_str = "No author"
|
163 |
|
164 |
-
entry_string
|
165 |
|
166 |
-
if work.
|
167 |
-
entry_string +=
|
|
|
|
|
168 |
else:
|
169 |
-
entry_string +=
|
170 |
-
|
|
|
|
|
171 |
if work.journal_name:
|
172 |
-
entry_string +=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
else:
|
174 |
-
entry_string +=
|
175 |
-
|
176 |
-
entry_string +=
|
177 |
-
|
178 |
-
|
179 |
-
if work.citations:
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
entry_string += f'*Similarity: {distance:.2f}*'
|
188 |
-
entry_string += ' \n'
|
189 |
|
190 |
result_string += entry_string
|
191 |
|
|
|
133 |
return [response[id_] for id_ in ids]
|
134 |
|
135 |
|
136 |
+
def collapse_newlines(x: str) -> str:
|
137 |
+
return x.replace("\r\n", " ").replace("\n", " ").replace("\r", " ")
|
138 |
+
|
139 |
+
|
140 |
def format_response(neighbors: list[Work], distances: list[float]) -> str:
|
141 |
+
result_string = ""
|
142 |
for work, distance in zip(neighbors, distances):
|
143 |
+
entry_string = "## "
|
144 |
+
|
145 |
+
if work.title and work.doi:
|
146 |
+
entry_string += f"[{collapse_newlines(work.title)}]({work.doi})"
|
147 |
+
elif work.title:
|
148 |
+
entry_string += f"{collapse_newlines(work.title)}"
|
149 |
+
elif work.doi:
|
150 |
+
entry_string += f"[No title]({work.doi})"
|
151 |
+
else:
|
152 |
+
entry_string += "No title"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
+
entry_string += "\n\n**"
|
155 |
|
156 |
+
if len(work.authors) >= 3: # truncate to 3 if necessary
|
157 |
+
entry_string += ', '.join(work.authors[:3]) + ', ...'
|
158 |
+
elif work.authors:
|
159 |
+
entry_string += ', '.join(work.authors)
|
160 |
else:
|
161 |
+
entry_string += "No author"
|
162 |
+
|
163 |
+
entry_string += f", {work.year}"
|
164 |
+
|
165 |
if work.journal_name:
|
166 |
+
entry_string += " - " + work.journal_name
|
167 |
+
|
168 |
+
entry_string += "**\n\n"
|
169 |
+
|
170 |
+
if work.abstract:
|
171 |
+
abstract = collapse_newlines(work.abstract)
|
172 |
+
if len(abstract) > 2000:
|
173 |
+
abstract = abstract[:2000] + '...'
|
174 |
+
entry_string += abstract
|
175 |
else:
|
176 |
+
entry_string += "No abstract"
|
177 |
+
|
178 |
+
entry_string += "\n\n*"
|
179 |
+
|
180 |
+
meta: list[tuple[str, str]] = []
|
181 |
+
if work.citations: # don't tack "Cited-by count: 0" on someones's work
|
182 |
+
meta.append(("Cited-by count", str(work.citations)))
|
183 |
+
if work.doi:
|
184 |
+
meta.append(("DOI", work.doi.replace("https://doi.org/", "")))
|
185 |
+
meta.append(("Similarity", f"{distance:.2f}"))
|
186 |
+
entry_string += (" " * 4).join(": ".join(tup) for tup in meta)
|
187 |
+
|
188 |
+
entry_string += "*\n"
|
|
|
|
|
189 |
|
190 |
result_string += entry_string
|
191 |
|