-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathparse-comment.js
More file actions
274 lines (243 loc) · 6.7 KB
/
parse-comment.js
File metadata and controls
274 lines (243 loc) · 6.7 KB
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
const nlp = require("compromise");
// Types that are valid (multi words must all be lower case)
const validContributionTypes = [
"a11y",
"audio",
"blog",
"bug",
"business",
"code",
"content",
"data",
"design",
"doc",
"eventorganizing",
"example",
"financial",
"fundingfinding",
"ideas",
"infra",
"maintenance",
"mentoring",
"platform",
"plugin",
"projectManagement",
"promotion",
"question",
"research",
"review",
"security",
"talk",
"test",
"tool",
"translation",
"tutorial",
"usertesting",
"video",
];
// Types that are valid multi words, that we need to re map back to their camelCase parts
const validMultiContributionTypesMapping = {
eventorganizing: "eventOrganizing",
fundingfinding: "fundingFinding",
usertesting: "userTesting",
projectmanagement: "projectManagement",
};
// Additional terms to match to types (plurals, aliases etc)
const contributionTypeMappings = {
accessibility: "a11y",
advertisement: "promotion",
blogs: "blog",
blogging: "blog",
bugs: "bug",
codes: "code",
coding: "code",
dataset: "data",
datasets: "data",
designing: "design",
designs: "design",
doc: "doc",
docs: "doc",
documenting: "doc",
documentation: "doc",
examples: "example",
finance: "financial",
financials: "financial",
funds: "fundingfinding",
idea: "ideas",
infras: "infra",
infrastructure: "infra",
maintaining: "maintenance",
management: "projectManagement",
managing: "projectManagement",
mentor: "mentoring",
music: "audio",
platforms: "platform",
plugins: "plugin",
project: "projectManagement",
projectManaging: "projectManagement",
questions: "question",
reviews: "review",
securing: "security",
sound: "audio",
talks: "talk",
tests: "test",
testing: "test",
tools: "tool",
tooling: "tool",
translator: "translation",
translating: "translation",
translations: "translation",
tutorials: "tutorial",
videos: "video",
};
// Additional terms to match to types (plurals, aliases etc) that are multi word
const contributionTypeMultiWordMapping = {
"audio production": "audio",
"audio recording": "audio",
"music production": "audio",
"data collection": "data",
"data collections": "data",
"data set": "data",
"data sets": "data",
"event organising": "eventOrganizing",
"event organizing": "eventOrganizing",
"fund finding": "fundingFinding",
"funding finding": "fundingFinding",
"user testing": "userTesting",
"business development": "business",
"project management": "projectManagement",
"social media": "promotion",
};
const Contributions = {};
validContributionTypes.forEach((type) => {
Contributions[type] = "Contribution";
});
Object.keys(contributionTypeMappings).forEach((type) => {
Contributions[`${type}`] = "Contribution";
});
const plugin = {
words: {
...Contributions,
add: "Action",
},
};
nlp.plugin(plugin);
function findWho(message, action) {
function findWhoSafe(match) {
message = message.replace(/\-/g, "#/#"); // workaround (https://github.com/spencermountain/compromise/issues/726)
const whoNormalizeSettings = {
whitespace: true, // remove hyphens, newlines, and force one space between words
case: false, // keep only first-word, and 'entity' titlecasing
numbers: false, // turn 'seven' to '7'
punctuation: true, // remove commas, semicolons - but keep sentence-ending punctuation
unicode: false, // visually romanize/anglicize 'Björk' into 'Bjork'.
contractions: false, // turn "isn't" to "is not"
acronyms: false, //remove periods from acronyms, like 'F.B.I.'
parentheses: false, //remove words inside brackets (like these)
possessives: false, // turn "Google's tax return" to "Google tax return"
plurals: false, // turn "batmobiles" into "batmobile"
verbs: false, // turn all verbs into Infinitive form - "I walked" → "I walk"
honorifics: false, //turn 'Vice Admiral John Smith' to 'John Smith'
};
const matchedSet = nlp(message)
.match(match)
.normalize(whoNormalizeSettings)
.data();
if (matchedSet.length > 0) {
matchedText = matchedSet[0].text;
matchedText = matchedText.replace(/#\/#/g, "-");
return matchedText;
}
}
const whoMatchedMention = findWhoSafe(`@[.]`);
// TODO: Unused
// if (whoMatchedMention) {
// return whoMatchedMention;
// }
const whoMatchedByAction = findWhoSafe(`${action} [.]`);
if (whoMatchedByAction) {
return whoMatchedByAction;
}
const whoMatchedByFor = findWhoSafe(`[.] for`);
if (whoMatchedByFor) {
return whoMatchedByFor;
}
}
function parseAddSentence(message, action) {
message = message
.split(",")
.map((e) => e.trim())
.join(", ");
const whoMatched = findWho(message, action);
if (!whoMatched) {
return {
who: undefined,
};
}
const who = whoMatched.startsWith("@") ? whoMatched.substr(1) : whoMatched;
// Contributions
const doc = nlp(message).toLowerCase();
// This is to support multi word 'matches' (altho the compromise docs say it supports this *confused*)
Object.entries(contributionTypeMultiWordMapping).forEach(
([multiWordType, singleWordType]) => {
doc.replace(multiWordType, singleWordType);
}
);
const rawContributions = doc
.match("#Contribution")
.data()
.map((data) => {
// This removes whitespace, commas etc
return data.normal;
});
const contributions = doc
.match("#Contribution")
.data()
.map((data) => {
// This removes whitespace, commas etc
return data.normal;
})
.filter((element, index) => {
return rawContributions.indexOf(element) === index;
})
.map((type) => {
if (contributionTypeMappings[type]) return contributionTypeMappings[type];
return type;
})
.map((type) => {
// Convert usertesting to userTesting for the node api
if (validMultiContributionTypesMapping[type])
return validMultiContributionTypesMapping[type];
return type;
});
return {
who,
contributions,
};
}
function parseAddComment(message, action) {
const contributors = {};
const sentences = nlp(message).sentences();
sentences.forEach(function (sentence) {
const sentenceRaw = sentence.data()[0].text;
const { who, contributions } = parseAddSentence(sentenceRaw, action);
if (who) {
contributors[who] = contributions;
}
});
return {
action: "add",
contributors,
};
}
function parseComment(message) {
const doc = nlp(message);
const action = doc.toLowerCase().match("#Action").normalize().out("string");
if (action.match("add")) {
return parseAddComment(message, action);
}
return {
action: false,
};
}
module.exports = parseComment;