Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Help on a keyword topic index

by neilwatson (Priest)
on Sep 17, 2015 at 16:18 UTC ( #1142333=perlquestion: print w/replies, xml ) Need Help??

neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Greetings. I came up with this today to be able to retrieve a topic based on keyword. There will not be more more than a few hundred topics, each with a keyword or two. Can anyone suggest a better way?

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Data::Dumper; # TODO turn into yml as an external file. my @index = ( { keywords => [ qw/ emacs cfengine_el / ], topic => "Cfengine_el is an EMACS plugin to edit your CFEngin +e ". "code: ". " +e.el", }, { keywords => [ 'best practices' ], topic => "CFEngine best practices: ". "" +. "and", }, ); my %topic; my %keyword; my $i = 0; # Build a fast index for keyword searches for my $next_topic ( @index ) { # Store topic in index. $topic{$i} = $next_topic->{topic}; for my $next_keyword ( @{ $next_topic->{keywords} } ) { # Store keyworkd in index. $keyword{$next_keyword} = $i; } $i++; } say Dumper( \%topic ); say Dumper( \%keyword ); say 'topic for keyword "best practices" is ' . $topic{ $keyword{ 'best practices' } };

Running it:

$VAR1 = { '0' => 'Cfengine_el is an EMACS plugin to edit your CFEngine + code: +el', '1' => 'CFEngine best practices: +ategory/cfengine/best-practices/and +alth-check' }; $VAR1 = { 'emacs' => 0, 'cfengine_el' => 0, 'best practices' => 1 }; topic for keyword "best practices" is CFEngine best practices: http:// https://githu

Neil Watson

Replies are listed 'Best First'.
Re: Help on a keyword topic index
by RichardK (Parson) on Sep 17, 2015 at 17:22 UTC

    In your code, each keyword can only point to one topic, which isn't very helpful.

    if you stored a list of topics per keyword it would be much more flexible. As emacs can do everything there are going to be a lot of topics with the emacs keyword ;)

    for my $next_keyword ( @{ $next_topic->{keywords} } ) { # Store keyword in index. push @{$keyword{$next_keyword}} , $i; } ...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1142333]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2019-08-17 14:35 GMT
Find Nodes?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?

    Results (134 votes). Check out past polls.