Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2006-03-24 04:46:45
- Champak
- Member
- Registered: 2006-01-31
- Posts: 56
A way to limit the amount of hits within one article
A way to limit the amount of hits within one article? Say I search for the word “dog”, each article returns and shows all 100 times the word dog was in each article. I want the return to show a set amount of times the word “dog” was in the article…say three sentances, instead of all 100. (very annoying the way it is)
Last edited by Jeremie (2006-03-24 15:02:06)
Offline
Re: A way to limit the amount of hits within one article
Thread’s subject edited for more clarity. I8 least I hope, I’m not sure I quite understand exactly what you need, and what is your annoyance.
Offline
Re: A way to limit the amount of hits within one article
I think he talks about search. And wants to be able to specify how many times the search excerpt shows that search term.
However, what kind of articles have you got or what terms do you search for that occur 100 times in one article?
Offline
#4 2006-03-24 20:01:37
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: A way to limit the amount of hits within one article
Couldn’t you use the excerpt field for that, and replace <txp:search_result_excerpt />
in the search_results form with <txp:excerpt />
? Or a custom field? (The ‘dog’ example makes sense to me if no one else ;) )
Offline
Re: A way to limit the amount of hits within one article
I think that the current way is very good (although advanced search features similar to those offered by plugins would be very welcome). Face it. An article which uses the same word many times would not make a very good read.
An article about my “dog” would not have the word 100 times especially if the text establishes from the beginning that it would talk about a k9.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
#6 2006-03-25 09:53:03
- marios
- Archived Plugin Author
- Registered: 2005-03-12
- Posts: 1,253
Re: A way to limit the amount of hits within one article
I have addressed this as well, I allso tried to dissect the code, ( Whith no results yet unfortunatelly)
Textpattern will wrap the search term within the returned search result excerpts with <code><strong></code> tags by default.
<em style=“color:green”>It is not the number of hits</em>, that get returned, but the way the regular expression flags of preg_match_all and preg_replace
truncate and reorder the article body, before it get’s rendered.
It is possible to get away with this in some degree by hacking the source a little.
But altering the code to return query terms only on real word boudaries, instead of any combinations of letters is much more difficult and will require a good deal of knolledge about regular expressions,regular expressions used in php, and how it’s done in textpattern actually (and may be some advanced database queries).
In fact some one has done a plugin for wordpress that does the real word bouderies search, plus some other features, I’ll post hte link here as soon as I can find it again.
(I wished that ob1 could head off once more to save the princess, I even would fill up the bucket, if needed.)
<h2>If you want to reduce the number of strings that contain the highlighted search term</h2>
go to line nr 1254 in publish/taghandlers (search_result_excerpt function)
and replace the line
<code>preg_match_all(“/\s.{1,50}”.preg_quote($q).”.{1,50}\s/iu”,$result,$concat);</code>
<br />
with this:
<br />
<code> preg_match_all(“/\s.{1,10}”.preg_quote($q).”.{1,10}\s/iu”,$result,$concat);</code>
(This will decrease the number of strings that contain the query term that get’s outputted)
<h2>If you want to get rid of the highlighting alltogether</h2>
then allso replace
(line 1260)
<code> $concat = preg_replace(“/(“.preg_quote($q).”)/i”,”<$hilight>$1”</$hilight>,$concat);</code>
<br />
(The reason why I don’t wan’t the hilighting, is that this would be very annoying for poeple with screen readers)
regards, marios
Last edited by marios (2006-03-25 10:11:44)
⌃ ⇧ < ⎋ ⌃ ⇧ >
Offline
#7 2006-03-25 18:27:14
- Champak
- Member
- Registered: 2006-01-31
- Posts: 56
Re: A way to limit the amount of hits within one article
marios, thank you. Although nothing to do with what I was inquiring about, that does help on reducing the “clutter” on search results. I don’t understand, things like that should be setable if results are going to throw in everything and the kitchen sink as default…maybe next version. Also, until I was testing out the hack you provided, I didn’t realize that the results were also throwing in the word when it was part of another word..ex. “MAN” and “woMAN”. That definately needs to be fixed. Is there a way to break the line after each search hit to make it even cleaner instead of only the “…”? Thanks.
colak, dog hitting 100 times was just to exemplify what I was talking about….come on. As far as I am personally concerned, two times is one time too many. That’s why I believe it should be settable to the admins preference.
Offline
#8 2006-03-25 18:46:16
- marios
- Archived Plugin Author
- Registered: 2005-03-12
- Posts: 1,253
Re: A way to limit the amount of hits within one article
You can try it out by yourself, enter single letters lide I , c , d , Textpattern will allways return non word boundaries matches and highlight them regardsless if that would make sense or not, it will match full word boundaries as well, but it doesn’t have a way to distinguish between them as is. (4.03)
regards, marios
⌃ ⇧ < ⎋ ⌃ ⇧ >
Offline
#9 2006-06-27 16:41:45
- ice
- New Member
- Registered: 2006-06-26
- Posts: 8
Re: A way to limit the amount of hits within one article
I noticed that the documentation referers to a limit parameter for search_result_excerpt, but on 4.0.3 this does not do anything. So I thought I’d have a dig and inspired by zem’s change in changeset1254 I came up with this slight modification in taghandlers.php. No idea if it is decent code or not but seems to work.
In addition the hilight parameter can now be empty or take attributes. For example the tag call
<code>
<txp:search_result_excerpt limit=“5” hilight=“span class=‘highlight’”/>
</code>
will put the matched string within a span tag with a class of highlight.
<code>
function search_result_excerpt($atts)
{
global $thisarticle, $q;
extract(lAtts(array(
‘hilight’ => ‘strong’,
‘limit’ => 5,
),$atts));
sscanf ($hilight,”%s %s”,$first, $second);
$histart = (empty($hilight) ? “” : “<$hilight>”);
$hiend = (empty($hilight) ? “” : “</$first>”);
if (empty($thisarticle)) return;
extract($thisarticle);
$result = preg_replace(“/>\s*</”,”> <”,$body);
preg_match_all(“/\s.{1,50}”.preg_quote($q).”.{1,50}\s/iu”,$result,$concat);
$r = array();
for ($i=0; $i < min($limit-1, count($concat0)); $i++)
$r[] = trim($concat0[$i]);
$concat = join(” …\n”,$r);
$concat = strip_tags($concat);
$concat = preg_replace(‘/^[^>]+>/U’,”“,$concat);
$concat = preg_replace(“/(“.preg_quote($q).”)/i”,”$histart$1$hiend”,$concat);
return ($concat) ? “… “.$concat.” …” : ‘’;
}
</code>
Offline
#10 2006-06-27 23:21:12
- net-carver
- Archived Plugin Author
- Registered: 2006-03-08
- Posts: 1,648
Re: A way to limit the amount of hits within one article
marios, ice,
you don’t need to resort to hacks/new revisions to get around the highlighting of search terms. Just use CSS to redefine what strong does in the results section.
There is an article about highlighting search results over at SonSpring but the same technique could be used to remove the emphasis too.
Of course, your other reasons for making changes are still valid :O)
— Steve
Offline
#11 2006-06-28 08:03:21
- ice
- New Member
- Registered: 2006-06-26
- Posts: 8
Re: A way to limit the amount of hits within one article
net-carver wrote:
marios, ice,
you don’t need to resort to hacks/new revisions to get around the highlighting of search terms. Just use CSS to redefine what strong does in the results section.
There is an article about highlighting search results over at SonSpring but the same technique could be used to remove the emphasis too.
Of course, your other reasons for making changes are still valid :O)
On reflection I think you are correct, I was under the impression that if the excerpt contained a strong tag then this would also get the highlight, but on further testing it appears that the excerpt is text only.
Probably change the ‘hack’ to implement only the limit functionality as this looks like it will come in the next release.
Offline
#12 2006-06-28 11:13:56
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: A way to limit the amount of hits within one article
The limit attribute is only in svn, which is why it didn’t work. You can grab a copy of the function from there.
Offline