fsa_state.php 2.8 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
<?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.
 */

class phpMorphy_Link_Base {
	protected
		$fsa,
		$trans,
		$raw_trans;
	
	function phpMorphy_Link_Base(phpMorphy_Fsa_Interface $fsa, $trans, $rawTrans) {
		$this->fsa = $fsa;
		$this->trans = $trans;
		$this->raw_trans = $rawTrans;
	}
	
	function isAnnotation() { }
	function getTrans() { return $this->trans; }
	function getFsa() { return $this->fsa; }
	function getRawTrans() { return $this->raw_trans; }
};

/**
 * This class represent "normal" link i.e. link that points to automat state
 */
class phpMorphy_Link extends phpMorphy_Link_Base {
	function isAnnotation() { return false; }
	
	function getDest() { return $this->trans['dest']; }
	function getAttr() { return $this->trans['attr']; }
	
	function getTargetState() {
		return $this->createState($this->trans['dest']);
	}
	
	protected function createState($index) {
		return new phpMorphy_State($this->fsa, $index);
	}
}

class phpMorphy_Link_Annot extends phpMorphy_Link_Base {
	function isAnnotation() { return true; }
	
	function getAnnotation() {
		return $this->fsa->getAnnot($this->raw_trans);
	}
};

class phpMorphy_State {
	protected
		$fsa,
		$transes,
		$raw_transes;
	
	function phpMorphy_State(phpMorphy_Fsa_Interface $fsa, $index) {
		$this->fsa = $fsa;
		
		$this->raw_transes = $fsa->readState($index);
		$this->transes = $fsa->unpackTranses($this->raw_transes);
	}
	
	function getLinks() {
		$result = array();
		
		for($i = 0, $c = count($this->transes); $i < $c; $i++) {
			$trans = $this->transes[$i];
		
			if(!$trans['term']) {
				$result[] = $this->createNormalLink($trans, $this->raw_transes[$i]);
			} else {
				$result[] = $this->createAnnotLink($trans, $this->raw_transes[$i]);
			}
		}
		
		return $result;
	}
	
	function getSize() { return count($this->transes); }
	
	protected function createNormalLink($trans, $raw) {
		return new phpMorphy_Link($this->fsa, $trans, $raw);
	}
	
	protected function createAnnotLink($trans, $raw) {
		return new phpMorphy_Link_Annot($this->fsa, $trans, $raw);
	}
};