-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
160 lines (145 loc) · 4.73 KB
/
parser.js
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
const { contentSecurityPolicy } = require("helmet");
const fetch = require("node-fetch");
const rolesRaw = require("./data/roles.json");
// const categories = Object.keys(rolesRaw)
// .map((category) =>
// Object.keys(rolesRaw[category]).map((role) => ({ role, category }))
// )
// .flat()
// .reduce(
// (acc, { role, category }) =>
// acc[role]
// ? acc[role].push(category) && acc
// : { ...acc, [role]: [category] },
// {}
// );
// const keywords = Object.values(rolesRaw)
// .map((r) =>
// Object.entries(r).map(([role, keywords]) =>
// keywords.map((keyword) => ({ keyword, role }))
// )
// )
// .flat(2)
// .reduce(
// (acc, { keyword, role }) =>
// acc[keyword]
// ? acc[keyword].push(role) && acc
// : { ...acc, [keyword]: [role] },
// {}
// );
// //remove all special characters incliuding spaces
// // const normalize = (text) => {
// // return text.toLowerCase().replace(/\W/g, "");
// // };
// /*
// const find = (text, values) => {
// const set = new Set();
// for (let key in values) {
// for (let word of values[key]) {
// if (text.search(word) != -1) {
// set.add(key);
// }
// }
// }
// return Array.from(set) || [];
// };
// */
// // const phoneRegex =
// // /(?!([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2}))(\+?\d[\d -]{8,12}\d)/g;
// const emailRegex =
// /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/g;
// /*
// const parsePhoneNumbers = (text) => {
// return [
// ...new Set(
// (text.match(phoneRegex) || [])
// .concat(text.replace(/\s+/g, "@").match(phoneRegex) || [])
// .map((phone) => phone.replace(/\s+|-/g, ""))
// .map((phone) =>
// phone.length == 10
// ? phone
// : phone.length > 10 && phone[0] == "0"
// ? phone.substring(0, 11)
// : phone.substring(phone.length - 10)
// )
// ),
// ].filter((_) => _);
// };
// */
// const parseURLs = (text) => {
// return Promise.all(
// (text.match(/https:\/\/t.co\/\w{10}/g) || []).map(
// async (url) => (await fetch(url)).url
// )
// );
// };
// const parseJobType = (text) => {
// let words = text
// .toLowerCase()
// .split(/\W/g)
// .filter((_) => _);
// let nextWords = new Array(words.length + 1).fill("");
// for (let i = 0; nextWords.length > 1 && i < 2; ++i) {
// nextWords.pop();
// for (let j = 0; j < nextWords.length; ++j) {
// nextWords[j] += words[i + j];
// }
// for (const word of nextWords) {
// if (word == "parttime") {
// return "parttime";
// }
// if (word == "intern" || word == "interns" || word == "internship") {
// return "internship";
// }
// if (word == "freelance" || word == "temporary") {
// return "freelance";
// }
// if (word == "fulltime") {
// return "fulltime";
// }
// }
// }
// return "fulltime";
// };
// const needManualVerification = (text) => {
// return text.search(/\?/) != -1;
// };
const parseRoles = (text) => {
// Converts a text like "This is a sentence" to "thisis", "isa", "asentence", "thisisa", "isasentence", "thisisasentence" and matches with the keywords list
let words = text
.toLowerCase()
.split(/\W/g)
.filter((_) => _);
let nextWords = new Array(words.length + 1).fill("");
const roles = new Set();
for (let i = 0; nextWords.length > 1 && i < 3; ++i) {
// stop when either there are no more words to join or the number of words already joined is more than 3, assuming our keyword list doesn't have any keyword that requires more than 3 whitespaces
nextWords.pop();
for (let j = 0; j < nextWords.length; ++j) {
nextWords[j] += words[i + j];
}
for (const word of nextWords) {
if (keywords[word]) {
// `keywords` is a map created from roles.json for constant time lookups
roles.add(keywords[word]);
}
}
}
//console.log(`Text: ${text}\nRoles: ${[...roles]}\n`);
return [...roles].flat();
};
// const parseTweet = async (raw_text) => {
// //const text = normalize(raw_text); //remove all special characters incliuding spaces
// const roles = parseRoles(raw_text);
// return {
// categories: [...new Set(roles.map((role) => categories[role]).flat())],
// roles: roles,
// type: parseJobType(raw_text),
// emails: raw_text.match(emailRegex) || [],
// urls: [...new Set(await parseURLs(raw_text))],
// need_manual_verification: needManualVerification(raw_text),
// //stripped_text: text,
// };
// };
// parseTweet, keywords, categories,
module.exports = { parseRoles };