@@ -2,61 +2,65 @@ import { getReactionsForAnalysis } from "../db/operations/reactionOperations";
22import { cosineSimilarityMatrix , computeAllSums } from "../utils/math" ;
33
44export interface ReactionAnalysis {
5- userIndexMap : Map < string , number > ;
6- argumentIndexMap : Map < string , number > ;
7- votingMatrix : number [ ] [ ] ;
8- unclearMatrix : number [ ] [ ] ;
9- uniquenessMatrix : number [ ] [ ] ;
10- sum_pos_pos : number [ ] [ ] ;
11- sum_pos_neg : number [ ] [ ] ;
12- sum_neg_pos : number [ ] [ ] ;
13- sum_neg_neg : number [ ] [ ] ;
5+ userIndexMap : Map < string , number > ;
6+ argumentIndexMap : Map < string , number > ;
7+ votingMatrix : number [ ] [ ] ;
8+ unclearMatrix : number [ ] [ ] ;
9+ uniquenessMatrix : number [ ] [ ] ;
10+ sum_pos_pos : number [ ] [ ] ;
11+ sum_pos_neg : number [ ] [ ] ;
12+ sum_neg_pos : number [ ] [ ] ;
13+ sum_neg_neg : number [ ] [ ] ;
1414}
1515
1616export async function analyzeReactions ( graphId : string ) : Promise < ReactionAnalysis > {
17- const { reactions } = await getReactionsForAnalysis ( graphId ) ;
18- const userIndexMap = new Map < string , number > ( ) ;
19- const argumentIndexMap = new Map < string , number > ( ) ;
20- reactions . forEach ( reaction => {
21- if ( ! userIndexMap . has ( reaction . userId ) ) {
22- userIndexMap . set ( reaction . userId , userIndexMap . size ) ;
23- }
24- if ( ! argumentIndexMap . has ( reaction . argumentId ) ) {
25- argumentIndexMap . set ( reaction . argumentId , argumentIndexMap . size ) ;
26- }
27- } ) ;
17+ const { reactions } = await getReactionsForAnalysis ( graphId ) ;
18+ const userIndexMap = new Map < string , number > ( ) ;
19+ const argumentIndexMap = new Map < string , number > ( ) ;
20+ reactions . forEach ( reaction => {
21+ if ( ! userIndexMap . has ( reaction . userId ) ) {
22+ userIndexMap . set ( reaction . userId , userIndexMap . size ) ;
23+ }
24+ if ( ! argumentIndexMap . has ( reaction . argumentId ) ) {
25+ argumentIndexMap . set ( reaction . argumentId , argumentIndexMap . size ) ;
26+ }
27+ } ) ;
2828
29- const userCount = userIndexMap . size ;
30- const argumentCount = argumentIndexMap . size ;
31- const votingMatrix = new Array ( userCount ) . fill ( 0 ) . map ( ( ) => new Array ( argumentCount ) . fill ( 0 ) ) ;
32- const unclearMatrix = new Array ( userCount ) . fill ( 0 ) . map ( ( ) => new Array ( argumentCount ) . fill ( 0 ) ) ;
33- for ( const reaction of reactions ) {
34- const userIdx = userIndexMap . get ( reaction . userId ) ! ;
35- const argumentIdx = argumentIndexMap . get ( reaction . argumentId ) ! ;
36- votingMatrix [ userIdx ] [ argumentIdx ] = reaction . voteValue ;
37- unclearMatrix [ userIdx ] [ argumentIdx ] = reaction . unclearValue ;
29+ const userCount = userIndexMap . size ;
30+ const argumentCount = argumentIndexMap . size ;
31+ const votingMatrix = new Array ( userCount ) . fill ( 0 ) . map ( ( ) => new Array ( argumentCount ) . fill ( 0 ) ) ;
32+ const unclearMatrix = new Array ( userCount ) . fill ( 0 ) . map ( ( ) => new Array ( argumentCount ) . fill ( 0 ) ) ;
33+ for ( const reaction of reactions ) {
34+ const userIdx = userIndexMap . get ( reaction . userId ) ! ;
35+ const argumentIdx = argumentIndexMap . get ( reaction . argumentId ) ! ;
36+ if ( reaction . voteValue !== 0 ) {
37+ votingMatrix [ userIdx ] [ argumentIdx ] = reaction . voteValue ;
3838 }
39+ if ( reaction . unclearValue !== 0 ) {
40+ unclearMatrix [ userIdx ] [ argumentIdx ] = reaction . unclearValue ;
41+ }
42+ }
3943
40- const userSimilarityMatrix : number [ ] [ ] = cosineSimilarityMatrix ( votingMatrix ) ;
41- const { sum_pos_pos, sum_pos_neg, sum_neg_pos, sum_neg_neg } = computeAllSums ( userSimilarityMatrix , votingMatrix ) ;
42- const uniquenessMatrix = votingMatrix . map ( ( row , i ) => row . map ( ( value , j ) => {
43- const sumIngroupAgree = sum_pos_pos [ i ] [ j ] ;
44- const sumIngroupDisagree = sum_pos_neg [ i ] [ j ] ;
45- const sumIngroupNoVote = votingMatrix [ i ] [ j ] === 0 ? 1 : 0 ;
44+ const userSimilarityMatrix : number [ ] [ ] = cosineSimilarityMatrix ( votingMatrix ) ;
45+ const { sum_pos_pos, sum_pos_neg, sum_neg_pos, sum_neg_neg } = computeAllSums ( userSimilarityMatrix , votingMatrix ) ;
46+ const uniquenessMatrix = votingMatrix . map ( ( row , i ) => row . map ( ( value , j ) => {
47+ const sumIngroupAgree = sum_pos_pos [ i ] [ j ] ;
48+ const sumIngroupDisagree = sum_pos_neg [ i ] [ j ] ;
49+ const sumIngroupNoVote = votingMatrix [ i ] [ j ] === 0 ? 1 : 0 ;
4650
47- const sumIngroup = sumIngroupAgree + sumIngroupDisagree + sumIngroupNoVote ;
48- return 1 / sumIngroup ;
49- } ) ) ;
51+ const sumIngroup = sumIngroupAgree + sumIngroupDisagree + sumIngroupNoVote ;
52+ return 1 / sumIngroup ;
53+ } ) ) ;
5054
51- return {
52- userIndexMap,
53- argumentIndexMap,
54- votingMatrix,
55- unclearMatrix,
56- uniquenessMatrix,
57- sum_pos_pos,
58- sum_pos_neg,
59- sum_neg_pos,
60- sum_neg_neg
61- } ;
62- }
55+ return {
56+ userIndexMap,
57+ argumentIndexMap,
58+ votingMatrix,
59+ unclearMatrix,
60+ uniquenessMatrix,
61+ sum_pos_pos,
62+ sum_pos_neg,
63+ sum_neg_pos,
64+ sum_neg_neg
65+ } ;
66+ }
0 commit comments