Skip to content

Commit e15a1b5

Browse files
alyssachvastacopybara-github
authored andcommitted
Add topics to advanced_runner comments output
GitOrigin-RevId: 0b0adb3a60e5427c46487a900ca8b5d8e0f40291
1 parent 1928b63 commit e15a1b5

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

runner-cli/advanced_runner.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
import { Command } from "commander";
3737
import { writeFileSync } from "fs";
38-
import { getCommentsFromCsv, getSummary } from "./runner_utils";
38+
import { concatTopics, getCommentsFromCsv, getSummary } from "./runner_utils";
3939
import { MajoritySummaryStats } from "../src/stats/majority_vote";
4040
import { TopicStats } from "../src/stats/summary_stats";
4141
import { RelativeContext } from "../src/tasks/summarization_subtasks/relative_context";
@@ -58,6 +58,8 @@ interface CommentWithScores {
5858
id: string;
5959
text: string;
6060
votes?: VoteInfo;
61+
topics?: string;
62+
6163
agreeRate?: number;
6264
disagreeRate?: number;
6365
passRate?: number;
@@ -114,7 +116,9 @@ function getCommentsWithScores(
114116
id: comment.id,
115117
text: comment.text,
116118
votes: comment.voteInfo,
119+
topics: concatTopics(comment),
117120
};
121+
118122
if (comment.voteInfo) {
119123
const commentWithVoteInfo = comment as CommentWithVoteInfo;
120124
commentWithScores.passRate = getTotalPassRate(comment.voteInfo, USE_PROBABILITY_ESTIMATES);

runner-cli/categorization_runner.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { parse } from "csv-parse";
3333
import { createObjectCsvWriter } from "csv-writer";
3434
import * as fs from "fs";
3535
import * as path from "path";
36+
import { concatTopics } from "./runner_utils";
3637

3738
type CommentCsvRow = {
3839
"comment-id": string;
@@ -150,31 +151,6 @@ function setTopics(csvRows: CommentCsvRow[], categorizedComments: Comment[]): Co
150151
return csvRowsWithTopics;
151152
}
152153

153-
// Returns topics and subtopics concatenated together like
154-
// "Transportation:PublicTransit;Transportation:Parking;Technology:Internet"
155-
function concatTopics(comment: Comment): string {
156-
const pairsArray = [];
157-
for (const topic of comment.topics || []) {
158-
if ("subtopics" in topic) {
159-
for (const subtopic of topic.subtopics || []) {
160-
if ("subtopics" in subtopic) {
161-
if ("subtopics" in (subtopic as Topic)) {
162-
for (const subsubtopic of subtopic.subtopics as Topic[]) {
163-
pairsArray.push(`${topic.name}:${subtopic.name}:${subsubtopic.name}`);
164-
}
165-
}
166-
} else {
167-
pairsArray.push(`${topic.name}:${subtopic.name}`);
168-
}
169-
}
170-
} else {
171-
// handle case where no subtopics available
172-
pairsArray.push(`${topic.name}`);
173-
}
174-
}
175-
return pairsArray.join(";");
176-
}
177-
178154
async function writeCsv(csvRows: CommentCsvRow[], outputFile: string) {
179155
// Expect that all objects have the same keys, and make id match header title
180156
const header: { id: string; title: string }[] = [];

runner-cli/runner_utils.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,31 @@ export function writeSummaryToHtml(summary: Summary, outputFile: string) {
205205
console.log(`Written summary to ${outputFile}`);
206206
}
207207

208+
// Returns topics and subtopics concatenated together like
209+
// "Transportation:PublicTransit;Transportation:Parking;Technology:Internet"
210+
export function concatTopics(comment: Comment): string {
211+
const pairsArray = [];
212+
for (const topic of comment.topics || []) {
213+
if ("subtopics" in topic) {
214+
for (const subtopic of topic.subtopics || []) {
215+
if ("subtopics" in subtopic && (subtopic.subtopics as Topic[]).length) {
216+
if ("subtopics" in (subtopic as Topic)) {
217+
for (const subsubtopic of subtopic.subtopics as Topic[]) {
218+
pairsArray.push(`${topic.name}:${subtopic.name}:${subsubtopic.name}`);
219+
}
220+
}
221+
} else {
222+
pairsArray.push(`${topic.name}:${subtopic.name}`);
223+
}
224+
}
225+
} else {
226+
// handle case where no subtopics available
227+
pairsArray.push(`${topic.name}`);
228+
}
229+
}
230+
return pairsArray.join(";");
231+
}
232+
208233
/**
209234
* Parse a topics string from the categorization_runner.ts into a (possibly) nested topics
210235
* array, omitting subtopics and subsubtopics if not present in the labels.

0 commit comments

Comments
 (0)