Subversion Repositories Applications.dictionnaire

Rev

Go to most recent revision | Details | 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
// http://stackoverflow.com/questions/1960473/unique-values-in-an-array
19
function getUnique(tableau) {
20
	var u = {}, a = [];
21
	for(var i = 0, l = tableau.length; i < l; ++i){
22
		if(u.hasOwnProperty(tableau[i])) {
23
			continue;
24
		}
25
		a.push(tableau[i]);
26
		u[tableau[i]] = 1;
27
	}
28
	return a;
29
}
30
 
31
// http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript
32
function clean(tableau, deleteValue) {
33
	for (var i = 0; i < tableau.length; i++) {
34
		if (tableau[i] == deleteValue) {
35
			tableau.splice(i, 1);
36
			i--;
37
		}
38
	}
39
	return tableau;
40
}
41
 
42
function trim(myString) {
43
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
44
}
45
 
46
function levenshtein (s1, s2) {
47
	// http://kevin.vanzonneveld.net
48
	// +            original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
49
	// +            bugfixed by: Onno Marsman
50
	// +             revised by: Andrea Giammarchi (http://webreflection.blogspot.com)
51
	// + reimplemented by: Brett Zamir (http://brett-zamir.me)
52
	// + reimplemented by: Alexander M Beedie
53
	// *                example 1: levenshtein('Kevin van Zonneveld', 'Kevin van Sommeveld');
54
	// *                returns 1: 3
55
	if (s1 == s2) {
56
		return 0;
57
	}
58
 
59
	var s1_len = s1.length;
60
	var s2_len = s2.length;
61
	if (s1_len === 0) {
62
		return s2_len;
63
	}
64
	if (s2_len === 0) {
65
		return s1_len;
66
	}
67
 
68
	// BEGIN STATIC
69
	var split = false;
70
	try {
71
		split = !('0')[0];
72
	} catch (e) {
73
		split = true; // Earlier IE may not support access by string index
74
	}
75
	// END STATIC
76
	if (split) {
77
		s1 = s1.split('');
78
		s2 = s2.split('');
79
	}
80
 
81
	var v0 = new Array(s1_len + 1);
82
	var v1 = new Array(s1_len + 1);
83
 
84
	var s1_idx = 0,
85
    s2_idx = 0,
86
    cost = 0;
87
	for (s1_idx = 0; s1_idx < s1_len + 1; s1_idx++) {
88
		v0[s1_idx] = s1_idx;
89
	}
90
	var char_s1 = '',
91
    char_s2 = '';
92
	for (s2_idx = 1; s2_idx <= s2_len; s2_idx++) {
93
		v1[0] = s2_idx;
94
		char_s2 = s2[s2_idx - 1];
95
 
96
		for (s1_idx = 0; s1_idx < s1_len; s1_idx++) {
97
			char_s1 = s1[s1_idx];
98
			cost = (char_s1 == char_s2) ? 0 : 1;
99
			var m_min = v0[s1_idx + 1] + 1;
100
			var b = v1[s1_idx] + 1;
101
			var c = v0[s1_idx] + cost;
102
			if (b < m_min) {
103
				m_min = b;
104
			}
105
			if (c < m_min) {
106
				m_min = c;
107
			}
108
			v1[s1_idx + 1] = m_min;
109
		}
110
		var v_tmp = v0;
111
		v0 = v1;
112
		v1 = v_tmp;
113
	}
114
	return v0[s1_len];
115
}
116