Skip to content

Commit

Permalink
Add cli for categorization
Browse files Browse the repository at this point in the history
  • Loading branch information
Ackuq committed Sep 17, 2023
1 parent 3349979 commit 6d988c6
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 44 deletions.
44 changes: 44 additions & 0 deletions packages/party-data/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { select } from "@inquirer/prompts";
import { readNotCategorizedStandpoints, readSubjects } from ".";

async function categorize() {
const subjects = readSubjects();
const standpoints = readNotCategorizedStandpoints();

const subjectChoices = subjects.map((subject) => ({
value: subject.name,
}));

for (const standpoint of standpoints) {
await select({
message: `
Party: ${standpoint.party}
Title: ${standpoint.title}
Opinions:
${standpoint.opinions.map((opinion) => `\t${opinion}`).join("\n")}`,
choices: subjectChoices,
});
}
}

enum Action {
Categorize = "categorize",
}

async function main() {
const choice = await select({
message: "What do you want to do",
choices: [
{
value: Action.Categorize,
description: "Categorize uncategorized standpoints",
},
],
});
switch (choice) {
case Action.Categorize:
categorize();
}
}

main();
44 changes: 33 additions & 11 deletions packages/party-data/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import c from "./parties/c.json";
import v from "./parties/v.json";
import subjects from "./subjects.json";

enum Party {
S = "s",
SD = "sd",
M = "m",
MP = "mp",
L = "l",
KD = "kd",
C = "c",
V = "v",
}

export interface Subject {
name: string;
relatedSubjects: string[];
Expand All @@ -21,37 +32,48 @@ export interface Standpoint {
subject?: string;
}

export type StandpointWithParty = Standpoint & { party: Party };

export function getSubjects(): Subject[] {
return Object.values(subjects);
}

function getPartyData(abbreviation: string) {
switch (abbreviation.toLocaleLowerCase()) {
case "s":
case Party.S:
return s;
case "sd":
case Party.SD:
return sd;
case "m":
case Party.M:
return m;
case "mp":
case Party.MP:
return mp;
case "c":
case Party.C:
return c;
case "l":
case Party.L:
return l;
case "v":
case Party.V:
return v;
case "kd":
case Party.KD:
return kd;
}
throw new Error(`No such party ${abbreviation}`);
}

export function readPartyStandpoints(abbreviation: string): Standpoint[] {
return Object.values(getPartyData(abbreviation));
export function readPartyStandpoints(
abbreviation: Party,
): StandpointWithParty[] {
return Object.values(getPartyData(abbreviation)).map((standpoint) => ({
...standpoint,
party: abbreviation,
}));
}

export function readPartyDataForSubject(party: string, subjectName: string) {
export function readPartyDataForSubject(party: Party, subjectName: string) {
const partyData = readPartyStandpoints(party);
return partyData.filter((subject) => subject.subject === subjectName);
}

export function readAllStandpoints(): StandpointWithParty[] {
return Object.values(Party).map(readPartyStandpoints).flat();
}
29 changes: 6 additions & 23 deletions packages/party-data/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from "node:fs";
import type { Standpoint, Subject } from "./client";
import { readAllStandpoints, type Standpoint, type Subject } from "./client";

export interface PartyData {
[url: string]: Standpoint;
Expand Down Expand Up @@ -66,28 +66,6 @@ function readSubjectData() {
return JSON.parse(fs.readFileSync(SUBJECTS_FILE).toString()) as SubjectData;
}

function updateSubjectData(subjectData: SubjectData) {
fs.writeFileSync(SUBJECTS_FILE, JSON.stringify(subjectData, null, 2));
}

export function updateSubject(subject: Subject) {
const subjectData = readSubjectData();
subjectData[subject.name] = subject;
updateSubjectData(subjectData);
}

export function deleteSubject(subjectName: string) {
const subjectData = readSubjectData();
delete subjectData[subjectName];
updateSubjectData(subjectData);
}

export function createSubject(subject: Subject) {
const subjectData = readSubjectData();
subjectData[subject.name] = subject;
updateSubjectData(subjectData);
}

export function readSubjects(): Subject[] {
return Object.values(readSubjectData());
}
Expand All @@ -103,3 +81,8 @@ export function readPartyDataForSubject(party: string, subjectName: string) {
const partyData = readPartyData(party);
return partyData.filter((subject) => subject.subject === subjectName);
}

export function readNotCategorizedStandpoints() {
const standpoints = readAllStandpoints();
return standpoints.filter((standpoint) => standpoint.subject === undefined);
}
9 changes: 8 additions & 1 deletion packages/party-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"cli": "ts-node cli.ts"
},
"exports": {
".": "./index.ts",
"./client": "./client.ts"
},
"devDependencies": {
"@partiguiden/eslint-config-base": "workspace:*",
"@partiguiden/tsconfig": "workspace:*",
"@types/node": "20.6.0"
"@types/node": "20.6.0",
"ts-node": "^10.9.1"
},
"dependencies": {
"@inquirer/prompts": "^3.1.1"
}
}
Loading

0 comments on commit 6d988c6

Please sign in to comment.