1 : <?php
2 :
3 : /**
4 : * Dictionaries of Old French and Latin
5 : *
6 : * PHP 5
7 : *
8 : * @category DicFro
9 : * @package View
10 : * @subpackage Helper
11 : * @author Michel Corne <mcorne@yahoo.com>
12 : * @copyright 2008-2010 Michel Corne
13 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
14 : * @link http://www.micmap.org/dicfro
15 : */
16 :
17 : require_once 'View/Helper/Base.php';
18 :
19 : /**
20 : * Words View Helper
21 : *
22 : * @category DicFro
23 : * @package View
24 : * @subpackage Helper
25 : * @author Michel Corne <mcorne@yahoo.com>
26 : * @copyright 2008-2010 Michel Corne
27 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
28 : */
29 :
30 : class View_Helper_Words extends View_Helper_Base
31 : {
32 :
33 : /**
34 : * Word forms separator
35 : */
36 : const FORM_SEPARATOR = ' *[,;] *';
37 :
38 : /**
39 : * Lemma and part of speech template
40 : */
41 : const LEMMA_POF_TPL = '%s, %s';
42 :
43 : /**
44 : * Text template
45 : */
46 : const TEXT_TPL = '%s : %s';
47 :
48 : /**
49 : * Returns the lemma and the part of speech of a word
50 : *
51 : * @param array $word the word details
52 : * @return string the lemma and the part of speech if any
53 : */
54 : public function getLemmaAndPof($word)
55 : {
56 4 : return empty($word['pof'])?
57 4 : $word['lemma'] :
58 4 : sprintf(self::LEMMA_POF_TPL, $word['lemma'], $word['pof']);
59 : }
60 :
61 : /**
62 : * Extracts the main forms or variants of a word
63 : *
64 : * @param string $type the type of form, ex. "main"
65 : * @return array the word forms
66 : */
67 : public function extractForms($type)
68 : {
69 1 : $forms = array();
70 :
71 1 : foreach($this->view->identifiedWords as $word) {
72 1 : empty($word[$type]) or
73 1 : $forms = array_merge($forms, $this->getForms($word, $type));
74 1 : }
75 :
76 1 : return $this->sortForms($forms);
77 : }
78 :
79 : /**
80 : * Extracts the other forms (not main nor variants) of a word
81 : *
82 : * @return array the word forms
83 : */
84 : public function extractOtherForms()
85 : {
86 1 : $forms = array();
87 :
88 1 : foreach($this->view->identifiedWords as $word) {
89 1 : empty($word['main']) and empty($word['variants']) and
90 1 : $forms = array_merge($forms, $this->getForms($word));
91 1 : }
92 :
93 1 : return $this->sortForms($forms);
94 : }
95 :
96 : /**
97 : * Returs the forms of a word
98 : *
99 : * @param array $word the word details
100 : * @param string $type the type of form, ex. "main"
101 : * @return array the forms of the word
102 : */
103 : public function getForms($word, $type = null)
104 : {
105 3 : $lemmaAndPof = $this->getLemmaAndPof($word);
106 :
107 : // extract the word forms
108 : $words = $type?
109 3 : preg_split(self::FORM_SEPARATOR, $word[$type]) :
110 3 : array($word['lemma']);
111 :
112 3 : $forms = array();
113 :
114 3 : foreach($words as $form) {
115 3 : $forms[] = array(
116 3 : 'value' => $form,
117 : 'text' => $type?
118 3 : sprintf(self::TEXT_TPL, $form, $lemmaAndPof) :
119 3 : $lemmaAndPof,
120 : );
121 3 : }
122 :
123 3 : return $forms;
124 : }
125 :
126 : /**
127 : * Sorts the forms
128 : *
129 : * @param array $forms the forms to sort
130 : * @return array the sorted forms
131 : */
132 : public function sortForms($forms)
133 : {
134 3 : $sorted = array();
135 :
136 3 : foreach($forms as $form) {
137 3 : list($key) = explode(',', $form['text']);
138 3 : $key = $this->string->utf8toASCII($key);
139 3 : $sorted[$key] = $form;
140 3 : }
141 :
142 3 : ksort($sorted);
143 :
144 3 : return array_values($sorted);
145 : }
|