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