Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Combine two globals?

(Page 1 of 2)
> >
Quote Reply
Combine two globals?
I'm using these two globals to show search words from the title and from description:
Code:
sub {
my $tags = GT::Template->tags;
my @words = split(/\s+/,$tags->{Title});
my @links;

foreach (@words){
s/^\s*//sg;
s/[\.,:\?!\(\)"]+//sg;
next if length($_)<6;
push(@links,qq|<a href="$CFG->{db_cgi_url}/search.cgi?query=$_">$_</a>|);
}

return join(" - ",@links);

}

and

Code:
sub {
my $tags = GT::Template->tags;
my @words = split(/\s+/,$tags->{Description});
my @links;

foreach (@words){
s/^\s*//sg;
s/[\.,:\?!\(\)"]+//sg;
next if length($_)<4;
push(@links,qq|<a href="$CFG->{db_cgi_url}/search.cgi?query=$_">$_</a>|);
}

return join(" - ",@links);

}


When description and title include the same words, I get double search words?
Is there a way to combine these two globals and print the results only one time...?

Thanks
Matthias

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
Hi,

This should do it:

Code:
sub {
my $tags = GT::Template->tags;
my @words = split(/\s+/,"$tags->{Description} $tags->{Title}");
my @links;
my %seen;
foreach (@words){
s/^\s*//sg;
s/[\.,:\?!\(\)"]+//sg;
next if length($_)<4;
next if $seen{$_};
push @links, qq|<a href="$CFG->{db_cgi_url}/search.cgi?query=$_">$_</a>|;
$seen{$_} = 1;
}

return join(" - ",@links);

}

Untested, but should do the trick Angelic

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Combine two globals? In reply to
Yeah, that's it.
Now I have a lot of words in the output.
In Germany we write substantives with capital letters.
Do you see a way to print out only substantives (Words with capital letters)???

Thanks
Matthias

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
So you just wanna do it based on if the word starts with a capital? If so, after this line:

Code:
next if length($_)<4;

Try adding:

Code:
next if $_ !~ /^[A-Z]/;

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Combine two globals? In reply to
Andy wrote:
So you just wanna do it based on if the word starts with a capital?


The code for capital letters is working fine.
Do you have more ways do reduce the results of the output???

Matthias

Matthias
gpaed.de

Last edited by:

Matthias70: Jan 5, 2013, 10:18 AM
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
Maybe limit it based on the most popular keywords? Say 10 words, that are seen the most in the title+description? That shouldn't be too hard to do (but I won't waste time doing it unless thats what your after Wink)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Combine two globals? In reply to
Andy wrote:
Maybe limit it based on the most popular keywords? Say 10 words, that are seen the most in the title+description? That shouldn't be too hard to do (but I won't waste time doing it unless thats what your after Wink)

Cheers

That's a good idea, but a the moment I'm fighting with singular an plural.
When the title includes "auto" and the description includes "autos". I have "auto" and "autos" in my output.
"auto" would be enough!

Thanks

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
You could try removing the "s" from the end of words. Try changing:

Code:
s/[\.,:\?!\(\)"]+//sg;

To:

Code:
s/[\.,:\?!\(\)"]+|s$//sg;

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Combine two globals? In reply to
Andy wrote:
You could try removing the "s" from the end of words. Try changing:

Code:
s/[\.,:\?!\(\)"]+//sg;


To:

Code:
s/[\.,:\?!\(\)"]+|s$//sg;


Cheers

I think this won't work in german. Cause there are different forms of plurals.

How will your idea of the 10 most used words work?
Perhaps it will work with an combination with a small list of words that should excluded...

Thanks

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
Ok, totally untested - but give this a go:

Code:
sub {
my $tags = GT::Template->tags;
my @words = split(/\s+/,"$tags->{Description} $tags->{Title}");
my @links;
my %seen;
foreach (@words){
s/^\s*//sg;
s/[\.,:\?!\(\)"]+//sg;
next if length($_)<4;
next if $seen{$_};
$seen{$_}++;
}
my $i = 0;
for my $k (sort {$seen{$b} cmp $seen{$a}} keys %seen) {
last if $i > 10;
push @links, qq|<a href="$CFG->{db_cgi_url}/search.cgi?query=$k">$k</a>|;
$i++;
}

return join(" - ",@links);

}

This should work out which words are used the most (between the title + desc), and then show the 10 most used (you can change that number if you want... its the last if $i > 10; bit

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Combine two globals? In reply to
Hi Andy,
this global just works fine.
I just added the code to sort out the german verbs
Code:
next if $_ !~ /^[A-Z]/;


What do you think. When there are 15 words in title and description.
All words used only on time.
What are the 10 words in the output?

Thanks
Matthias

Matthias
gpaed.de

Last edited by:

Matthias70: Jan 7, 2013, 4:34 AM
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
Hi,

Glad it works =)

Quote:
What do you think. When there are 15 words in title and description.
All words used only on time.
What are the 10 words in the output?

I'm not sure what you're asking? Are you asking which words are showing? It will grab those that have the most occurrences (sorted by the number of occurrences, desc) .

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Combine two globals? In reply to
Andy wrote:

I'm not sure what you're asking? Are you asking which words are showing? It will grab those that have the most occurrences (sorted by the number of occurrences, desc) .

Yes, but what happens, when every word occures only one time?

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
It will just show the words, in normal order (as all the "counts" will be the same)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Combine two globals? In reply to
Hi Andy,
there is something wrong with the global.
I did a test entry
http://www.gpaed.de/...t3_Test4_D17451.html
I have these settings
Code:
last if $i > 3;
This code is deleting small letter words (works fine with test8 and test9)
Code:
next if $_ !~ /^[A-Z]/;

When you have a look at the example (link above) you will see, that Test6 is shown in the Tags but Test4 occures 2 times and is not shown in the Tags...
And it would be great if Test1 (occures three times) would be shown as the first tag...

This is the whole global I'm using
Code:
sub {
my $tags = GT::Template->tags;
my @words = split(/\s+/,"$tags->{Description} $tags->{Title}");
my @links;
my %seen;
foreach (@words){
s/^\s*//sg;
s/[\.,;:\?!\(\)"]+//sg;
next if length($_)<4;
next if $_ !~ /^[A-Z]/;
next if $seen{$_};
$seen{$_}++;
}
my $i = 0;
for my $k (sort {$seen{$b} cmp $seen{$a}} keys %seen) {
last if $i > 3;
push @links, qq|<a href="$CFG->{db_cgi_url}/search.cgi?query=$k">$k</a>|;
$i++;
}

return join(" - ",@links);

}

Thanks

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
You have:

Code:
next if $_ !~ /^[A-Z]/;

Should be insensative:

Code:
next if $_ !~ /^[A-Z]/i;

Also, try changing this so it lowercases (in which case you don't need to worry about the above);

Code:
my @words = split(/\s+/,lc("$tags->{Description} $tags->{Title}"));

That should lowercase the whole string (otherwise "Test" and "test" will be seen as 2 different words)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Combine two globals? In reply to
Hi Andy,
now it shows Test1 (occures three times in title and description) and Test2 (occures two times).
But it also shows Test6 and Test7 (occures only one time)
The weird thing is, that Test3 is not shown, but it occures two times in title and description...???

http://www.gpaed.de/...t3_Test4_D17451.html

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
Hi Andy,
now it shows Test1 (occures three times in title and description) and Test2 (occures two times).
But it also shows Test6 and Test7 (occures only one time)
The weird thing is, that Test3 is not shown, but it occures two times in title and description...???

I hope you understand what I mean.
In other words where is Test3 in the expample? (see link below)

http://www.gpaed.de/...t3_Test4_D17451.html

BTW:
These two lines do not work. They just print out german substantives with lower letters. But I need capital letter...
Code:
next if $_ !~ /^[A-Z]/i;

Code:
my @words = split(/\s+/,lc("$tags->{Description} $tags->{Title}"));

Matthias
gpaed.de

Last edited by:

Matthias70: Jan 7, 2013, 12:08 PM
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
Ok, think I see it. We have:

Code:
next if $seen{$_};

Remove that line. That was from before (where we only wanted to process each word 1 time). Not sure/.how where that line came back in - but I expect its the cause of the problem =) I just did a test of the function on my PC and it looks ok now (I removed the limit, which is why you can also see more than 3 words);

Code:
C:\Users\Andy>perl test.pl
Test1 - 3
- Test2 - 2
- Test3 - 2
- Test4 - 2
- Test6 - 1
- Test5 - 1
- Test7 - 1

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Combine two globals? In reply to
Seems to work fine. I will test it the next days and give you a feedback.
Thanks

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
Hi Andy,
I tested the global on my page and sorting by occurance works fine.
But when all words occure only one time (or in my example two times) the sorting and selection is a bit weird...
Here is an example: ABCc is not in the taglist but it is in the mid of title and description???
http://www.gpaed.de/...BCe_ABCf_D17451.html

Thanks

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
Try changing the "sort" line from:

Code:
for my $k (sort {$seen{$b} cmp $seen{$a} } keys %seen) {

to:

Code:
for my $k (sort {$seen{$b} cmp $seen{$a} || lc($a) cmp lc($b) } keys %seen) {

This should sort by the most used words first and THEN also alphabetically.

Before the change:

Code:
C:\Users\Andy>perl test.pl
ABCb - 3
- ABCe - 2
- ABCa - 2
- ABCd - 2
- ABCc - 2
- ABCf - 1


..and after:

Code:
C:\Users\Andy>perl test.pl
ABCb - 3
- ABCa - 2
- ABCc - 2
- ABCd - 2
- ABCe - 2
- ABCf - 1

Hopefully thats the last I'll hear about this global hehe

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Combine two globals? In reply to
Perfect!
May I ask for a line where I can exclude some words from the output BlushBlushBlush

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combine two globals? In reply to
LOL. Ok, after:

Code:
next if $_ !~ /^[A-Z]/;

Add something like:

Code:
next if $_ !~ /^(word1|word2|word3)$/;

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Combine two globals? In reply to
Andy wrote:

Code:
next if $_ !~ /^(word1|word2|word3)$/;

Hi Andy,
this code shows only word1, word2 and word3 and deletes all other words. Should be the the other way...
Thanks

Matthias
gpaed.de
> >