-
Notifications
You must be signed in to change notification settings - Fork 3
/
strdist.js
917 lines (828 loc) · 26 KB
/
strdist.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
/*
STRING DIST EVALUATION PROJECT
Prof. Charlotte Schubert, Alte Geschichte Leipzig 2019
# GPLv3 copyrigth
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
/*------------------------------------------------------------------------------
Programming Helper
------------------------------------------------------------------------------*/
function len( aA ){
if( aA ){
if( aA instanceof Set ){
return aA.size;
} else {
let li = aA.length;
if( li == undefined ){
return Object.keys( aA ).length;
}
return li;
}
} else {
return 0;
}
}
function set( aA ){
return new Set( aA );
}
function list( aS ){
return Array.from( aS );
}
function print( ){//call with arbitrary arguments and print the argumnet array
for( let a in arguments ){
console.log( a, arguments[a].toString() );
}
}
function max( a, b ){
return Math.max(a, b);
}
function min(a, b){
return Math.min(a, b);
}
const True = true;
const False = false;
/*------------------------------------------------------------------------------
SET OPERATIONS
------------------------------------------------------------------------------*/
function SetSymDiff( setA, setB ){
let AB = [...setA].filter(x => !setB.has( x ));
let BA = [...setB].filter(x => !setA.has( x ));
/*print(BA instanceof Array, AB instanceof Array);
for( let item of AB ){
BA.add( item );
}*/
let symDiff = set( AB.concat( BA ) );
return symDiff;
}
function SetDiff( setA, setB ){
return [...setA].filter(x => !setB.has( x ));
}
function SetUnsion( setA, setB ){
let union = set( [...setA, ...setB] );
return union;
}
function SetIntersection( setA, setB ){
return [...setA].filter( x => setB.has( x ));
}
/*------------------------------------------------------------------------------
generalized comparison: DISTANCES
------------------------------------------------------------------------------*/
//NOTE: all dist/Containedness/common functions take two arrays as first input, the array could be any representation of text (string, sequence, gram, selected words)
function WLEV( s1, s2, Wv, Ws ){
/*
NAME: weighted levenshtein,
INPUT: - s1 and s2 as representations,
- Wv a weight for pairs in A and B,
- Ws a list of 4 weights related to the operations
substitution, insertion, deletion, exchange,
RETURN: Number of edited Letters / sum of editweights,
*/
const lens1 = len( s1 );
const lens2 = len( s2 );
if( lens1 === 0 || lens2 === 0 ){
return Infinity;
}
if( Ws === undefined ){ //optional param
Ws = {"hk": 2, "ui": 1}; //default values, to be extended
}
if( Wv === undefined ){ //optional param
Wv = [1, 1, 1, 2]; //default values
}
if( lens1 < lens2 ){
return WLEV( s2, s1 );
}
let m = []; // is matrix
let i;
let j;
// increment along the first column of each row
for( i = 0; i <= lens2; i+=1 ){
m[ i ] = [i];
}
// increment each column in the first row
for( j = 0; j <= lens1; j+=1 ){
m[0][j] = j;
}
// fill in the rest of the matrix
for( i = 1; i <= lens2; i+=1 ){
for( j = 1; j <= lens1; j+=1 ){
if( s2[ i-1 ] === s1[ j-1 ] ){
m[i][j] = m[i-1][j-1];
} else {
let charsum = s2[ i-1 ]+""+s1[ j-1 ];
let weightofdigram = Ws[ charsum ];
if( weightofdigram === undefined ){
weightofdigram = 0;
}
if( 1 < i && 1 < j ){
m[i][j] = min(
min(
m[i-1][j-1] + Wv[0], //substitution
min(
m[i][j-1] + Wv[1], //insertion
m[i-1][j] + Wv[2])), //deletion
m[i-2][j-2] + Wv[3] ) //exchange
+ weightofdigram; //digram weight
} else {
m[i][j] = min(m[i-1][j-1] + Wv[0], // substitution
min(m[i][j-1] + Wv[1], // insertion
m[i-1][j] + Wv[2])) // deletion
+ weightofdigram; //digram weight
}
}
}
}
return m[ lens2 ][ lens1 ]; //returns distnace similarity is 1 - (d/max(len(A,B)))
}
function LEVDAM( s1, s2, Wv ){
/*
NAME: damerau levenshtein,
INPUT: - a text representation s1 and s2,
- Wv a list of 4 weights related to the operations
substitution, insertion, deletion, exchange,
RETURN: sum of editweights,
*/
const lens1 = len( s1 );
const lens2 = len( s2 );
if( lens1 === 0 || lens2 === 0 ){
return Infinity;
}
if( Wv === undefined ){ //optional param
Wv = [1, 1, 1, 2];
}
if( lens1 < lens2 ){
return LEVDAM( s2, s1 );
}
let m = []; // is matrix
let i;
let j;
// increment along the first column of each row
for( i = 0; i <= lens2; i+=1 ){
m[ i ] = [i];
}
// increment each column in the first row
for( j = 0; j <= lens1; j+=1 ){
m[0][j] = j;
}
// Fill in the rest of the matrix
for( i = 1; i <= lens2; i+=1 ){
for( j = 1; j <= lens1; j+=1 ){
if( s2[ i-1 ] === s1[ j-1 ] ){
m[i][j] = m[i-1][j-1];
} else {
if( 1 < i && 1 < j ){
m[i][j] = min(
min(
m[i-1][j-1] + Wv[0], //substitution
min(
m[i][j-1] + Wv[1], //insertion
m[i-1][j] + Wv[2])), //deletion
m[i-2][j-2] + Wv[3] ); //exchange
} else {
m[i][j] = min(m[i-1][j-1] + Wv[0], // substitution
min(m[i][j-1] + Wv[1], // insertion
m[i-1][j] + Wv[2])); // deletion
}
}
}
}
return m[ lens2 ][ lens1 ]; //returns distnace similarity is 1 - (d/max(len(A,B)))
}
function levenshtein( s1, s2, Wv ){
/*
NAME: Levenshtein wie immer, weightable,
INPUT: - s1 and s2 text representations,
- Wv a list of 4 weights related to the operations
substitution, insertion, deletion, exchange,
RETURN: number of edits,
*/
const lens1 = len( s1 );
const lens2 = len( s2 );
if( lens1 === 0 || lens2 === 0 ){
return Infinity;
}
if( Wv === undefined ){ //optional param
Wv = [1, 1, 1];
}
if( lens1 < lens2 ){
return levenshtein( s2, s1 );
}
let m = []; // is matrix
let i;
let j;
// increment along the first column of each row
for( i = 0; i <= lens2; i+=1 ){
m[ i ] = [i];
}
// increment each column in the first row
for( j = 0; j <= lens1; j+=1 ){
m[ 0 ][ j ] = j;
}
// Fill in the rest of the matrix
for( i = 1; i <= lens2; i+=1 ){
for( j = 1; j <= lens1; j+=1 ){
if( s2[ i-1 ] === s1[ j-1 ] ){
m[i][j] = m[i-1][j-1];
} else {
m[i][j] = min(
(m[i-1][j-1] + Wv[0]), // substitution
(min(
(m[i][j-1] + Wv[1]), // insertion
(m[i-1][j] + Wv[2])
)
)
); // deletion
}
}
}
return m[ lens2 ][ lens1 ]; //returns distance; similarity is: 1 - (d/max(len(A,B)))
}
function LCS( vecA, vecB ){
/*
NAME: longest common subsequence (sequence is not substring, it is like sequencial but not next to eachother),
INPUT: vecA and vecB text representations,
RETURN: 0 (distant) and max(len(A),len(B)) (not distant),
*/
const lenA = len(vecA);
const lenB = len(vecB);
if( lenA === 0 || lenB === 0 ){
return 0;
}
let C = new Array( lenA ).fill( 0 ).map( () => new Array( lenB ).fill( 0 ));//[[0 for i in range(len(vecB))] for i in range(len(vecA))]
for( let i = 0; i < lenA; i+=1 ){
for( let j = 0; j < lenB; j+=1 ){
if( vecA[i] === vecB[j] ){
if( i !== 0 && j !== 0 ){
C[i][j] = C[i-1][j-1] + 1;
} else {
C[i][j] = 1;
}
} else {
if( i !== 0 && j !== 0 ){
C[i][j] = max( C[i][j-1], C[i-1][j] );
}
}
}
}
return C[lenA-1][lenB-1]; //SEE containedness: LCS/len(A) for B contained A or LCS/len(B) for A contained B
}
function LCF( vecA, vecB ){
/*
NAME: longest common substring (factor, sequential and next to each other members of a vector),
INPUT: vecA and vecB text representations,
RETURN: 0 (distant, nothing in common) and max(len(A),len(B)) (not distant),
*/
const lenA = len(vecA);
const lenB = len(vecB);
if( lenA === 0 || lenB === 0 ){
return 0;
}
let C = new Array( lenA ).fill( 0 ).map( () => new Array( lenB ).fill( 0 ));
let maxlen = 0;
for( let i = 0; i < lenA; i+=1 ){
for( let j = 0; j < lenB; j+=1 ){
if( vecA[i] === vecB[j] ){
if( i !== 0 && j !== 0 ){
C[i][j] = C[i-1][j-1] + 1;
if( maxlen < C[i][j] ){
maxlen = C[i][j];
}
} else {
C[i][j] = 1;
}
} else {
if( i !== 0 && j !== 0 ){
if( maxlen < C[i-1][j-1]){
maxlen = C[i-1][j-1];
}
}
C[i][j] = 0;
}
}
}
return maxlen;
}
function containednessLCS( a, b ){
/*
NAME: according to LCS the containedness of a in b or b in a,
INPUT: a and b text representations,
RETURN: 1 (contained) and 0 (not contained),
*/
const lenb = len(b);
const lena = len(a);
if( lena === 0 || lenb === 0 ){
return 0;
}
let lcsab = LCS(a,b);
if( lcsab === 0 ){
return lcsab;
} else {
return max( lcsab/lena, lcsab/lenb );
}
}
function containednessLCF( a, b ){
/*
NAME: according to LCF the containedness of a in b or b in a
INPUT: a and b text representations
RETURN: 1 (contained) and 0 (not contained),
*/
const lenb = len(b);
const lena = len(a);
if( lena === 0 || lenb === 0 ){
return 0;
}
let lcfab = LCF(a,b);
if( lcfab === 0 ){
return lcfab;
} else {
return max( lcfab/lena, lcfab/lenb);
}
}
function LCP( vecA, vecB ){
/*
NAME: longest commen prefix,
INPUT: vecA and vecB text representations,
RETURN: 0 (distant) and max(len(A),len(B)) (not distant),
*/
let sizeofcommenprefix = 0;
let lenMIN = min( len(vecA), len(vecB) );
if( lenMIN === 0 ){
return 0;
}
let i;
for( i = 0; i < lenMIN; i+=1 ){
if( vecA[i] === vecB[i] ){
sizeofcommenprefix += 1;
} else {
break;
}
}
return sizeofcommenprefix;
}
function bagdist( vecA, vecB ){
/*
NAME: bag distance (vecA is a bag is a sequencial, and next to eachother, redundant vector), aproximation of levensthein,
INPUT: vecA and vecB text representations,
RETURN: max(len(A),len(B)) (distant) and 0 (not distant),
*/
let eraseA = Array.from( vecA );
const lenA = len( vecA );
let eraseB = Array.from( vecB );
const lenB = len( vecB );
let sliceindex = -1;
let i = 0;
for( i = 0; i < lenA; i+=1 ){
sliceindex = eraseB.indexOf( vecA[ i ] );
if( sliceindex !== -1 ){
eraseB[ sliceindex ] = false;
}
}
let countinB = 0;
for( i = 0; i < lenB; i+=1 ){
if( eraseB[ i ] ){
countinB+=1;
}
}
sliceindex = -1;
for( i = 0; i < lenB; i+=1 ){
sliceindex = eraseA.indexOf( vecB[ i ] );
if( sliceindex !== -1 ){
eraseA[ sliceindex ] = false;
}
}
let countinA = 0;
for( i = 0; i < lenA; i+=1 ){
if( eraseA[ i ] ){
countinA+=1;
}
}
return max( countinA, countinB );
}
function JA( vecA, vecB ){
/*
NAME: jaro distance,
INPUT: vecA, vecB text represenations,
RETURN: Inf (distant) and 0.0 (not distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
if( lenB < lenA ){
return JA( vecB, vecA );
}
let maAB = max( lenB, lenA );
/*if( lenA != lenB ){
return maAB; //ist das der richtige rückgabewert
}HÄ ????????????*/
let matchDist = (maAB/2)-1;
let Amatches = new Array( lenA ).fill( False );
let Bmatches = new Array( lenB ).fill( False );
let matchcount = 0;
let traspocount = 0;
let i;
let j;
for( i = 0; i < lenA; i+=1 ){
let sta = Math.round(max( 0, i - matchDist ));
let en = Math.round(min( i + matchDist + 1, lenB ));
for( j = sta; j < en; j+= 1 ){
if( Bmatches[j] ){
continue;
}
if( vecA[i] !== vecB[j] ){
continue;
}
Amatches[i] = True;
Bmatches[j] = True;
matchcount+=1;
break;
}
}
if( matchcount === 0 ){
return maAB; //ist das der richtige rückgabewert
}
j = 0;
for( i = 0; i < lenA; i+=1 ){
if( !Amatches[i] ){
continue;
}
while( !Bmatches[j] ){
j+=1;
}
if( vecA[i] !== vecB[i] ){
traspocount += 1;
}
j+=1;
}
return ( ( (matchcount/lenA) + (matchcount/lenB) + (((matchcount-traspocount)/2)/matchcount)) / 3 );
}
function JAWI( vecA, vecB ){
/*
NAME: jaro winkler distance, transpositions,
INPUT: vecA, vecB text represenations,
RETURN: Inf (distant) and 0.0 (not distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
let onlyJaro = JA(vecA, vecB);
return onlyJaro + ((max(4, LCP(vecA, vecB))/10)*(1-onlyJaro));
}
function baire( vecA, vecB ){
/*
NAME: baire distance,
INPUT: vecA, vecB text represenations,
RETURN: Inf/1 (distant) and 0.0 (not distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
return ( 1 / (1 + LCP(vecA, vecB)) );
}
function generalizedcantor( vecA, vecB ){
/*
NAME: gen. cantor distance,
INPUT: vecA, vecB text represenations,
RETURN: Inf/1 (distant) and 0.0 (not distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
return Math.pow( (1/Math.E), (1 + LCP(vecA, vecB)) ); //a 1/Math.E can also be 1/2
}
function notgeneralizedcantor( vecA, vecB ){
/*
NAME: not gen. cantor distance,
INPUT: vecA, vecB text represenations,
RETURN: Inf/1 (distant) and 0.0 (not distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
return Math.pow( (1/Math.E), (1 + LCF(vecA, vecB)) );
}
function jaccardMASZzwei( vecA, vecB ){
/*
NAME: derived from jaccard distance, transpositions,
INPUT: vecA, vecB text represenations,
RETURN: Inf/1 (not distant) and 0.0 (distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
let setA = set( vecA );
let setB = set( vecB );
return (1.0 - parseFloat( parseFloat( len( SetSymDiff(setA, setB) )) / parseFloat( len( SetUnsion( setB, setA ) ) ) ) );
}
function jaccardMASZ( vecA, vecB ){
/*
NAME: jaccard distance, transpositions,
INPUT: vecA, vecB text represenations,
RETURN: Inf/1 (distant) and 0.0 (not distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
let setA = set( vecA );
let setB = set( vecB );
return (1.0 - parseFloat( parseFloat( len( SetIntersection(setA, setB) )) / parseFloat( len( SetUnsion( setB, setA ) ) ) ) );
}
function cosineMASZ( vecA, vecB ){
/*
NAME: cosine distance,
INPUT: vecA, vecB text represenations,
RETURN: Inf/1 (distant) and 0.0 (not distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
//müsste doch so klappen, oder was????
let setA = set( vecA );
let setB = set( vecB );
let unionAB = list( SetUnsion( setA, setB ) );
//occurenz count of gram in A or B
let x = []; //A
let y = []; //B
const lenAB = len( unionAB );
for( let i = 0; i < lenAB; i+=1 ){
let currcount = 0;
for(let j = 0; j < lenA; j+=1 ){
if( unionAB[ i ] === vecA[ j ] ){
currcount += 1;
}
}
x.push( currcount );
currcount = 0;
for( let j = 0; j < lenB; j+=1 ){
if( unionAB[ i ] === vecB[ j ] ){
currcount += 1;
}
}
y.push( currcount );
}
let summederquadrateA = 0;
let summederquadrateB = 0;
let scalarprod = 0;
const lenx = len(x);
for( let u = 0; u < lenx; u+=1 ){
summederquadrateA += x[ u ] * x[ u ];
summederquadrateB += y[ u ] * y[ u ];
scalarprod += x[ u ] * y[ u ];
}
let vecnormA = Math.sqrt( summederquadrateA );
let vecnormB = Math.sqrt( summederquadrateB );
return 1 - ( scalarprod/ ( vecnormA*vecnormB ) );
}
function quadradiffMASZ( vecA, vecB ){
/*
NAME: quadratic difference distance,
# vec A and B are arrays of ngrams or silben, quadraDiff is a messure taken from the haufigkeitsvektor of A and B
INPUT: vecA, vecB text represenations,
RETURN: Inf (distant) and 0.0 (not distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
//müsste doch so klappen, oder was????
let setA = set(vecA);
let setB = set(vecB);
let unionAB = list( SetUnsion(setA, setB) );
//occurenz count of gram in A or B
let x = []; //A
let y = []; //B
const lenAB = len( unionAB );
for( let i = 0; i < lenAB; i+=1 ){
let currcount = 0;
for(let j = 0; j < lenA; j+=1 ){
if( unionAB[ i ] === vecA[ j ] ){
currcount += 1;
}
}
x.push( currcount );
currcount = 0;
for( let j = 0; j < lenB; j+=1 ){
if( unionAB[ i ] === vecB[ j ] ){
currcount += 1;
}
}
y.push( currcount );
}
let sumitup = 0;
const lenx = len( x );
for( let u = 0; u < lenx; u+=1 ){
sumitup += ( Math.abs(x[ u ] - y[ u ]) )*( Math.abs(x[ u ] - y[ u ]) );
}
return Math.sqrt( sumitup );
}
function diceMASZ( vecA, vecB ){
/*
NAME: dice coefficent distance,
INPUT: vecA, vecB text represenations,
RETURN: Inf/1 (distant) and 0.0 (not distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
let setA = set(vecA);
let setB = set(vecB);
return 1.0-parseFloat((2.0*parseFloat(len( SetIntersection(setA, setB) )))/parseFloat(len(setA)+len(setB)))
}
function markingmetric( vecA, vecB ){
/*
NAME: marking distance,
# https://www.sciencedirect.com/science/article/pii/0166218X88900765
# wir untersuchen die Übergränge ist eine übergang nicht Teil des anderen, dann merke die position des buchstabens der in gelöscht werden muss, entweder einer oder beide
INPUT: vecA, vecB text represenations,
RETURN: Inf (distant) and 0.0 (not distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
let posesA = [];
let i = 0;
for(i = 1; i < lenA; i++ ){
let iba = vecB.indexOf( vecA[i-1] );
let ibb = vecB.indexOf( vecA[i] );
if( iba !== -1 && ibb !== -1 ){
if( !(Math.abs(iba-ibb) === 1) ){
posesA.push( i ); //völlig egal welcher index aufgeschrieben wird
}
} else {
if( iba === -1 && ibb === -1 ){
posesA.push( i-1 );
posesA.push( i );
} else {
posesA.push( i-1 );
}
}
}
let posesB = [];
for( i = 1; i < lenB; i++ ){
let iaa = vecA.indexOf( vecB[i-1] );
let iab = vecA.indexOf( vecB[i] );
if( iaa !== -1 && iab !== -1 ){
if( !(Math.abs(iaa-iab) === 1) ){
posesB.push( i ); //völlig egal welcher index aufgeschrieben wird
}
} else {
if( iaa === -1 && iab === -1 ){
posesB.push( i-1 );
posesB.push( i );
} else {
posesB.push( i-1 );
}
}
}
return Math.log( ( len(posesA)+1 )*( len(posesB)+1 ) );
}
function setdiffmetric( vecA, vecB ){
/*
NAME: set diff distance, derived from marking metric, containedness gedanken
INPUT: vecA, vecB text represenations,
RETURN: Inf (distant) and 0.0 (not distant) ???,
*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
let setA = set( vecA );
let setB = set( vecB );
let AB = SetDiff( setA, setB );
let BA = SetDiff( setB, setA );
const ABlen = len( AB );
const BAlen = len( BA );
return Math.log( ( ABlen+1 )*( BAlen+1 ) );
}
/*------------------------------------------------------------------------------
SPEZIELLE MASZE
------------------------------------------------------------------------------*/
function weightedngram( n, vecA, vecB, known ){
let distis = Infinity;
//known a letter statistic of ALL the corpus or a selected target subset or a in general asymetric heuristics
//str1 is how distant from str2 and vise versa
//at fisrt we come a Setdiffence of n-gram rep of str1 and str2
//let vecA = ngram( str1, n, False );
//let vecB = ngram( str2, n, False );
/*const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
let setA = set( vecA );
let setB = set( vecB );
let AB = SetDiff( setA, setB );
let BA = SetDiff( setB, setA );
const ABlen = len( AB );
const BAlen = len( BA );
let sd = Math.log( ( ABlen+1 )*( BAlen+1 ) );*/
const lenA = len( vecA );
const lenB = len( vecB );
if( lenA === 0 || lenB === 0 ){
return Infinity;
}
let setA = set(vecA);
let setB = set(vecB);
distis = 1.0-parseFloat((2.0*parseFloat(len( SetIntersection(setA, setB) )))/parseFloat(len(setA)+len(setB)))
if( distis != 1 ){
let I = SetIntersection( setA, setB );
let lI = len(I);
//let W = 0;
let WW = 0;
/* for( let t = 0; t < lI; t += 1){
let gram = I[t];
let wtemp = 0;
for( let r = 0; r < n; r += 1 ){
if( known[ gram[r] ] ){
wtemp += (1-known[ gram[r] ]);
} else {
wtemp += 1;
}
}
wtemp /= n;
W += wtemp;
}
*/
let nB = {};
for( let t = 0; t < lI; t += 1){
let gram = I[t];
for( let r = 0; r < n; r += 1 ){
if( nB[ gram[r] ] ){
nB[ gram[r] ] += 1;
} else {
nB[ gram[r] ] = 1;
}
}
}
let countnb = 0;
for( let n in nB ){
if( known[ n ] ){
WW += (1-known[ n ]);
} else {
WW += 1;
}
countnb += 1;
}
WW /= countnb;
//W /= lI;
/*if(distis*W != distis*WW){
console.log(W, distis*W, WW, distis*WW, distis);
}*/
distis = distis*WW;
}
return distis;
}
/*
Usage Summary of distances:
WLEV( A, B, Wv, Ws )
LEVDAM( s1, s2, Wv )
levenshtein( s1, s2, Wv )
LCS( vecA, vecB )
LCF( vecA, vecB )
containednessLCS( a, b )
containednessLCF( a, b )
LCP( vecA, vecB )
bagdist( vecA, vecB )
JA( vecA, vecB )
JAWI( vecA, vecB )
baire( vecA, vecB )
generalizedcantor( vecA, vecB )
notgeneralizedcantor( vecA, vecB )
jaccardMASZzwei( vecA, vecB )
jaccardMASZ( vecA, vecB )
cosineMASZ( vecA, vecB )
quadradiffMASZ( vecA, vecB )
diceMASZ( vecA, vecB )
markingmetric( vecA, vecB )
setdiffmetric( vecA, vecB )
*/