colonelwatch commited on
Commit
5db389f
·
1 Parent(s): cdb1b8c

Redo format_response to be more serial

Browse files
Files changed (1) hide show
  1. app.py +46 -46
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
- # edge cases: no title
140
- abstract = work.abstract if work.abstract is not None else "No abstract"
141
-
142
- # title: knock out escape sequences
143
- if work.title is not None:
144
- title = work.title.replace('\n', '\\n').replace('\r', '\\r')
145
- else: # edge case: no title
146
- title = "No title"
147
-
148
- # abstract: knock out escape sequences, then truncate to 2000 chars
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.doi: # edge case: for now, no doi -> no link
167
- entry_string += f'## [{title}]({work.doi})\n\n'
 
 
168
  else:
169
- entry_string += f'## {title}\n\n'
170
-
 
 
171
  if work.journal_name:
172
- entry_string += f'**{authors_str} - {work.journal_name}, {work.year}**\n\n'
 
 
 
 
 
 
 
 
173
  else:
174
- entry_string += f'**{authors_str}, {work.year}**\n\n'
175
-
176
- entry_string += f'{abstract}\n\n'
177
-
178
- # edge case: we shouldn't tack "Cited-by count: 0" onto someone's paper
179
- if work.citations:
180
- entry_string += f'*Cited-by count: {work.citations}*'
181
- entry_string += '    '
182
-
183
- if work.doi: # list the doi if it exists
184
- entry_string += f'*DOI: {work.doi.replace("https://doi.org/", "")}*'
185
- entry_string += '    '
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