|
19 | 19 |
|
20 | 20 | import { Sensemaker } from "../src/sensemaker";
|
21 | 21 | import { VertexModel } from "../src/models/vertex_model";
|
22 |
| -import { Summary, VoteTally, Comment, SummarizationType, Topic } from "../src/types"; |
| 22 | +import { |
| 23 | + Summary, |
| 24 | + VoteTally, |
| 25 | + Comment, |
| 26 | + SummarizationType, |
| 27 | + Topic, |
| 28 | + SummaryContent, |
| 29 | +} from "../src/types"; |
23 | 30 | import * as path from "path";
|
24 | 31 | import * as fs from "fs";
|
25 | 32 | import { parse } from "csv-parse";
|
| 33 | +import { marked } from "marked"; |
| 34 | +import { createObjectCsvWriter } from "csv-writer"; |
26 | 35 |
|
27 | 36 | /**
|
28 | 37 | * Core comment columns, sans any vote tally rows
|
@@ -56,6 +65,62 @@ export interface VoteTallyCsvRow {
|
56 | 65 | //This is a type that combines VoteTallyCsvRow and CoreCommentCsvRow
|
57 | 66 | export type CommentCsvRow = VoteTallyCsvRow & CoreCommentCsvRow;
|
58 | 67 |
|
| 68 | +/** |
| 69 | + * Add the text and supporting comments to statementsWithComments. Also adds nested content. |
| 70 | + * @param summaryContent the content and subcontent to add |
| 71 | + * @param allComments all the comments from the deliberation |
| 72 | + * @param statementsWithComments where to add new text and supporting comments |
| 73 | + * @returns none |
| 74 | + */ |
| 75 | +function addStatement( |
| 76 | + summaryContent: SummaryContent, |
| 77 | + allComments: Comment[], |
| 78 | + statementsWithComments: { summary: string; comments: string }[] |
| 79 | +) { |
| 80 | + if (summaryContent.subContents) { |
| 81 | + summaryContent.subContents.forEach((subContent) => { |
| 82 | + addStatement(subContent, allComments, statementsWithComments); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + if (summaryContent.text.length === 0 && !summaryContent.title) { |
| 87 | + return; |
| 88 | + } |
| 89 | + let comments: Comment[] = []; |
| 90 | + if (summaryContent.citations) { |
| 91 | + comments = summaryContent.citations |
| 92 | + .map((commentId: string) => allComments.find((comment: Comment) => comment.id === commentId)) |
| 93 | + .filter((comment) => comment !== undefined); |
| 94 | + } |
| 95 | + statementsWithComments.push({ |
| 96 | + summary: (summaryContent.title || "") + summaryContent.text, |
| 97 | + comments: comments.map((comment) => `* [${comment.id}] ${comment.text}`).join("\n"), |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Outputs a CSV where each row represents a statement and its associated comments. |
| 103 | + * |
| 104 | + * @param summary the summary to split. |
| 105 | + * @param outputFilePath Path to the output CSV file that will have columns "summary" for the statement, and "comments" for the comment texts associated with that statement. |
| 106 | + */ |
| 107 | +export function writeSummaryToGroundedCSV(summary: Summary, outputFilePath: string) { |
| 108 | + const statementsWithComments: { summary: string; comments: string }[] = []; |
| 109 | + |
| 110 | + for (const summaryContent of summary.contents) { |
| 111 | + addStatement(summaryContent, summary.comments, statementsWithComments); |
| 112 | + } |
| 113 | + |
| 114 | + const csvWriter = createObjectCsvWriter({ |
| 115 | + path: outputFilePath, |
| 116 | + header: [ |
| 117 | + { id: "summary", title: "summary" }, |
| 118 | + { id: "comments", title: "comments" }, |
| 119 | + ], |
| 120 | + }); |
| 121 | + csvWriter.writeRecords(statementsWithComments); |
| 122 | + console.log(`Summary statements saved to ${outputFilePath}`); |
| 123 | +} |
59 | 124 | /**
|
60 | 125 | * Identify topics and subtopics when input data has not already been categorized.
|
61 | 126 | * @param project The Vertex GCloud project name
|
@@ -99,6 +164,55 @@ export async function getSummary(
|
99 | 164 | );
|
100 | 165 | }
|
101 | 166 |
|
| 167 | +// Gets existing topics from comments or learns them if not provided. |
| 168 | +export async function getTopics(comments: Comment[], vertexProject: string): Promise<Topic[]> { |
| 169 | + if (comments.length > 0 && comments.some((comment) => comment.topics)) { |
| 170 | + console.log("Comments already have topics. Skipping topic learning."); |
| 171 | + return getTopicsFromComments(comments); |
| 172 | + } else { |
| 173 | + console.log("Learning topics from comments."); |
| 174 | + return await getTopicsAndSubtopics(vertexProject, comments); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +export function writeSummaryToHtml(summary: Summary, outputFile: string) { |
| 179 | + const markdownContent = summary.getText("MARKDOWN"); |
| 180 | + const htmlContent = ` |
| 181 | +<!DOCTYPE html> |
| 182 | +<html> |
| 183 | +<head> |
| 184 | + <title>Summary</title> |
| 185 | + <style> |
| 186 | + body { |
| 187 | + font-family: Arial, sans-serif; |
| 188 | + line-height: 1.6; |
| 189 | + max-width: 800px; |
| 190 | + margin: 0 auto; |
| 191 | + padding: 20px; |
| 192 | + } |
| 193 | + </style> |
| 194 | + ${ |
| 195 | + // When in DEBUG_MODE, we need to add the DataTables and jQuery libraries, and hook |
| 196 | + // into our table elements to add support for features like sorting and search. |
| 197 | + process.env.DEBUG_MODE === "true" |
| 198 | + ? ` |
| 199 | + <script src="https://code.jquery.com/jquery-3.7.1.js"></script> |
| 200 | + <script src="https://cdn.datatables.net/2.2.1/js/dataTables.js"></script> |
| 201 | + <link rel="stylesheet" href="https://cdn.datatables.net/2.2.1/css/dataTables.dataTables.css" /> |
| 202 | + <script>$(document).ready( function () {$('table').DataTable();} )</script> |
| 203 | + ` |
| 204 | + : "" |
| 205 | + } |
| 206 | +</head> |
| 207 | +<body> |
| 208 | + ${marked(markdownContent)} |
| 209 | +</body> |
| 210 | +</html>`; |
| 211 | + |
| 212 | + fs.writeFileSync(outputFile, htmlContent); |
| 213 | + console.log(`Written summary to ${outputFile}`); |
| 214 | +} |
| 215 | + |
102 | 216 | /**
|
103 | 217 | * Parse a topics string from the categorization_runner.ts into a (possibly) nested topics and subtopics
|
104 | 218 | * array, omitting subtopics if not present in the labels.
|
|
0 commit comments