-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
132 lines (113 loc) · 2.74 KB
/
app.js
File metadata and controls
132 lines (113 loc) · 2.74 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
const sw = require('stopword')
const express = require('express')
const app = express()
const _emsearch = require('@jukben/emoji-search').default
const emojilib = require('emojilib')
const sesearch = require('../index')
let emoji;
run();
async function run() {
emoji = Object.entries(require('emojilib'))
.filter(([k, v]) => k.length < 5)
.map(([k, v]) => [k, v.map(v => v.replace(/_/g, ' '))])
app.listen(process.env.PORT || 3012);
}
app.get('/test', async (req, res) => {
const phrases = [
'watching tv',
'smoking cigarettes',
'taking a bath',
'swimsuit',
'driving kids to school',
'reading',
'on vacation',
'sick',
'awesome',
'making it happen',
"workin'",
"taking a break",
"focus time",
"conversion tracking",
"lunchtime",
"reporting",
"taking photos",
"performance testing",
"getting ready",
"artist",
"writing",
"plumber",
"space exploration",
"commuting",
"on my way",
"doctor",
"on a call",
"napping",
"listening to music",
];
const results = [];
for (const p of phrases) {
const emchar = emsearch(p);
const sechar = sesearch(p)?.[0]?.char;
results.push({ phrase: p, emchar, sechar })
}
res.send(`
<style>
table {
border-spacing: 0;
border-collapse: collapse;
}
td, th {
padding: 12px;
border: 1px solid #ddd;
text-align: center;
}
</style>
<table>
<tr>
<th>phrase</th>
<th>emoji-search</th>
<th>emojisearch</th>
</tr>
${results.map(r => `
<tr>
<td>${r.phrase || '-'}</td>
<td>${r.emchar || '-'}</td>
<td>${r.sechar || '-'}</td>
</tr>
`).join``}
</table>
`)
})
app.get('/', async (req, res) => {
const q = req.query.q || 'happy'
const emchar = emsearch(q)
const sechar = sesearch(q)?.[0]?.char
res.send(`
<style>
small {
opacity: 0.5;
}
</style>
<form>
<input name="q" value="${q}" autofocus>
<input type="submit">
</form>
<table>
<tr><td><small>emoji-search</small></td><td><h1>${emchar || '-'}</h1></td></tr>
<tr><td><small>emojisearch</small></td><td><h1>${sechar || '-'}</h1></td></tr>
</table>
`)
})
const emsearch = phrase => {
const split = sw.removeStopwords(phrase.trim().split(' '));
const results = split.map(_emsearch).map(x => x[0])
const filtered = results.filter(r => !!r)
if (!filtered.length) return '';
const match = filtered[filtered.length - 1].name;
for (const [k,v] of Object.entries(emojilib)) {
if (v[0].replace(/ /g, '_') == match.replace(/ /g, '_')) {
return k;
}
}
return filtered[filtered.length - 1].char;
}