http://www.mediawiki.org/wiki/Special:Code/MediaWiki/89536
Revision: 89536
Author: foxtrott
Date: 2011-06-05 21:33:52 +0000 (Sun, 05 Jun 2011)
Log Message:
-----------
try to optimize glossary building
Modified Paths:
--------------
trunk/extensions/Lingo/LingoBackend.php
trunk/extensions/Lingo/LingoParser.php
trunk/extensions/Lingo/LingoTree.php
Modified: trunk/extensions/Lingo/LingoBackend.php
===================================================================
--- trunk/extensions/Lingo/LingoBackend.php 2011-06-05 21:09:50 UTC (rev
89535)
+++ trunk/extensions/Lingo/LingoBackend.php 2011-06-05 21:33:52 UTC (rev
89536)
@@ -28,7 +28,7 @@
/**
*
- * @return Boolean true, if a next element is available
+ * @return the next element or null
*/
abstract public function next();
Modified: trunk/extensions/Lingo/LingoParser.php
===================================================================
--- trunk/extensions/Lingo/LingoParser.php 2011-06-05 21:09:50 UTC (rev
89535)
+++ trunk/extensions/Lingo/LingoParser.php 2011-06-05 21:33:52 UTC (rev
89536)
@@ -95,16 +95,8 @@
$backend = &$this->mLingoBackend;
// assemble the result array
- $this->mLingoArray = array();
while ( $elementData = $backend->next() ) {
-
- if ( array_key_exists( ( $term =
$elementData[LingoElement::ELEMENT_TERM] ), $this->mLingoArray ) ) {
- $this->mLingoArray[$term]->addDefinition(
$elementData );
- } else {
- $this->mLingoArray[$term] = new LingoElement(
$term, $elementData );
- }
-
- $this->mLingoTree->addTerm( $term, $elementData );
+ $this->mLingoTree->addTerm(
$elementData[LingoElement::ELEMENT_TERM], $elementData );
}
wfProfileOut( __METHOD__ );
Modified: trunk/extensions/Lingo/LingoTree.php
===================================================================
--- trunk/extensions/Lingo/LingoTree.php 2011-06-05 21:09:50 UTC (rev
89535)
+++ trunk/extensions/Lingo/LingoTree.php 2011-06-05 21:33:52 UTC (rev
89536)
@@ -26,8 +26,8 @@
class LingoTree {
private $mTree = array();
- private $mDefinition = null;
- private $mMinLength = -1;
+ private $mList = array();
+ private $mMinLength = 1000;
/**
* Adds a string to the Lingo Tree
@@ -38,49 +38,50 @@
return;
}
- $matches;
- preg_match_all( '/[[:alpha:]]+|[^[:alpha:]]/u', $term, $matches
);
+ if ( isset( $this->mList[$term] ) ) { // term exists, store 2nd
definition
- $this->addElement( $matches[0], $term, $definition );
+ $this->mList[$term][-1]->addDefinition( $definition );
- if ( $this->mMinLength > -1 ) {
- $this->mMinLength = min( array( $this->mMinLength,
strlen( $term ) ) );
} else {
- $this->mMinLength = strlen( $term );
+
+ $matches;
+ preg_match_all( '/[[:alpha:]]+|[^[:alpha:]]/u', $term,
$matches );
+
+ $this->mList[$term] = $this->addElement( $matches[0],
$term, $definition );
+
+ $this->mMinLength = min( array($this->mMinLength,
strlen( $term )) );
}
}
/**
- * Recursively adds an element to the Lingo Tree
+ * Adds an element to the Lingo Tree
*
* @param array $path
* @param <type> $index
+ * @return Array the tree node the element was stored in
*/
- protected function addElement( Array &$path, &$term, &$definition ) {
+ protected function &addElement( Array &$path, &$term, &$definition ) {
+
+ $tree = &$this->mTree;
+
// end of path, store description; end of recursion
- if ( $path == null ) {
- $this -> addDefinition( $term, $definition );
- } else {
- $step = array_shift( $path );
+ while ( $step = array_shift( $path ) ) {
- if ( !array_key_exists( $step, $this->mTree ) ) {
- $this->mTree[$step] = new LingoTree();
+ if ( !isset( $tree[$step] ) ) {
+ $tree[$step] = array();
}
- $this->mTree[$step]->addElement( $path, $term,
$definition );
+ $tree = &$tree[$step];
+
}
- }
- /**
- * Adds a defintion to the treenodes list of definitions
- * @param <type> $definition
- */
- protected function addDefinition( &$term, &$definition ) {
- if ( $this->mDefinition ) {
- $this->mDefinition->addDefinition( $definition );
+ if ( isset( $tree[-1] ) ) {
+ $tree[-1]->addDefinition( $definition );
} else {
- $this->mDefinition = new LingoElement( $term,
$definition );
+ $tree[-1] = new LingoElement( $term, $definition );
}
+
+ return $tree;
}
function getMinTermLength() {
@@ -99,7 +100,7 @@
// Did we find the start of a term?
if ( array_key_exists( $currLex, $this->mTree ) ) {
- list( $lastindex, $definition ) =
$this->mTree[$currLex]->findNextTermNoSkip( $lexemes, $index, $countLexemes );
+ list( $lastindex, $definition ) =
$this->findNextTermNoSkip( $this->mTree[$currLex], $lexemes, $index,
$countLexemes );
}
// this will increase the index even if we found
something;
@@ -109,19 +110,19 @@
wfProfileOut( __METHOD__ );
if ( $definition ) {
- return array( $index - $start - 1, $lastindex - $index
+ 2, $definition );
+ return array($index - $start - 1, $lastindex - $index +
2, $definition);
} else {
- return array( $index - $start, 0, null );
+ return array($index - $start, 0, null);
}
}
- function findNextTermNoSkip( &$lexemes, $index, $countLexemes ) {
+ function findNextTermNoSkip( Array &$tree, &$lexemes, $index,
$countLexemes ) {
wfProfileIn( __METHOD__ );
- if ( $index + 1 < $countLexemes && array_key_exists( $currLex =
$lexemes[$index + 1][0], $this->mTree ) ) {
- $ret = $this->mTree[$currLex]->findNextTermNoSkip(
$lexemes, $index + 1, $countLexemes );
+ if ( $index + 1 < $countLexemes && array_key_exists( $currLex =
$lexemes[$index + 1][0], $tree ) ) {
+ $ret = $this->findNextTermNoSkip( $tree[$currLex],
$lexemes, $index + 1, $countLexemes );
} else {
- $ret = array( $index, &$this->mDefinition );
+ $ret = array($index, &$tree[-1]);
}
wfProfileOut( __METHOD__ );
return $ret;
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs