Skip to content

Commit

Permalink
chore: added Decimal to avoid weird number with floating point
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernand authored and Fernand committed Mar 22, 2024
1 parent 902b54e commit b804e7b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@commander-js/extra-typings": "12.0.1",
"@octokit/rest": "20.0.2",
"commander": "12.0.0",
"decimal.js": "10.4.3",
"dotenv": "16.4.5",
"jsdom": "24.0.0",
"markdown-it": "14.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/get-activity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("GetActivity class", () => {
const processor = new Processor();
await processor.run(activity);
processor.dump();
});
}, 30000);

it("should create an instance of GetActivity", () => {
expect(activity).toBeInstanceOf(GetActivity);
Expand Down
10 changes: 6 additions & 4 deletions src/parser/content-evaluator-module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Decimal from "decimal.js";
import OpenAI from "openai";
import configuration from "../configuration/config-reader";
import { GetActivity } from "../get-activity";
Expand Down Expand Up @@ -25,12 +26,13 @@ export class ContentEvaluatorModule implements Module {
const commentBody = comment.content;
if (specificationBody && commentBody) {
const { relevance } = await this._evaluateComment(specificationBody, commentBody);
const reward = comment.score?.reward ? comment.score.reward * relevance : relevance;
const currentReward = new Decimal(comment.score?.reward ? comment.score.reward : 0);
const reward = currentReward.mul(relevance).toNumber();
comments[i] = {
...comment,
score: {
...comment.score,
relevance,
relevance: relevance.toNumber(),
reward,
},
};
Expand Down Expand Up @@ -58,11 +60,11 @@ export class ContentEvaluatorModule implements Module {
presence_penalty: 0,
});

return { relevance: Number(response.choices[0].message.content) };
return { relevance: new Decimal(Number(response.choices[0].message.content)) };
} catch (error) {
console.error("Failed to evaluate comment", error);
return {
relevance: 0,
relevance: relevance,
};
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/parser/formatting-evaluator-module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Decimal from "decimal.js";
import { JSDOM } from "jsdom";
import MarkdownIt from "markdown-it";
import configuration from "../configuration/config-reader";
Expand Down Expand Up @@ -44,17 +45,22 @@ export class FormattingEvaluatorModule implements Module {
const formattingTotal = formatting
? Object.values(formatting).reduce(
(acc, curr) =>
acc + curr.score * multiplierFactor.formattingMultiplier * (curr.count * multiplierFactor.wordValue),
0
acc.add(
new Decimal(curr.score)
.mul(multiplierFactor.formattingMultiplier)
.mul(curr.count)
.mul(multiplierFactor.wordValue)
),
new Decimal(0)
)
: 0;
: new Decimal(0);
comment.score = {
...comment.score,
formatting: {
content: formatting,
...multiplierFactor,
},
reward: comment.score?.reward ? comment.score.reward + formattingTotal : formattingTotal,
reward: (comment.score?.reward ? formattingTotal.add(comment.score.reward) : formattingTotal).toNumber(),
};
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/parser/processor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Decimal from "decimal.js";
import * as fs from "fs";
import configuration from "../configuration/config-reader";
import { CommentType, GetActivity } from "../get-activity";
Expand Down Expand Up @@ -52,19 +53,19 @@ export class Processor {
}

_sumRewards(obj: Record<string, unknown>) {
let totalReward = 0;
let totalReward = new Decimal(0);

for (const [key, value] of Object.entries(obj)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (key === "reward" && typeof value === "number") {
totalReward += value;
totalReward = totalReward.add(value);
} else if (typeof value === "object") {
totalReward += this._sumRewards(value as Record<string, unknown>);
totalReward = totalReward.add(this._sumRewards(value as Record<string, unknown>));
}
}
}

return totalReward;
return totalReward.toNumber();
}
}

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3400,7 +3400,7 @@ decamelize@^1.1.0:
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
integrity sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA==

decimal.js@^10.4.3:
decimal.js@10.4.3, decimal.js@^10.4.3:
version "10.4.3"
resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.4.3.tgz#1044092884d245d1b7f65725fa4ad4c6f781cc23"
integrity sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==
Expand Down

0 comments on commit b804e7b

Please sign in to comment.