-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
72 lines (65 loc) · 2.01 KB
/
index.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
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import * as fs from 'fs';
const similarity = async (wordToFind:string,guessedWord:string) => {
const body = {
sim1: wordToFind,
sim2: guessedWord,
lang: "fr",
type: "General Word2Vec",
};
const similarityResponse = await fetch(
"http://nlp.polytechnique.fr/similarityscore",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
}
);
const similarityResponseJson = await similarityResponse.json();
return Number(similarityResponseJson.simscore);
}
const extractGuess = async (req: Request) => {
const slackPayload = await req.formData();
const guess = await slackPayload.get("text")?.toString();
if (!guess) {
throw Error("Guess is empty or null");
}
return guess;
};
const responseBuilder = (similarity:number,guessedWord:string) => {
if (similarity == 1) {
return `Ouiii ! ${guessedWord} était bien le mot`;
}
if (similarity>.7) {
return `Tu es proche avec ${guessedWord}`;
}
if (similarity < .3) {
return `Tu es bien loiiiin avec ${guessedWord}`;
}
return `Courage, ça progresse avec ${guessedWord}`;
}
const generateWordToGuess = () => {
let dateTime = new Date()
let month = dateTime.getUTCMonth() + 1; //months from 1-12
let day = dateTime.getUTCDate();
let year = dateTime.getUTCFullYear();
const List = fs.readFileSync('List.txt','utf8');
const wordsList = List.split('\r\n');
const index = (day+month+year)% wordsList.length;
return wordsList[index];
}
async function handler(_req: Request): Promise<Response> {
try {
const guess = await extractGuess(_req);
const wordToGuess = generateWordToGuess();
const new_similarity = await similarity(wordToGuess,guess);
const response = responseBuilder(new_similarity,guess);
return new Response(response);
} catch(e) {
console.error(e);
return new Response("Error : ",e);
}
}
serve(handler);