Index: trunk/extensions/Lingo/LingoBackend.php |
— | — | @@ -28,7 +28,7 @@ |
29 | 29 | |
30 | 30 | /** |
31 | 31 | * |
32 | | - * @return Boolean true, if a next element is available |
| 32 | + * @return the next element or null |
33 | 33 | */ |
34 | 34 | abstract public function next(); |
35 | 35 | |
Index: trunk/extensions/Lingo/LingoParser.php |
— | — | @@ -95,16 +95,8 @@ |
96 | 96 | $backend = &$this->mLingoBackend; |
97 | 97 | |
98 | 98 | // assemble the result array |
99 | | - $this->mLingoArray = array(); |
100 | 99 | while ( $elementData = $backend->next() ) { |
101 | | - |
102 | | - if ( array_key_exists( ( $term = $elementData[LingoElement::ELEMENT_TERM] ), $this->mLingoArray ) ) { |
103 | | - $this->mLingoArray[$term]->addDefinition( $elementData ); |
104 | | - } else { |
105 | | - $this->mLingoArray[$term] = new LingoElement( $term, $elementData ); |
106 | | - } |
107 | | - |
108 | | - $this->mLingoTree->addTerm( $term, $elementData ); |
| 100 | + $this->mLingoTree->addTerm( $elementData[LingoElement::ELEMENT_TERM], $elementData ); |
109 | 101 | } |
110 | 102 | |
111 | 103 | wfProfileOut( __METHOD__ ); |
Index: trunk/extensions/Lingo/LingoTree.php |
— | — | @@ -26,8 +26,8 @@ |
27 | 27 | class LingoTree { |
28 | 28 | |
29 | 29 | private $mTree = array(); |
30 | | - private $mDefinition = null; |
31 | | - private $mMinLength = -1; |
| 30 | + private $mList = array(); |
| 31 | + private $mMinLength = 1000; |
32 | 32 | |
33 | 33 | /** |
34 | 34 | * Adds a string to the Lingo Tree |
— | — | @@ -38,49 +38,50 @@ |
39 | 39 | return; |
40 | 40 | } |
41 | 41 | |
42 | | - $matches; |
43 | | - preg_match_all( '/[[:alpha:]]+|[^[:alpha:]]/u', $term, $matches ); |
| 42 | + if ( isset( $this->mList[$term] ) ) { // term exists, store 2nd definition |
44 | 43 | |
45 | | - $this->addElement( $matches[0], $term, $definition ); |
| 44 | + $this->mList[$term][-1]->addDefinition( $definition ); |
46 | 45 | |
47 | | - if ( $this->mMinLength > -1 ) { |
48 | | - $this->mMinLength = min( array( $this->mMinLength, strlen( $term ) ) ); |
49 | 46 | } else { |
50 | | - $this->mMinLength = strlen( $term ); |
| 47 | + |
| 48 | + $matches; |
| 49 | + preg_match_all( '/[[:alpha:]]+|[^[:alpha:]]/u', $term, $matches ); |
| 50 | + |
| 51 | + $this->mList[$term] = $this->addElement( $matches[0], $term, $definition ); |
| 52 | + |
| 53 | + $this->mMinLength = min( array($this->mMinLength, strlen( $term )) ); |
51 | 54 | } |
52 | 55 | } |
53 | 56 | |
54 | 57 | /** |
55 | | - * Recursively adds an element to the Lingo Tree |
| 58 | + * Adds an element to the Lingo Tree |
56 | 59 | * |
57 | 60 | * @param array $path |
58 | 61 | * @param <type> $index |
| 62 | + * @return Array the tree node the element was stored in |
59 | 63 | */ |
60 | | - protected function addElement( Array &$path, &$term, &$definition ) { |
| 64 | + protected function &addElement( Array &$path, &$term, &$definition ) { |
| 65 | + |
| 66 | + $tree = &$this->mTree; |
| 67 | + |
61 | 68 | // end of path, store description; end of recursion |
62 | | - if ( $path == null ) { |
63 | | - $this -> addDefinition( $term, $definition ); |
64 | | - } else { |
65 | | - $step = array_shift( $path ); |
| 69 | + while ( $step = array_shift( $path ) ) { |
66 | 70 | |
67 | | - if ( !array_key_exists( $step, $this->mTree ) ) { |
68 | | - $this->mTree[$step] = new LingoTree(); |
| 71 | + if ( !isset( $tree[$step] ) ) { |
| 72 | + $tree[$step] = array(); |
69 | 73 | } |
70 | 74 | |
71 | | - $this->mTree[$step]->addElement( $path, $term, $definition ); |
| 75 | + $tree = &$tree[$step]; |
| 76 | + |
72 | 77 | } |
73 | | - } |
74 | 78 | |
75 | | - /** |
76 | | - * Adds a defintion to the treenodes list of definitions |
77 | | - * @param <type> $definition |
78 | | - */ |
79 | | - protected function addDefinition( &$term, &$definition ) { |
80 | | - if ( $this->mDefinition ) { |
81 | | - $this->mDefinition->addDefinition( $definition ); |
| 79 | + if ( isset( $tree[-1] ) ) { |
| 80 | + $tree[-1]->addDefinition( $definition ); |
82 | 81 | } else { |
83 | | - $this->mDefinition = new LingoElement( $term, $definition ); |
| 82 | + $tree[-1] = new LingoElement( $term, $definition ); |
84 | 83 | } |
| 84 | + |
| 85 | + return $tree; |
85 | 86 | } |
86 | 87 | |
87 | 88 | function getMinTermLength() { |
— | — | @@ -99,7 +100,7 @@ |
100 | 101 | |
101 | 102 | // Did we find the start of a term? |
102 | 103 | if ( array_key_exists( $currLex, $this->mTree ) ) { |
103 | | - list( $lastindex, $definition ) = $this->mTree[$currLex]->findNextTermNoSkip( $lexemes, $index, $countLexemes ); |
| 104 | + list( $lastindex, $definition ) = $this->findNextTermNoSkip( $this->mTree[$currLex], $lexemes, $index, $countLexemes ); |
104 | 105 | } |
105 | 106 | |
106 | 107 | // this will increase the index even if we found something; |
— | — | @@ -109,19 +110,19 @@ |
110 | 111 | |
111 | 112 | wfProfileOut( __METHOD__ ); |
112 | 113 | if ( $definition ) { |
113 | | - return array( $index - $start - 1, $lastindex - $index + 2, $definition ); |
| 114 | + return array($index - $start - 1, $lastindex - $index + 2, $definition); |
114 | 115 | } else { |
115 | | - return array( $index - $start, 0, null ); |
| 116 | + return array($index - $start, 0, null); |
116 | 117 | } |
117 | 118 | } |
118 | 119 | |
119 | | - function findNextTermNoSkip( &$lexemes, $index, $countLexemes ) { |
| 120 | + function findNextTermNoSkip( Array &$tree, &$lexemes, $index, $countLexemes ) { |
120 | 121 | wfProfileIn( __METHOD__ ); |
121 | 122 | |
122 | | - if ( $index + 1 < $countLexemes && array_key_exists( $currLex = $lexemes[$index + 1][0], $this->mTree ) ) { |
123 | | - $ret = $this->mTree[$currLex]->findNextTermNoSkip( $lexemes, $index + 1, $countLexemes ); |
| 123 | + if ( $index + 1 < $countLexemes && array_key_exists( $currLex = $lexemes[$index + 1][0], $tree ) ) { |
| 124 | + $ret = $this->findNextTermNoSkip( $tree[$currLex], $lexemes, $index + 1, $countLexemes ); |
124 | 125 | } else { |
125 | | - $ret = array( $index, &$this->mDefinition ); |
| 126 | + $ret = array($index, &$tree[-1]); |
126 | 127 | } |
127 | 128 | wfProfileOut( __METHOD__ ); |
128 | 129 | return $ret; |