catch more forms

This commit is contained in:
Fraser
2023-03-31 15:16:12 -04:00
parent 5f98f10767
commit 30c9c7d727
+28 -1
View File
@@ -141,6 +141,8 @@ const Home: NextPage = () => {
const data = await res.json();
// ---------------------- normalize citation form ----------------------
// transform all things that look like [a, b, c] into [a][b][c]
let response = data.response.replace(
@@ -151,6 +153,30 @@ const Home: NextPage = () => {
.join("][")
)
// transform all things that look like [(a), (b), (c)] into [(a)][(b)][(c)]
response = response.replace(
/\[((?:\([a-z]+\),\s*)*\([a-z]+\))\]/g, // identify groups of this form
(block: string) => block.split(',')
.map((x) => x.trim())
.join("][")
)
// transform all things that look like [(a)] into [a]
response = response.replace(
/\[\(([a-z]+)\)\]/g,
(_match: string, x: string) => `[${x}]`
)
// transform all things that look like [ a ] into [a]
response = response.replace(
/\[\s*([a-z]+)\s*\]/g,
(_match: string, x: string) => `[${x}]`
)
// -------------- map citations from strings into numbers --------------
// figure out what citations are in the response, and map them appropriately
const cite_map = new Map<string, number>();
let cite_count = 0;
@@ -169,7 +195,8 @@ const Home: NextPage = () => {
response = response_copy + response.slice(response_copy.length);
// create the ordered citation array
// ----------------- create the ordered citation array -----------------
const citations = new Array<Citation>();
cite_map.forEach((value, key) => {
citations[value] = data.citations[key.charCodeAt(0) - 'a'.charCodeAt(0)];