graminfo.php 9.72 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
 /**
 * This file is part of phpMorphy library
 *
 * Copyright c 2007-2008 Kamaev Vladimir <heromantor@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

interface phpMorphy_GramInfo_Interace {
    /**
     * Returns langugage for graminfo file
     * @return string
     */
    function getLocale();
    
    /**
     * Return encoding for graminfo file
     * @return string
     */
    function getEncoding();
    
    /**
     * Return size of character (cp1251 - 1, utf8 - 1, utf16 - 2, utf32 - 4 etc)
     * @return int
     */
    function getCharSize();
    
    /**
     * Return end of string value (usually string with \0 value of char_size + 1 length)
     * @return string
     */
    function getEnds();
    
    /**
     * Reads graminfo header
     *
     * @param int $offset
     * @return array
     */
    function readGramInfoHeader($offset);

    /**
     * Returns size of header struct
     */
    function getGramInfoHeaderSize();
    
    /**
     * Read ancodes section for header retrieved with readGramInfoHeader
     *
     * @param array $info
     * @return array
     */
    function readAncodes($info);
    
    /**
     * Read flexias section for header retrieved with readGramInfoHeader
     *
     * @param array $info
     * @return array
     */
    function readFlexiaData($info);
    
    /**
     * Read all graminfo headers offsets, which can be used latter for readGramInfoHeader method
     * @return array
     */
    function readAllGramInfoOffsets();
    
    function getHeader();
    function readAllPartOfSpeech();
    function readAllGrammems();
    function readAllAncodes();
}
 
abstract class phpMorphy_GramInfo implements phpMorphy_GramInfo_Interace {
    const HEADER_SIZE = 128;
    
    protected
        $resource,
        $header,
        $ends,
        $ends_size;
    
    protected function __construct($resource, $header) {
        $this->resource = $resource;
        $this->header = $header;
        
        $this->ends = str_repeat("\0", $header['char_size'] + 1);
        $this->ends_size = $GLOBALS['__phpmorphy_strlen']($this->ends);
    }
    
    static function create(phpMorphy_Storage $storage, $lazy) {
        if($lazy) {
            return new phpMorphy_GramInfo_Proxy($storage);
        }
        
        $header = phpMorphy_GramInfo::readHeader(
            $storage->read(0, self::HEADER_SIZE)
        );
        
        if(!phpMorphy_GramInfo::validateHeader($header)) {
            throw new phpMorphy_Exception('Invalid graminfo format');
        }
        
        $storage_type = $storage->getTypeAsString();
        $file_path = dirname(__FILE__) . "/access/graminfo_{$storage_type}.php";
        $clazz = 'phpMorphy_GramInfo_' . ucfirst($storage_type);
        
        require_once($file_path);
        return new $clazz($storage->getResource(), $header);
    }
    
    function getLocale() {
        return $this->header['lang'];
    }
    
    function getEncoding() {
        return $this->header['encoding'];
    }
    
    function getCharSize() {
        return $this->header['char_size'];
    }
    
    function getEnds() {
        return $this->ends;
    }
    
    function getHeader() {
        return $this->header;
    }
    
    static protected function readHeader($headerRaw) {
        $header = unpack(
            'Vver/Vis_be/Vflex_count_old/' .
            'Vflex_offset/Vflex_size/Vflex_count/Vflex_index_offset/Vflex_index_size/' .
            'Vposes_offset/Vposes_size/Vposes_count/Vposes_index_offset/Vposes_index_size/' .
            'Vgrammems_offset/Vgrammems_size/Vgrammems_count/Vgrammems_index_offset/Vgrammems_index_size/' .
            'Vancodes_offset/Vancodes_size/Vancodes_count/Vancodes_index_offset/Vancodes_index_size/' .
            'Vchar_size/',
            $headerRaw
        );
        
        $offset = 24 * 4;
        $len = ord($GLOBALS['__phpmorphy_substr']($headerRaw, $offset++, 1));
        $header['lang'] = rtrim($GLOBALS['__phpmorphy_substr']($headerRaw, $offset, $len));
        
        $offset += $len;
        
        $len = ord($GLOBALS['__phpmorphy_substr']($headerRaw, $offset++, 1));
        $header['encoding'] = rtrim($GLOBALS['__phpmorphy_substr']($headerRaw, $offset, $len));
        
        return $header;
    }
    
    static protected function validateHeader($header) {
        if(
            3 != $header['ver'] ||
            1 == $header['is_be']
        ) {
            return false;
        }
        
        return true;
    }
    
    protected function cleanupCString($string) {
        if(false !== ($pos = $GLOBALS['__phpmorphy_strpos']($string, $this->ends))) {
            $string = $GLOBALS['__phpmorphy_substr']($string, 0, $pos);
        }
        
        return $string;
    }
    
    abstract protected function readSectionIndex($offset, $count);
    
    protected function readSectionIndexAsSize($offset, $count, $total_size) {
        if(!$count) {
            return array();
        }
        
        $index = $this->readSectionIndex($offset, $count);
        $index[$count] = $index[0] + $total_size;
        
        for($i = 0; $i < $count; $i++) {
            $index[$i] = $index[$i + 1] - $index[$i];
        }
        
        unset($index[$count]);
        
        return $index;
    }
};

class phpMorphy_GramInfo_Decorator implements phpMorphy_GramInfo_Interace {
    protected $info;
    
    function __construct(phpMorphy_GramInfo_Interace $info) {
        $this->info = $info;
    }
    
    function readGramInfoHeader($offset) { return $this->info->readGramInfoHeader($offset); }
    function getGramInfoHeaderSize() { return $this->info->getGramInfoHeaderSize($offset); }
    function readAncodes($info) { return $this->info->readAncodes($info); }
    function readFlexiaData($info) { return $this->info->readFlexiaData($info); }
    function readAllGramInfoOffsets() { return $this->info->readAllGramInfoOffsets(); }
    function readAllPartOfSpeech() { return $this->info->readAllPartOfSpeech(); }
    function readAllGrammems() { return $this->info->readAllGrammems(); }
    function readAllAncodes() { return $this->info->readAllAncodes(); }
    
    function getLocale()  { return $this->info->getLocale(); }
    function getEncoding()  { return $this->info->getEncoding(); }
    function getCharSize()  { return $this->info->getCharSize(); }
    function getEnds() { return $this->info->getEnds(); }
    function getHeader() { return $this->info->getHeader(); }
}

class phpMorphy_GramInfo_Proxy extends phpMorphy_GramInfo_Decorator {
    protected $storage;
    
    function __construct(phpMorphy_Storage $storage) {
        $this->storage = $storage;
        unset($this->info);
    }
    
    function __get($propName) {
        if($propName == 'info') {
            $this->info = phpMorphy_GramInfo::create($this->storage, false);
            unset($this->storage);
            return $this->info;
        }
        
        throw new phpMorphy_Exception("Unknown prop name '$propName'");
    }
}

class phpMorphy_GramInfo_Proxy_WithHeader extends phpMorphy_GramInfo_Proxy {
    protected
        $cache,
        $ends;
    
    function __construct(phpMorphy_Storage $storage, $cacheFile) {
        parent::__construct($storage);
        
        $this->cache = $this->readCache($cacheFile);
        $this->ends = str_repeat("\0", $this->getCharSize() + 1);
    }
    
    protected function readCache($fileName) {
        if(!is_array($result = include($fileName))) {
            throw new phpMorphy_Exception("Can`t get header cache from '$fileName' file'");
        }
        
        return $result;
    }
    
    function getLocale()  {
        return $this->cache['lang'];
    }
    
    function getEncoding()  {
        return $this->cache['encoding'];
    }
    
    function getCharSize()  {
        return $this->cache['char_size'];
    }
    
    function getEnds() {
        return $this->ends;
    }
    
    function getHeader() {
        return $this->cache;
    }
}

class phpMorphy_GramInfo_RuntimeCaching extends phpMorphy_GramInfo_Decorator {
    protected
        $flexia = array(),
        $ancodes = array();
    
    function readFlexiaData($info) {
        $offset = $info['offset'];
        
        if(!isset($this->flexia_all[$offset])) {
            $this->flexia_all[$offset] = $this->info->readFlexiaData($info);
        }
        
        return $this->flexia_all[$offset];
    }
}

class phpMorphy_GramInfo_AncodeCache extends phpMorphy_GramInfo_Decorator {
    public
        $hits = 0,
        $miss = 0;

    protected
        $cache;

    function __construct(phpMorphy_GramInfo_Interace $inner, $resource) {
        parent::__construct($inner);

        if(false === ($this->cache = unserialize($resource->read(0, $resource->getFileSize())))) {
            throw new phpMorphy_Exception("Can`t read ancodes cache");
        }
    }

    function readAncodes($info) {
        $offset = $info['offset'];

        if(isset($this->cache[$offset])) {
            $this->hits++;

            return $this->cache[$offset];
        } else {
            // in theory misses never occur
            $this->miss++;

            return parent::readAncodes($info);
        }
    }
}