1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
22
23 interface phpMorphy_GramTab_Interface {
24 function getGrammems($ancodeId);
25 function getPartOfSpeech($ancodeId);
26 function resolveGrammemIds($ids);
27 function resolvePartOfSpeechId($id);
28 function includeConsts();
29 function ancodeToString($ancodeId, $commonAncode = null);
30 function stringToAncode($string);
31 function toString($partOfSpeechId, $grammemIds);
32 }
33
34 class phpMorphy_GramTab_Empty implements phpMorphy_GramTab_Interface {
35 function getGrammems($ancodeId) { return array(); }
36 function getPartOfSpeech($ancodeId) { return 0; }
37 function resolveGrammemIds($ids) { return is_array($ids) ? array() : ''; }
38 function resolvePartOfSpeechId($id) { return ''; }
39 function includeConsts() { }
40 function ancodeToString($ancodeId, $commonAncode = null) { return ''; }
41 function stringToAncode($string) { return null; }
42 function toString($partOfSpeechId, $grammemIds) { return ''; }
43 }
44
45 class phpMorphy_GramTab_Proxy implements phpMorphy_GramTab_Interface {
46 protected $storage;
47
48 function __construct(phpMorphy_Storage $storage) {
49 $this->storage = $storage;
50 }
51
52 function getGrammems($ancodeId) {
53 return $this->__obj->getGrammems($ancodeId);
54 }
55
56 function getPartOfSpeech($ancodeId) {
57 return $this->__obj->getPartOfSpeech($ancodeId);
58 }
59
60 function resolveGrammemIds($ids) {
61 return $this->__obj->resolveGrammemIds($ids);
62 }
63
64 function resolvePartOfSpeechId($id) {
65 return $this->__obj->resolvePartOfSpeechId($id);
66 }
67
68 function includeConsts() {
69 return $this->__obj->includeConsts();
70 }
71
72 function ancodeToString($ancodeId, $commonAncode = null) {
73 return $this->__obj->ancodeToString($ancodeId, $commonAncode);
74 }
75
76 function stringToAncode($string) {
77 return $this->__obj->stringToAncode($string);
78 }
79
80 function toString($partOfSpeechId, $grammemIds) {
81 return $this->__obj->toString($partOfSpeechId, $grammemIds);
82 }
83
84 function __get($name) {
85 if($name === '__obj') {
86 $this->__obj = phpMorphy_GramTab::create($this->storage);
87 unset($this->storage);
88
89 return $this->__obj;
90 }
91
92 throw new phpMorphy_Exception("Invalid prop name '$name'");
93 }
94 }
95
96 class phpMorphy_GramTab implements phpMorphy_GramTab_Interface {
97 protected
98 $data,
99 $ancodes,
100 $grammems,
101
102 $poses;
103
104 protected function __construct(phpMorphy_Storage $storage) {
105 $this->data = unserialize($storage->read(0, $storage->getFileSize()));
106
107 if(false === $this->data) {
108 throw new phpMorphy_Exception("Broken gramtab data");
109 }
110
111 $this->grammems = $this->data['grammems'];
112 $this->poses = $this->data['poses'];
113 $this->ancodes = $this->data['ancodes'];
114 }
115
116
117 static function create(phpMorphy_Storage $storage) {
118 return new phpMorphy_GramTab($storage);
119 }
120
121 function getGrammems($ancodeId) {
122 if(!isset($this->ancodes[$ancodeId])) {
123 throw new phpMorphy_Exception("Invalid ancode id '$ancodeId'");
124 }
125
126 return $this->ancodes[$ancodeId]['grammem_ids'];
127 }
128
129 function getPartOfSpeech($ancodeId) {
130 if(!isset($this->ancodes[$ancodeId])) {
131 throw new phpMorphy_Exception("Invalid ancode id '$ancodeId'");
132 }
133
134 return $this->ancodes[$ancodeId]['pos_id'];
135 }
136
137 function resolveGrammemIds($ids) {
138 if(is_array($ids)) {
139 $result = array();
140
141 foreach($ids as $id) {
142 if(!isset($this->grammems[$id])) {
143 throw new phpMorphy_Exception("Invalid grammem id '$id'");
144 }
145
146 $result[] = $this->grammems[$id]['name'];
147 }
148
149 return $result;
150 } else {
151 if(!isset($this->grammems[$ids])) {
152 throw new phpMorphy_Exception("Invalid grammem id '$ids'");
153 }
154
155 return $this->grammems[$ids]['name'];
156 }
157 }
158
159 function resolvePartOfSpeechId($id) {
160 if(!isset($this->poses[$id])) {
161 throw new phpMorphy_Exception("Invalid part of speech id '$id'");
162 }
163
164 return $this->poses[$id]['name'];
165 }
166
167 function includeConsts() {
168 require_once(PHPMORPHY_DIR . '/gramtab_consts.php');
169 }
170
171 function ancodeToString($ancodeId, $commonAncode = null) {
172 if(isset($commonAncode)) {
173 $commonAncode = implode(',', $this->getGrammems($commonAncode)) . ',';
174 }
175
176 return
177 $this->getPartOfSpeech($ancodeId) . ' ' .
178 $commonAncode .
179 implode(',', $this->getGrammems($ancodeId));
180 }
181
182 protected function findAncode($partOfSpeech, $grammems) {
183 }
184
185 function stringToAncode($string) {
186 if(!isset($string)) {
187 return null;
188 }
189
190 if(!isset($this->__ancodes_map[$string])) {
191 throw new phpMorphy_Exception("Ancode with '$string' graminfo not found");
192 }
193
194 return $this->__ancodes_map[$string];
195 }
196
197 function toString($partOfSpeechId, $grammemIds) {
198 return $partOfSpeechId . ' ' . implode(',', $grammemIds);
199 }
200
201 protected function buildAncodesMap() {
202 $result = array();
203
204 foreach($this->ancodes as $ancode_id => $data) {
205 $key = $this->toString($data['pos_id'], $data['grammem_ids']);
206
207 $result[$key] = $ancode_id;
208 }
209
210 return $result;
211 }
212
213 function __get($propName) {
214 switch($propName) {
215 case '__ancodes_map':
216 $this->__ancodes_map = $this->buildAncodesMap();
217 return $this->__ancodes_map;
218 }
219
220 throw new phpMorphy_Exception("Unknown '$propName' property");
221 }
222 }
223
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-