Subversion Repositories Applications.dictionnaire

Rev

Rev 9 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9 raphael 1
// cache les association "mot dans le texte" => "mot du dictionnaire"
2
// obtenues à l'aide de levenshtein() et texte_to_mot()
3
var assoc_mot_clef = new Array();
4
 
5
function texte_to_mot(mot) {
6
	if(assoc_mot_clef[mot]) return assoc_mot_clef[mot];
7
 
8
	var proche = '';
9
	var lev = 99;
10
	$.each(motsAyantDefinition, function(i, item) {
11
		x = levenshtein(mot, item);
12
		if(x < lev) { lev = x; proche = item; }
13
	});
14
	assoc_mot_clef[mot] = proche;
15
	return proche;
16
}
17
 
18
function levenshtein (s1, s2) {
19
	// http://kevin.vanzonneveld.net
20
	// +            original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
21
	// +            bugfixed by: Onno Marsman
22
	// +             revised by: Andrea Giammarchi (http://webreflection.blogspot.com)
23
	// + reimplemented by: Brett Zamir (http://brett-zamir.me)
24
	// + reimplemented by: Alexander M Beedie
25
	// *                example 1: levenshtein('Kevin van Zonneveld', 'Kevin van Sommeveld');
26
	// *                returns 1: 3
27
	if (s1 == s2) {
28
		return 0;
29
	}
30
 
31
	var s1_len = s1.length;
32
	var s2_len = s2.length;
33
	if (s1_len === 0) {
34
		return s2_len;
35
	}
36
	if (s2_len === 0) {
37
		return s1_len;
38
	}
39
 
40
	// BEGIN STATIC
41
	var split = false;
42
	try {
43
		split = !('0')[0];
44
	} catch (e) {
45
		split = true; // Earlier IE may not support access by string index
46
	}
47
	// END STATIC
48
	if (split) {
49
		s1 = s1.split('');
50
		s2 = s2.split('');
51
	}
52
 
53
	var v0 = new Array(s1_len + 1);
54
	var v1 = new Array(s1_len + 1);
55
 
56
	var s1_idx = 0,
57
    s2_idx = 0,
58
    cost = 0;
59
	for (s1_idx = 0; s1_idx < s1_len + 1; s1_idx++) {
60
		v0[s1_idx] = s1_idx;
61
	}
62
	var char_s1 = '',
63
    char_s2 = '';
64
	for (s2_idx = 1; s2_idx <= s2_len; s2_idx++) {
65
		v1[0] = s2_idx;
66
		char_s2 = s2[s2_idx - 1];
67
 
68
		for (s1_idx = 0; s1_idx < s1_len; s1_idx++) {
69
			char_s1 = s1[s1_idx];
70
			cost = (char_s1 == char_s2) ? 0 : 1;
71
			var m_min = v0[s1_idx + 1] + 1;
72
			var b = v1[s1_idx] + 1;
73
			var c = v0[s1_idx] + cost;
74
			if (b < m_min) {
75
				m_min = b;
76
			}
77
			if (c < m_min) {
78
				m_min = c;
79
			}
80
			v1[s1_idx + 1] = m_min;
81
		}
82
		var v_tmp = v0;
83
		v0 = v1;
84
		v1 = v_tmp;
85
	}
86
	return v0[s1_len];
87
}