-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
156 lines (139 loc) · 4.7 KB
/
script.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
const dictionaryDatabaseLink = 'https://raw.githubusercontent.com/MinhasKamal/BengaliDictionary/master/BengaliDictionary.json';
const RADIX = 256;
const PRIME = 908209935089;
var initialVariableA = null;
var initialVariableB = null;
var NOS;
var dictionary; //number of words in dictionary
var data;
const primeArray = [1, 2, 5, 11, 17, 29, 37, 53, 67]; //array of next highest prime greater than sqauare of index
var tempHashTable = new Array();
var maxKey = -1;
var hashTable = new Array();
var hashtable1Indicator;
var hashTable2Indicator = new Array();
window.onload = function initializeHashing(){
console.log('Data Received');
data = fetch(dictionaryDatabaseLink)
.then(response => {
if(!response.ok){
throw new Error("HTTP error " + response.status);
}
return response.json()
})
.then(json => {
dictionary = json;
NOS = Object.keys(dictionary).length;
})
.then(response => {
hashtable1Indicator = new Array(NOS).fill(0);
for(var i = 0; i < NOS; i++) {
hashTable[i] = new Array(80).fill(null);
tempHashTable[i] = new Array(80).fill(null);
hashTable2Indicator[i] =new Array(5).fill(-1);
hashTable[i][0] = null;
}
for(var i = 0; i < NOS; i++){
var word = dictionary[i].en.toLowerCase();
var key = primaryHash(convertWordToKey(word));
maxKey = Math.max(key, maxKey);
if(hashtable1Indicator[key] == 0){
hashTable[key][0] = dictionary[i];
tempHashTable[key][0] = dictionary[i];
hashtable1Indicator[key] = 1;
}
else{
tempHashTable[key][hashtable1Indicator[key]++] = dictionary[i];
}
}
for(var i = 0; i <= maxKey; i++){
if(hashtable1Indicator[i] != 0){
var result = secondaryHash(i, tempHashTable[i]);
hashTable[i] = result[0];
hashTable2Indicator[i] = result[1];
}
}
});
}
function convertWordToKey(word){
var val = 0;
var a = Math.floor(Math.random() * (PRIME - 1) ) + 1;
var b = Math.floor(Math.random() * PRIME);
if(initialVariableA == null || initialVariableB == null){
initialVariableA = a;
initialVariableB = b;
}
else{
a = initialVariableA;
b = initialVariableB;
}
for(var i=0; i<word.length; i++){
val = ( (val*RADIX) % PRIME + word.charCodeAt(i) ) % PRIME;
}
const aB = BigInt(a*val + b);
const out = aB % BigInt(PRIME);
return Number(out);
}
function primaryHash(number){
return number % NOS;
}
function secondaryHash(key, tempHashArray){
var set = false;
var finalArray;
while(!set){
finalArray = new Array(80).fill(null);
var secondaryHashArray = new Array(3).fill(-1);
var a = BigInt(Math.floor(Math.random() * (PRIME - 1) ) + 1);
var b = BigInt(Math.floor(Math.random() * PRIME));
for(var i = 0; i < hashtable1Indicator[key]; i++){
var pos = BigInt((a * BigInt(key) + b) % BigInt(primeArray[hashtable1Indicator[key]]));
if(finalArray[pos] == null){
finalArray[pos] = tempHashArray[i];
secondaryHashArray = [a, b , BigInt(primeArray[hashtable1Indicator[key]])];
if(i + 1 == hashtable1Indicator[key]) set = true;
}
else if(tempHashArray[i].en === finalArray[pos].en){
if(i + 1 == hashtable1Indicator[key]) set = true;
continue;
}
else break;
}
return[
finalArray, secondaryHashArray
];
}
}
function search(){
var input = document.getElementById('query');
var word = input.value.toLowerCase();
var output1 = document.getElementById('output1');
var output2 = document.getElementById('output2');
var output3 = document.getElementById('output3');
var output4 = document.getElementById('output4');
var output5 = document.getElementById('output5');
var key = primaryHash(convertWordToKey(word));
var sHash;
try{
if(hashtable1Indicator[key] == 0){
throw 'Word Not Found';
}
const a = hashTable2Indicator[key][0];
const b = hashTable2Indicator[key][1];
const c = hashTable2Indicator[key][2];
sHash = BigInt((a * BigInt(key) + b) % BigInt(primeArray[hashtable1Indicator[key]]));
if(hashTable[key][sHash] != null &&
hashTable[key][sHash].en == word){
output1.innerHTML = hashTable[key][sHash].pron;
output2.innerHTML = hashTable[key][sHash].bn;
output3.innerHTML = hashTable[key][sHash].bn_syns;
output4.innerHTML = hashTable[key][sHash].en_syns;
output5.innerHTML = hashTable[key][sHash].sents;
}
else{
throw 'Word Not Found';
}
}catch(err){
console.log(err);
output.innerHTML = '';
};
}