Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computing argument scores #6

Merged
merged 13 commits into from
Nov 29, 2024
Prev Previous commit
Next Next commit
Show argument scores on the graph (in an ugly way)
sofvanh committed Nov 29, 2024
commit 7d37de19ef7afc173300ed650b56db1c888e9cdd
18 changes: 17 additions & 1 deletion backend/src/db/operations/graphOperations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { query } from '../db';
import { generateGraphId } from '../idGenerator';
import { Argument, Edge, Graph } from '../../.shared/types';
import { getArgumentScores } from '../../analysis/argumentScoreHandler';

export async function createGraph(name: string, authorId: string): Promise<string> {
const id = generateGraphId();
@@ -60,14 +61,29 @@ export async function getGraphData(graphId: string, userId: string): Promise<Gra
});
}

// Get argument scores
// TODO: Make sure this is efficient
const argumentScores = await getArgumentScores(graphId);
const scoresMap = new Map(
argumentScores.map(score => [
score.argumentId,
{
consensus: score.consensusScore,
fragmentation: score.fragmentationScore,
clarity: score.clarityScore
}
])
);

const args: Argument[] = argumentsResult.rows.map((row: { id: string; graph_id: string; statement: string; embedding: number[], author_id: string }) => ({
id: row.id,
graphId: row.graph_id,
statement: row.statement,
embedding: row.embedding,
authorId: row.author_id,
reactionCounts: reactionCountsMap.get(row.id) || { agree: 0, disagree: 0, unclear: 0 },
userReaction: userReactionsMap.get(row.id)
userReaction: userReactionsMap.get(row.id),
score: scoresMap.get(row.id)
}));

const links: Edge[] = edgesResult.rows.map((row: { id: string; graph_id: string; source_id: string; target_id: string }) => ({
5 changes: 5 additions & 0 deletions frontend/src/shared/types.ts
Original file line number Diff line number Diff line change
@@ -21,6 +21,11 @@ export interface Argument {
disagree?: boolean;
unclear?: boolean;
};
score?: {
consensus: number;
fragmentation: number;
clarity: number;
};
}

// Edges are the connections between arguments
15 changes: 13 additions & 2 deletions frontend/src/views/GraphView.tsx
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ import { Argument, Graph } from '../shared/types';
import { Link, useParams } from 'react-router-dom';
import { useWebSocket } from '../contexts/WebSocketContext';
import { useAuth } from '../contexts/AuthContext';

import LoadingSpinner from '../components/LoadingSpinner';
import NodeInfoBox from '../components/NodeInfoBox';
import { buttonStyles } from '../styles/defaultStyles';
@@ -51,7 +50,19 @@ const GraphView: React.FC = () => {
useEffect(() => {
if (graph) {
document.title = `${graph.name} - MindMeld`;
const nodes: NodeData[] = graph.arguments.map(arg => ({ id: arg.id, name: arg.statement, argument: arg }));
const nodes: NodeData[] = graph.arguments.map(arg => {
const r = Math.round((arg.score?.consensus ?? 0) * 255);
const g = Math.round((arg.score?.fragmentation ?? 0) * 255);

return {
id: arg.id,
name: arg.statement,
color: arg.score
? `rgb(${r}, ${g}, 0)`
: '#cccccc',
argument: arg
};
});
const links: LinkData[] = graph.edges.map(edge => ({ source: edge.sourceId, target: edge.targetId }));
setGraphData({ nodes, links });
}