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

fix: catch errors in weight Ajust #2

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions dist/features/coreServerFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
async function coreServerFeature(api, user) {
let results = [];
let pages = 10;
for await (const page of api.v1.accounts.$select(user.id).following.list({ limit: 80 })) {
results = results.concat(page);
pages--;
if (pages === 0 || results.length < 80) {
break;
try {
for await (const page of api.v1.accounts.$select(user.id).following.list({ limit: 80 })) {
results = results.concat(page);
pages--;
if (pages === 0 || results.length < 80) {
break;
}
}
}
catch (e) {
return {};
}
const serverFrequ = results.reduce((accumulator, follower) => {
const server = follower.url.split("@")[0].split("https://")[1];
if (server in accumulator) {
Expand Down
15 changes: 10 additions & 5 deletions dist/features/favsFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
async function favFeature(api) {
let results = [];
let pages = 3;
for await (const page of api.v1.favourites.list({ limit: 80 })) {
results = results.concat(page);
pages--;
if (pages === 0 || results.length < 80) {
break;
try {
for await (const page of api.v1.favourites.list({ limit: 80 })) {
results = results.concat(page);
pages--;
if (pages === 0 || results.length < 80) {
break;
}
}
}
catch (e) {
return {};
}
const favFrequ = results.reduce((accumulator, status) => {
if (!status.account)
return accumulator;
Expand Down
15 changes: 10 additions & 5 deletions dist/features/interactsFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
async function interactFeature(api) {
let results = [];
let pages = 3;
for await (const page of api.v1.notifications.list({ limit: 80 })) {
results = results.concat(page);
pages--;
if (pages === 0 || results.length < 80) {
break;
try {
for await (const page of api.v1.notifications.list({ limit: 80 })) {
results = results.concat(page);
pages--;
if (pages === 0 || results.length < 80) {
break;
}
}
}
catch (e) {
return {};
}
const interactFrequ = results.reduce((accumulator, status) => {
if (!status.account)
return accumulator;
Expand Down
15 changes: 10 additions & 5 deletions dist/features/reblogsFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
async function getReblogsFeature(api) {
let results = [];
let pages = 3;
for await (const page of api.v1.timelines.home.list({ limit: 80 })) {
results = results.concat(page);
pages--;
if (pages === 0 || results.length < 80) {
break;
try {
for await (const page of api.v1.timelines.home.list({ limit: 80 })) {
results = results.concat(page);
pages--;
if (pages === 0 || results.length < 80) {
break;
}
}
}
catch (e) {
return {};
}
const reblogFrequ = results.reduce((accumulator, status) => {
if (status.reblog) {
if (status.reblog.account.acct in accumulator) {
Expand Down
3 changes: 3 additions & 0 deletions dist/feeds/topPostsFeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ async function getTopPostFeed(api) {
const servers = Object.keys(core_servers).sort((a, b) => {
return core_servers[b] - core_servers[a];
}).slice(0, 10);
if (servers.length === 0) {
return [];
}
results = await Promise.all(servers.map(async (server) => {
if (server === "undefined" || typeof server == "undefined" || server === "")
return [];
Expand Down
5 changes: 2 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,12 @@ class TheAlgorithm {
//Adjust Weights based on user interaction
if (statusWeights == undefined)
return;
const mean = Object.values(statusWeights).reduce((accumulator, currentValue) => accumulator + Math.abs(currentValue), 0) / Object.values(statusWeights).length;
const mean = Object.values(statusWeights).filter((value) => !isNaN(value)).reduce((accumulator, currentValue) => accumulator + Math.abs(currentValue), 0) / Object.values(statusWeights).length;
const currentWeight = await this.getWeights();
const currentMean = Object.values(currentWeight).reduce((accumulator, currentValue) => accumulator + currentValue, 0) / Object.values(currentWeight).length;
const currentMean = Object.values(currentWeight).filter((value) => !isNaN(value)).reduce((accumulator, currentValue) => accumulator + currentValue, 0) / Object.values(currentWeight).length;
for (let key in currentWeight) {
let reweight = 1 - (Math.abs(statusWeights[key]) / mean) / (currentWeight[key] / currentMean);
currentWeight[key] = currentWeight[key] - step * currentWeight[key] * reweight;
console.log(reweight);
}
await this.setWeights(currentWeight);
return currentWeight;
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,12 @@ export default class TheAlgorithm {
async weightAdjust(statusWeights: weightsType, step = 0.001): Promise<weightsType | undefined> {
//Adjust Weights based on user interaction
if (statusWeights == undefined) return;
const mean = Object.values(statusWeights).reduce((accumulator, currentValue) => accumulator + Math.abs(currentValue), 0) / Object.values(statusWeights).length;
const mean = Object.values(statusWeights).filter((value: number) => !isNaN(value)).reduce((accumulator, currentValue) => accumulator + Math.abs(currentValue), 0) / Object.values(statusWeights).length;
const currentWeight: weightsType = await this.getWeights()
const currentMean = Object.values(currentWeight).reduce((accumulator, currentValue) => accumulator + currentValue, 0) / Object.values(currentWeight).length;
const currentMean = Object.values(currentWeight).filter((value: number) => !isNaN(value)).reduce((accumulator, currentValue) => accumulator + currentValue, 0) / Object.values(currentWeight).length;
for (let key in currentWeight) {
let reweight = 1 - (Math.abs(statusWeights[key]) / mean) / (currentWeight[key] / currentMean);
currentWeight[key] = currentWeight[key] - step * currentWeight[key] * reweight;
console.log(reweight);
}
await this.setWeights(currentWeight);
return currentWeight;
Expand Down