-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecipher.js
More file actions
41 lines (31 loc) · 951 Bytes
/
decipher.js
File metadata and controls
41 lines (31 loc) · 951 Bytes
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
const {
decypher,
enrich,
getWordsByLength,
findBestNextWord,
cleanUpSentence,
} = require("./utils");
const findByLetterCount = (cypher) => {
var maps = [{}];
const cleanCypher = cleanUpSentence(cypher);
const wordByLength = getWordsByLength(cleanCypher);
maps = Object.entries(wordByLength).reduce(
(acc, [length, wordArray]) =>
wordArray.reduce((acc, word) => enrich(acc, word, cypher), acc),
maps
);
return maps.map((m) => decypher(cypher, m));
};
const findByNextClosest = (cypher) => {
var maps = [{}];
const cleanCypher = cleanUpSentence(cypher);
var wordArray = cleanCypher.split(" ");
while (wordArray.length > 1 && maps.length > 0) {
next = findBestNextWord(wordArray, maps[0]);
if (!next) break;
maps = enrich(maps, next, cypher);
wordArray = wordArray.filter((w) => w !== next);
}
return maps.map((m) => decypher(cypher, m));
};
module.exports = findByNextClosest;