Skip to content

Commit b4ab3f5

Browse files
committed
Tweak in API making each project of implicit cat appear a certain times
1 parent 69b4f42 commit b4ab3f5

File tree

2 files changed

+77
-32
lines changed

2 files changed

+77
-32
lines changed

src/flow/flow.service.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { PrismaService } from 'src/prisma.service';
1010
import {
1111
getPairwiseCombinations,
12+
shuffleArraySeeded,
1213
sortCombinationsByImplicitCategoryAndOccurance,
1314
} from 'src/utils';
1415
import {
@@ -699,8 +700,6 @@ export class FlowService {
699700
(a.implicitCategory || '').localeCompare(b.implicitCategory || ''),
700701
);
701702

702-
console.log(allProjects.slice(0, 10).map((item) => item.implicitCategory));
703-
704703
const projectStars = allStars.filter(
705704
(item) => !projectCoIs.find((el) => el.projectId === item.projectId),
706705
);
@@ -751,24 +750,36 @@ export class FlowService {
751750

752751
const allIds = allProjects.map((child) => child.id);
753752

754-
const projectIdOccurencesRanking = this.determineIdRanking(votedIds);
755-
756-
// ascending id rankings (i.e., the last element has been voted the most)
757-
let idRanking: number[] = [];
753+
let idRanking = this.determineIdRanking(votedIds);
758754

759755
for (let i = 0; i < allIds.length; i++) {
760756
const value = allIds[i];
761-
if (!projectIdOccurencesRanking.includes(value)) idRanking.push(value);
757+
if (!(value in idRanking)) idRanking = { ...idRanking, [value]: 0 };
762758
}
763759

764-
idRanking = [...idRanking, ...projectIdOccurencesRanking];
760+
const getProjectOccurances = (id: number) =>
761+
id in idRanking ? idRanking[id] : 0;
762+
763+
// idRanking = [...idRanking, ...projectIdOccurencesRanking];
765764

766765
const combinations = getPairwiseCombinations(allIds);
767766

767+
const implicitCategoryPriorities = Array.from(
768+
new Set(allProjects.map((item) => item.implicitCategory)),
769+
).map((cat, index) => ({ name: cat, priority: index * 2 }));
770+
771+
console.log(shuffleArraySeeded(implicitCategoryPriorities, userId));
772+
773+
const getImplicitCatScore = (cat: string) =>
774+
shuffleArraySeeded(implicitCategoryPriorities, userId).find(
775+
(el) => el.name === cat,
776+
)?.priority || 0;
777+
768778
const sortedCombinations = sortCombinationsByImplicitCategoryAndOccurance(
769779
combinations,
770-
idRanking,
780+
getProjectOccurances,
771781
getProjectImplicitCatById,
782+
getImplicitCatScore,
772783
);
773784

774785
const result = [];
@@ -1090,7 +1101,7 @@ export class FlowService {
10901101

10911102
private determineIdRanking = (
10921103
ids: number[],
1093-
order: 'ASC' | 'DSC' = 'ASC',
1104+
// order: 'ASC' | 'DSC' = 'ASC',
10941105
) => {
10951106
// Function to count the occurrences of an element in an array
10961107
const countOccurrences = (arr: number[], val: number) =>
@@ -1100,17 +1111,19 @@ export class FlowService {
11001111
const frequencyMap: Record<number, number> = {};
11011112
ids.forEach((i) => (frequencyMap[i] = countOccurrences(ids, i)));
11021113

1103-
// Create an array of unique elements
1104-
const uniqueElements = [...new Set(ids)];
1114+
return frequencyMap;
11051115

1106-
// Sort the unique elements array based on the frequency of each element
1107-
uniqueElements.sort((a, b) =>
1108-
order === 'ASC'
1109-
? frequencyMap[a] - frequencyMap[b]
1110-
: frequencyMap[b] - frequencyMap[a],
1111-
);
1116+
// // Create an array of unique elements
1117+
// const uniqueElements = [...new Set(ids)];
1118+
1119+
// // Sort the unique elements array based on the frequency of each element
1120+
// uniqueElements.sort((a, b) =>
1121+
// order === 'ASC'
1122+
// ? frequencyMap[a] - frequencyMap[b]
1123+
// : frequencyMap[b] - frequencyMap[a],
1124+
// );
11121125

1113-
return uniqueElements;
1126+
// return uniqueElements;
11141127
};
11151128

11161129
private validateVote = async (

src/utils/index.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,33 +105,65 @@ export const sortCombinationsByImplicitCategory = (
105105
// and least occurance (same sub-category is the first differentiator though)
106106
export const sortCombinationsByImplicitCategoryAndOccurance = (
107107
combinations: number[][],
108-
order: number[],
108+
getProjectOccurances: (id: number) => number,
109109
getImplicitCat: (id: number) => string,
110+
getImplicitCatScore: (cat: string) => number,
110111
) => {
111-
const getImplicitCatScore = (id1: number, id2: number) =>
112-
getImplicitCat(id1) === getImplicitCat(id2) ? 3 : -3;
113-
114-
const getOccuranceScore = (item: number) =>
115-
order.findIndex((el) => el === item);
112+
const compareImplicitCat = (id1: number, id2: number) =>
113+
getImplicitCat(id1) === getImplicitCat(id2) ? 1 : -1;
116114

117115
const sorted = [...combinations];
118116

119117
sorted.sort((c1, c2) => {
120118
const c1Score =
121-
getImplicitCatScore(c1[0], c1[1]) -
122-
getOccuranceScore(c1[0]) -
123-
getOccuranceScore(c1[1]);
119+
getImplicitCatScore(getImplicitCat(c1[0])) +
120+
getImplicitCatScore(getImplicitCat(c1[1])) +
121+
compareImplicitCat(c1[0], c1[1]) -
122+
getProjectOccurances(c1[0]) -
123+
getProjectOccurances(c1[1]);
124124
const c2Score =
125-
getImplicitCatScore(c2[0], c2[1]) -
126-
getOccuranceScore(c2[0]) -
127-
getOccuranceScore(c2[1]);
128-
125+
getImplicitCatScore(getImplicitCat(c2[0])) +
126+
getImplicitCatScore(getImplicitCat(c2[1])) +
127+
compareImplicitCat(c2[0], c2[1]) -
128+
getProjectOccurances(c2[0]) -
129+
getProjectOccurances(c2[1]);
130+
131+
console.log('score of', c1, '=', c1Score);
132+
console.log('score of', c2, '=', c2Score);
129133
return c2Score - c1Score;
130134
});
131135

132136
return sorted;
133137
};
134138

139+
// Seeded random number generator
140+
class SeededRandom {
141+
private seed: number;
142+
143+
constructor(seed: number) {
144+
this.seed = seed;
145+
}
146+
147+
// Generate a random number between 0 and 1
148+
random(): number {
149+
const x = Math.sin(this.seed++) * 10000;
150+
return x - Math.floor(x);
151+
}
152+
}
153+
154+
// Fisher-Yates shuffle algorithm
155+
export function shuffleArraySeeded<T>(array: T[], seed: number): T[] {
156+
const shuffled = [...array];
157+
const random = new SeededRandom(seed);
158+
159+
for (let i = shuffled.length - 1; i > 0; i--) {
160+
const j = Math.floor(random.random() * (i + 1));
161+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
162+
}
163+
164+
return shuffled;
165+
}
166+
135167
export const sortProjectId = (
136168
project1Id: number,
137169
project2Id: number,

0 commit comments

Comments
 (0)