[SPIP] +2.1.12
[velocampus/web/www.git] / www / ecrire / inc / mots.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2011 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) return;
14 include_spip('inc/actions');
15
16 // ne pas faire d'erreur si les chaines sont > 254 caracteres
17 // http://doc.spip.org/@levenshtein255
18 function levenshtein255 ($a, $b) {
19 $a = substr($a, 0, 254);
20 $b = substr($b, 0, 254);
21 return @levenshtein($a,$b);
22 }
23
24 // reduit un mot a sa valeur translitteree et en minuscules
25 // http://doc.spip.org/@reduire_mot
26 function reduire_mot($mot) {
27 return strtr(
28 translitteration(trim($mot)),
29 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
30 'abcdefghijklmnopqrstuvwxyz'
31 );
32 }
33
34 // http://doc.spip.org/@mots_ressemblants
35 function mots_ressemblants($mot, $table_mots, $table_ids='') {
36
37 $result = array();
38
39 if (!$table_mots) return $result;
40
41 $lim = 2;
42 $nb = 0;
43 $opt = 1000000;
44 $mot_opt = '';
45 $mot = reduire_mot($mot);
46 $len = strlen($mot);
47
48 while (!$nb AND $lim < 10) {
49 reset($table_mots);
50 if ($table_ids) reset($table_ids);
51 while (list(, $val) = each($table_mots)) {
52 if ($table_ids) list(, $id) = each($table_ids);
53 else $id = $val;
54 $val2 = trim($val);
55 if ($val2) {
56 if (!isset($distance[$id])) {
57 $val2 = reduire_mot($val2);
58 $len2 = strlen($val2);
59 if ($val2 == $mot)
60 $m = -2; # resultat exact
61 else if (substr($val2, 0, $len) == $mot)
62 $m = -1; # sous-chaine
63 else {
64 # distance
65 $m = levenshtein255($val2, $mot);
66 # ne pas compter la distance due a la longueur
67 $m -= max(0, $len2 - $len);
68 }
69 $distance[$id] = $m;
70 } else $m = 0;
71 if ($m <= $lim) {
72 $selection[$id] = $m;
73 if ($m < $opt) {
74 $opt = $m;
75 $mot_opt = $val;
76 }
77 $nb++;
78 }
79 }
80 }
81 $lim += 2;
82 }
83
84 if (!$nb) return $result;
85 reset($selection);
86 if ($opt > -1) {
87 $moy = 1;
88 while(list(, $val) = each($selection)) $moy *= $val;
89 if($moy) $moy = pow($moy, 1.0/$nb);
90 $lim = ($opt + $moy) / 2;
91 }
92 else $lim = -1;
93
94 reset($selection);
95 while (list($key, $val) = each($selection)) {
96 if ($val <= $lim) {
97 $result[] = $key;
98 }
99 }
100 return $result;
101 }
102
103
104 /*
105 * Affiche la liste des mots-cles associes a l'objet specifie
106 * plus le formulaire d'ajout de mot-cle
107 */
108
109 // http://doc.spip.org/@affiche_mots_ressemblant
110 function affiche_mots_ressemblant($cherche_mot, $objet, $id_objet, $resultat, $table, $table_id, $url_base)
111 {
112 $les_mots = sql_in('id_mot', $resultat);
113 $res = sql_allfetsel("*", "spip_mots", $les_mots, "", "titre", "17");
114
115 foreach ($res as $k => $row) {
116 $id_mot = $row['id_mot'];
117 $titre = $row['titre'];
118 $type = typo($row['type']);
119 $descriptif = $row['descriptif'];
120
121 $res[$k]= ajax_action_auteur('editer_mots', "$id_objet,,$table,$table_id,$objet,$id_mot", $url_base, "$table_id=$id_objet", array(typo($titre),' title="' . _T('info_ajouter_mot') .'"'),"&id_objet=$id_objet&objet=$objet") .
122 (!$descriptif ? '' : ("\n(<span class='spip_xx-small'>".supprimer_tags(couper(propre($descriptif), 100)).")</span><br />\n"));
123
124 }
125
126 $res2 = ($type
127 ? "<strong>$type</strong>&nbsp;: "
128 : '' )
129 . _T('info_plusieurs_mots_trouves', array('cherche_mot' => $cherche_mot))
130 ."<br />";
131
132 if (count($resultat) > 17)
133 $res2 .= "<br /><strong>" ._T('info_trop_resultat', array('cherche_mot' => $cherche_mot)) ."</strong><br />\n";
134
135 return $res2 . '<ul><li>' . join("</li>\n<li>", $res) . '</li></ul>';
136 }
137
138 ?>