Clean up JavaScript

This commit is contained in:
Florine W. Dekker 2019-06-13 19:27:52 +02:00
parent 64ae47808b
commit 1ae1d5d328
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
4 changed files with 117 additions and 361 deletions

View File

@ -84,7 +84,6 @@
<!-- Scripts -->
<script src="js/levenshtein.js"></script>
<script src="js/jaro-winkler.js"></script>
<script src="js/main.js"></script>
</body>

View File

@ -1,18 +1,18 @@
/**
* The MIT License (MIT)
*
*
* Copyright (c) 2015 Jordan Thomas
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -24,58 +24,44 @@
"use strict";
var jaro_winkler = function(a, b, options)
{
const jaro_winkler = function (a, b, options) {
// Load default options
options = optionDefault(
{
"caseSensitive" : true
},
options
);
options = optionDefault({"caseSensitive": true}, options);
// Convert to lowercase if not case-sensitive
if(!options.caseSensitive)
{
if (!options.caseSensitive) {
a = a.toLowerCase();
b = b.toLowerCase();
}
var alen = a.length,
blen = b.length;
const aLen = a.length;
const bLen = b.length;
// Short-circuit if either is empty
if(alen === 0 || blen === 0)
{
if (aLen === 0 || bLen === 0)
return 0;
}
// Short-circuit if exact match
if(a === b)
{
if (a === b)
return 1;
}
// Calculate difference
var i, j;
var low, high,
m = 0,
range = (Math.floor(Math.max(alen, blen) / 2)) - 1,
aMatches = [],
bMatches = [];
const range = (Math.floor(Math.max(aLen, bLen) / 2)) - 1;
const aMatches = [];
const bMatches = [];
let low;
let high;
let m = 0;
// Find matches
for(i = 0; i < alen; i++)
{
low = ((i >= range) ? i - range : 0);
high = ((i + range <= blen) ? (i + range) : (blen - 1));
for (let i = 0; i < aLen; i++) {
low = ((i >= range) ? i - range : 0);
high = ((i + range <= bLen) ? (i + range) : (bLen - 1));
for(j = low; j <= high; j++)
{
if(aMatches[i] !== true && bMatches[j] !== true && a[i] === b[j])
{
for (let j = low; j <= high; j++) {
if (aMatches[i] !== true && bMatches[j] !== true && a[i] === b[j]) {
aMatches[i] = true;
bMatches[j] = true;
m++;
@ -85,45 +71,36 @@ var jaro_winkler = function(a, b, options)
}
// Short-circuit if not matches found
if(m === 0)
{
if (m === 0) {
return 0;
}
// Count transpositions
var k = 0,
numTrans = 0;
let k = 0;
let numTrans = 0;
for(i = 0; i < alen; i++)
{
if(aMatches[i] === true)
{
for(j = k; j < blen; j++)
{
if(bMatches[j] === true)
{
for (let i = 0; i < aLen; i++) {
if (aMatches[i] === true) {
for (let j = k; j < bLen; j++) {
if (bMatches[j] === true) {
k = j + 1;
break;
}
}
if(a[i] !== b[j])
{
if (a[i] !== b[j]) {
numTrans++;
}
}
}
var weight = (((m / alen) + (m / blen) + (m - (numTrans / 2)) / m) / 3);
var l = 0,
p = 0.1;
let weight = (((m / aLen) + (m / bLen) + (m - (numTrans / 2)) / m) / 3);
let l = 0;
const p = 0.1;
if(weight > 0.7)
{
while(a[l] === b[l] && l < 4)
{
if (weight > 0.7) {
while (a[l] === b[l] && l < 4)
l++;
}
weight += l * p * (1 - weight);
}

View File

@ -1,93 +0,0 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2011 Andrei Mackenzie
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
"use strict";
var levenshtein = function(a, b, options)
{
// Load default options
options = optionDefault(
{
"caseSensitive" : true
},
options
);
// Convert to lowercase if not case-sensitive
if(!options.caseSensitive)
{
a = a.toLowerCase();
b = b.toLowerCase();
}
var alen = a.length,
blen = b.length;
if(alen == 0)
{
return blen;
}
if(blen == 0)
{
return alen;
}
var matrix = [];
// Increment along the first column of each row
for(var i = 0; i <= blen; i++)
{
matrix[i] = [i];
}
// Increment each column in the first row
for(var j = 0; j <= alen; j++)
{
matrix[0][j] = j;
}
// Fill in the rest of the matrix
for(i = 1; i <= blen; i++)
{
for(j = 1; j <= alen; j++)
{
if(b.charAt(i - 1) == a.charAt(j - 1))
{
matrix[i][j] = matrix[i - 1][j - 1];
}
else
{
matrix[i][j] =
min(
matrix[i - 1][j - 1] + 1, // Substitution
matrix[i][j - 1] + 1, // Insertion
matrix[i - 1][j] + 1 // Deletion
);
}
}
}
return matrix[blen][alen];
};

File diff suppressed because one or more lines are too long