/
dictionary.js
83 lines (73 loc) · 1.81 KB
/
dictionary.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
const fs = require("fs")
/**
* Dictionary class that interacts with the dictionary.
*/
module.exports = class Dictionary{
/**
* Constructs a dictionary object by opening the wordlist.txt file and preprocessing it.
*/
constructor(){
console.log("Initializing dictionary...");
this.hashMap = {};
const fileContents = fs.readFileSync('./wordlist.txt').toString();
this.makeHashMap(fileContents);
console.log("Done.");
}
/**
* Checks if a word contains only alphanumeric characters, spaces, and single quotes.
* @param word The word to check.
* @returns True if alphanumeric.
*/
isAlphaNumeric(word){
const word2 = word.replace(/[^\w\s']/gi, "");
return word2.length === word.length;
}
/**
* Converts word into hash.
* @param word The word to convert.
* @returns The hash.
*/
getHash(word){
return word.split('').sort().join('').toLowerCase()
}
/**
* Takes a string of words, splits it by new lines, and constructs a hashMap out of them.
* @param words The string of words.
* @returns True if successful.
*/
makeHashMap(words){
if (words.length < 1) {return;}
words = words.split("\r\n")
for (let word of words) {
// We don't add any words that contain special characters to the hashMap.
if (!this.isAlphaNumeric(word)){
continue;
}
let hash = this.getHash(word)
if (hash.length){
if (!this.hashMap[hash]){
this.hashMap[hash] = [word];
} else {
this.hashMap[hash].push(word);
}
}
}
return true;
}
/**
* Checks a word against the hashMap.
* @param word The word.
* @returns An array of anagrams found.
*/
getAnagrams (word){
if (!this.isAlphaNumeric(word)){
return [];
}
const hash = this.getHash(word)
if(typeof this.hashMap[hash] !== "undefined"){
return this.hashMap[hash];
} else {
return [];
}
}
}