This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcomment-scoring-rubric.ts
283 lines (247 loc) · 10.5 KB
/
comment-scoring-rubric.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { JSDOM } from "jsdom";
// TODO: should be inherited from default config. This is a temporary solution.
import Decimal from "decimal.js";
import _ from "lodash";
import MarkdownIt from "markdown-it";
import { GitHubComment } from "../../types/payload";
import { ContributorClassesKeys } from "./contribution-style-types";
import { FormatScoreConfig, FormatScoreConfigParams } from "./element-score-config";
export type Tags = keyof HTMLElementTagNameMap;
const md = new MarkdownIt();
const ZERO = new Decimal(0);
const ONE = new Decimal(1);
type CommentScoringConstructor = {
contributionClass: ContributorClassesKeys;
formattingMultiplier: number;
wordValue: number;
};
export class CommentScoring {
public contributionClass: ContributorClassesKeys; // This instance is used to calculate the score for this contribution `[view][role] "Comment"` class
// public viewWordScore: Decimal; // TODO: implement
// public viewWordScoreMultiplier!: number; // TODO: implement
public roleWordScore: Decimal;
public roleWordScoreMultiplier!: number;
public commentScores: {
[userId: string]: {
totalScoreTotal: Decimal;
wordScoreTotal: Decimal;
formatScoreTotal: Decimal;
details: {
[commentId: string]: {
totalScoreComment: Decimal;
relevanceScoreComment: Decimal; // nullable because this is handled elsewhere in the program logic
// clarityScoreComment: null | Decimal; // TODO: implement
wordScoreComment: Decimal;
wordScoreCommentDetails: { [word: string]: Decimal };
formatScoreComment: Decimal;
formatScoreCommentDetails: {
[tagName in Tags]?: {
count: number;
score: Decimal;
words: number;
};
};
comment: GitHubComment;
};
};
};
} = {};
private _formatConfig: { [tagName in Tags]?: FormatScoreConfigParams } = {
img: new FormatScoreConfig({ element: "img", disabled: true }), // disabled
blockquote: new FormatScoreConfig({
element: "blockquote",
disabled: true,
}), // disabled
em: new FormatScoreConfig({ element: "em", disabled: true }), // disabled
strong: new FormatScoreConfig({ element: "strong", disabled: true }), // disabled
h1: new FormatScoreConfig({ element: "h1", value: ONE }),
h2: new FormatScoreConfig({ element: "h2", value: ONE }),
h3: new FormatScoreConfig({ element: "h3", value: ONE }),
h4: new FormatScoreConfig({ element: "h4", value: ONE }),
h5: new FormatScoreConfig({ element: "h5", value: ONE }),
h6: new FormatScoreConfig({ element: "h6", value: ONE }),
a: new FormatScoreConfig({ element: "a", value: ONE }),
// ul: new ElementScoreConfig({ element: "ul", value: ONE }),
li: new FormatScoreConfig({ element: "li", value: ONE }),
// p: new ElementScoreConfig({ element: "p", value: ZERO }),
code: new FormatScoreConfig({ element: "code", value: ONE }),
// table: new ElementScoreConfig({ element: "table", value: ONE }),
td: new FormatScoreConfig({ element: "td", value: ONE }),
// tr: new ElementScoreConfig({ element: "tr", value: ONE }),
br: new FormatScoreConfig({ element: "br", value: ONE }),
hr: new FormatScoreConfig({ element: "hr", value: ONE }),
// del: new ElementScoreConfig({ element: "del", value: ONE }),
// pre: new ElementScoreConfig({ element: "pre", value: ONE }),
// ol: new ElementScoreConfig({ element: "ol", value: ONE }),
};
private _renderCache: { [commentId: number]: string } = {};
constructor({ contributionClass, formattingMultiplier = 1, wordValue = 0 }: CommentScoringConstructor) {
this.contributionClass = contributionClass;
this._applyRoleMultiplier(formattingMultiplier);
this.roleWordScore = new Decimal(wordValue);
}
private _getRenderedCommentBody(comment: GitHubComment): string {
if (!this._renderCache[comment.id]) {
this._renderCache[comment.id] = md.render(comment.body);
}
return this._renderCache[comment.id];
}
public compileTotalUserScores(): void {
for (const userId in this.commentScores) {
const userCommentScore = this.commentScores[userId];
const wordScores = [];
const formatScores = [];
for (const commentId in userCommentScore.details) {
const commentScoreDetails = userCommentScore.details[commentId];
const formatScoreComment = commentScoreDetails.formatScoreComment;
const wordScoreComment = commentScoreDetails.wordScoreComment;
commentScoreDetails.totalScoreComment = formatScoreComment.plus(wordScoreComment);
wordScores.push(wordScoreComment);
formatScores.push(formatScoreComment);
}
userCommentScore.wordScoreTotal = wordScores.reduce((total, score) => total.plus(score), ZERO);
userCommentScore.formatScoreTotal = formatScores.reduce((total, score) => total.plus(score), ZERO);
userCommentScore.totalScoreTotal = userCommentScore.wordScoreTotal.plus(userCommentScore.formatScoreTotal);
}
}
public getTotalScorePerId(userId: number): Decimal {
const score = this.commentScores[userId].totalScoreTotal;
if (!score) {
throw new Error(`No score for id ${userId}`);
}
return score;
}
private _getWordsNotInDisabledElements(comment: GitHubComment): string[] {
const htmlString = this._getRenderedCommentBody(comment);
const dom = new JSDOM(htmlString);
const doc = dom.window.document;
const disabledElements = Object.entries(this._formatConfig)
.filter(([, config]) => config.disabled)
.map(([elementName]) => elementName);
disabledElements.forEach((elementName) => {
const elements = doc.getElementsByTagName(elementName);
for (let i = 0; i < elements.length; i++) {
this._removeTextContent(elements[i]); // Recursively remove text content
}
});
// Provide a default value when textContent is null
return (doc.body.textContent || "").match(/\w+/g) || [];
}
private _removeTextContent(element: Element): void {
if (element.hasChildNodes()) {
for (const child of Array.from(element.childNodes)) {
this._removeTextContent(child as Element);
}
}
element.textContent = ""; // Remove the text content of the element
}
private _calculateWordScores(
words: string[]
): (typeof CommentScoring.prototype.commentScores)[number]["details"][number]["wordScoreCommentDetails"] {
const wordScoreCommentDetails: { [key: string]: Decimal } = {};
for (const word of words) {
let counter = wordScoreCommentDetails[word] || ZERO;
counter = counter.plus(this.roleWordScore);
wordScoreCommentDetails[word] = counter;
}
return wordScoreCommentDetails;
}
private _calculateWordScoresTotals(
wordScoreCommentDetails: (typeof CommentScoring.prototype.commentScores)[number]["details"][number]["wordScoreCommentDetails"]
): Decimal {
let totalScore = ZERO;
for (const score of Object.values(wordScoreCommentDetails)) {
totalScore = totalScore.plus(score);
}
return totalScore;
}
private _countWordsInTag(html: string, tag: string): number {
const regex = new RegExp(`<${tag}[^>]*>(.*?)</${tag}>`, "g");
let match;
let wordCount = 0;
while ((match = regex.exec(html)) !== null) {
const content = match[1];
const words = content.match(/\w+/g) || [];
wordCount += words.length;
}
return wordCount;
}
public computeElementScore(comment: GitHubComment, userId: number) {
const htmlString = this._getRenderedCommentBody(comment);
const formatStatistics = _.mapValues(_.cloneDeep(this._formatConfig), () => ({
count: 0,
score: ZERO,
words: 0,
}));
let totalElementScore = ZERO;
for (const _elementName in formatStatistics) {
const elementName = _elementName as Tags;
const tag = formatStatistics[elementName];
if (!tag) continue;
tag.count = this._countTags(htmlString, elementName);
const value = this._formatConfig[elementName]?.value;
if (value) tag.score = value.times(tag.count);
tag.words = this._countWordsInTag(htmlString, elementName);
if (tag.count !== 0 || !tag.score.isZero()) {
totalElementScore = totalElementScore.plus(tag.score);
} else {
delete formatStatistics[elementName]; // Delete the element if count and score are both zero
}
}
this._initialize(comment, userId);
// Store the element score for the comment
this.commentScores[userId].details[comment.id].formatScoreComment = totalElementScore;
this.commentScores[userId].details[comment.id].formatScoreCommentDetails = formatStatistics;
return htmlString;
}
private _initialize(comment: GitHubComment, userId: number) {
if (!this.commentScores[userId]) {
console.debug("good thing we initialized, was unsure if necessary");
const initialCommentScoreValue = {
totalScoreTotal: ZERO,
wordScoreTotal: ZERO,
formatScoreTotal: ZERO,
details: {},
};
this.commentScores[userId] = { ...initialCommentScoreValue };
}
if (!this.commentScores[userId].details[comment.id]) {
console.debug("good thing we initialized, was unsure if necessary");
this.commentScores[userId].details[comment.id] = {
totalScoreComment: ZERO,
relevanceScoreComment: ZERO,
wordScoreComment: ZERO,
formatScoreComment: ZERO,
formatScoreCommentDetails: {},
wordScoreCommentDetails: {},
comment,
};
}
}
public computeWordScore(comment: GitHubComment, userId: number) {
const words = this._getWordsNotInDisabledElements(comment);
const wordScoreDetails = this._calculateWordScores(words);
this._initialize(comment, userId);
this.commentScores[userId].details[comment.id].comment = comment;
this.commentScores[userId].details[comment.id].wordScoreComment = this._calculateWordScoresTotals(wordScoreDetails);
this.commentScores[userId].details[comment.id].wordScoreCommentDetails = wordScoreDetails;
return wordScoreDetails;
}
private _applyRoleMultiplier(multiplier: number) {
for (const tag in this._formatConfig) {
const selection = this._formatConfig[tag as Tags];
const value = selection?.value;
if (value) {
selection.value = value.times(multiplier);
}
}
this.roleWordScoreMultiplier = multiplier;
}
private _countTags(html: string, tag: Tags) {
if (this._formatConfig[tag]?.disabled) {
return 0;
}
const regex = new RegExp(`<${tag}[^>]*>`, "g");
return (html.match(regex) || []).length;
}
}