Author: pmacadden
Date: 2010-03-16 13:09:34 +0100 (Tue, 16 Mar 2010)
New Revision: 28549
Added:
plugins/dcSwishPlugin/trunk/lib/dcSwishHighlight.class.php
plugins/dcSwishPlugin/trunk/web/
plugins/dcSwishPlugin/trunk/web/css/
plugins/dcSwishPlugin/trunk/web/css/main.css
Modified:
plugins/dcSwishPlugin/trunk/lib/SwishManager.class.php
plugins/dcSwishPlugin/trunk/lib/SwishResultWrapper.class.php
plugins/dcSwishPlugin/trunk/modules/dc_swish/templates/indexSuccess.php
Log:
added highlighting capabilities
Modified: plugins/dcSwishPlugin/trunk/lib/SwishManager.class.php
===================================================================
--- plugins/dcSwishPlugin/trunk/lib/SwishManager.class.php 2010-03-16
11:23:02 UTC (rev 28548)
+++ plugins/dcSwishPlugin/trunk/lib/SwishManager.class.php 2010-03-16
12:09:34 UTC (rev 28549)
@@ -50,14 +50,19 @@
public static function doSelect($query, $index, $offset, $limit, $sort=null)
{
+ if (empty($index) )
+ {
+ $index = sfConfig::get('app_dc_swish_index', "swish_index_default");
+ }
$sm=SwishManager::getInstance($index);
$result=$sm->search($query,$sort);
$result->seekResult($offset);
+ $removed_stopwords = $result->getRemovedStopwords($index);
$i=0;
$ret=array();
while( ( $current=$result->nextResult() ) && ($i++ < $limit) )
{
- $ret[]=new SwishResultWrapper($current);
+ $ret[]=new SwishResultWrapper($current, $removed_stopwords);
}
return $ret;
}
@@ -73,7 +78,9 @@
protected function getQueryString($query)
{
- return
(is_array($query)&&array_key_exists('query',$query))?$query['query']:(!is_array($query)?$query:null);
+ $str =
(is_array($query)&&array_key_exists('query',$query))?$query['query']:(!is_array($query)?$query:null);
+
+ return $str;
}
protected function getSortString($sort)
Modified: plugins/dcSwishPlugin/trunk/lib/SwishResultWrapper.class.php
===================================================================
--- plugins/dcSwishPlugin/trunk/lib/SwishResultWrapper.class.php
2010-03-16 11:23:02 UTC (rev 28548)
+++ plugins/dcSwishPlugin/trunk/lib/SwishResultWrapper.class.php
2010-03-16 12:09:34 UTC (rev 28549)
@@ -7,9 +7,10 @@
$size,
$lastmodified,
$title,
- $description;
+ $description,
+ $removed_stopwords;
- public function __construct(SwishResult $s)
+ public function __construct(SwishResult $s, $removed_stopwords)
{
$this->rank= @$s->swishrank;
$this->path= @$s->swishdocpath;
@@ -17,6 +18,7 @@
$this->lastmodified = @$s->swishlastmodified;
$this->title = @$s->swishtitle;
$this->description = @$s->swishdescription;
+ $this->removed_stopwords = $removed_stopwords;
}
public function getRank()
@@ -59,4 +61,8 @@
return base64_decode($p);
}
+ public function getRemovedStopwords()
+ {
+ return $this->removed_stopwords;
+ }
}
Added: plugins/dcSwishPlugin/trunk/lib/dcSwishHighlight.class.php
===================================================================
--- plugins/dcSwishPlugin/trunk/lib/dcSwishHighlight.class.php
(rev 0)
+++ plugins/dcSwishPlugin/trunk/lib/dcSwishHighlight.class.php 2010-03-16
12:09:34 UTC (rev 28549)
@@ -0,0 +1,113 @@
+<?php
+
+class dcSwishHighlight
+{
+ public static function highlight(SwishResultWrapper $srw, $query)
+ {
+ $highlighted_text = "";
+ $text = $srw->getDescription();
+
+ // save the positions of the keywords
+ $positions = array();
+
+ $words = self::explode($query, $srw->getRemovedStopwords());
+
+ // word by word...
+ foreach ($words as $word)
+ {
+ $offset = 0;
+ // ...match all occurrences
+ while ($pos = stripos($text, $word, $offset))
+ {
+ // if the position is not already seen
+ if (!self::near_position($pos, $positions))
+ {
+ // save a chunk of the text
+ $positions[] = $pos;
+ $highlighted_text .= "...".substr($text, $pos - 20, 100)."... ";
+ break;
+ }
+
+ $offset = $pos + strlen($word);
+ }
+ }
+
+ // highlight word by word
+ foreach ($words as $word)
+ {
+ $word = preg_quote($word);
+ $highlighted_text = preg_replace("/($word)/i", '<span
class="highlight_word">\1</span>', $highlighted_text);
+ }
+
+ return $highlighted_text;
+ }
+
+ private static function near_position($position, $positions)
+ {
+ $near = false;
+
+ for ($i = $position - 20; $i <= $position + 100; $i ++)
+ {
+ if (in_array($i, $positions))
+ {
+ $near = true;
+ break;
+ }
+ }
+
+ return $near;
+ }
+
+ /**
+ * explode the query string:
+ * * words within "" remains together
+ */
+ private static function explode($query, $stopwords)
+ {
+ $words = array();
+ $non_empty_words = array();
+
+ foreach (explode("\"", $query) as $word)
+ {
+ $words[] = $word;
+ }
+
+ foreach ($words as $word)
+ {
+ if (!preg_match("/\s\w\s/", $word) && !empty($word))
+ {
+ $non_empty_words[] = $word;
+ }
+ }
+
+ $words = array();
+
+ foreach (explode(" ", $query) as $word)
+ {
+ if (!preg_match("/\"/", $word))
+ {
+ $words[] = $word;
+ }
+ }
+
+ foreach ($words as $word)
+ {
+ if (!empty($word))
+ {
+ $non_empty_words[] = $word;
+ }
+ }
+
+ // remove all stopwords
+ $new_non_empty_words = array();
+ for ($i = 0; $i < count($non_empty_words); $i++)
+ {
+ if (!in_array($non_empty_words[$i], $stopwords))
+ {
+ $new_non_empty_words[] = $non_empty_words[$i];
+ }
+ }
+
+ return $new_non_empty_words;
+ }
+}
Modified:
plugins/dcSwishPlugin/trunk/modules/dc_swish/templates/indexSuccess.php
===================================================================
--- plugins/dcSwishPlugin/trunk/modules/dc_swish/templates/indexSuccess.php
2010-03-16 11:23:02 UTC (rev 28548)
+++ plugins/dcSwishPlugin/trunk/modules/dc_swish/templates/indexSuccess.php
2010-03-16 12:09:34 UTC (rev 28549)
@@ -1,5 +1,6 @@
<?php use_helper('I18N', 'Date') ?>
+<?php use_stylesheet("/dcSwishPlugin/css/main.css") ?>
<?php include_partial("dc_swish/flashes") ?>
<form action="<?php echo url_for('@dc_swish_search') ?>" method="POST">
Added: plugins/dcSwishPlugin/trunk/web/css/main.css
===================================================================
--- plugins/dcSwishPlugin/trunk/web/css/main.css
(rev 0)
+++ plugins/dcSwishPlugin/trunk/web/css/main.css 2010-03-16 12:09:34 UTC
(rev 28549)
@@ -0,0 +1,4 @@
+.highlight_word
+{
+ font-weight: bold;
+}
--
You received this message because you are subscribed to the Google Groups
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/symfony-svn?hl=en.