Skip to content

Instantly share code, notes, and snippets.

@swuecho
Created February 21, 2017 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swuecho/1b960ae17a1f47466be006fd14e3b7ff to your computer and use it in GitHub Desktop.
Save swuecho/1b960ae17a1f47466be006fd14e3b7ff to your computer and use it in GitHub Desktop.
#!/usr/local/bin/perl
package MyAnalyzer {
use v5.10;
use base qw( Lucy::Analysis::Analyzer );
sub new {
my $self = shift->SUPER::new;
return $self;
}
sub transform {
my ($self, $inversion)= @_;
return $inversion;
}
sub transform_text {
my ($self, $text) = @_;
my $inversion = Lucy::Analysis::Inversion->new;
my @tokens = (['a', 0,1], ['b', 1,2] );
$inversion->append(
Lucy::Analysis::Token->new(text =>$_->[0],
start_offset=> $_->[1] ,
end_offset=>$_->[2]
)
) for @tokens;
return $inversion;
}
1;
}
package main;
use DBI;
use File::Spec::Functions qw( catfile );
use Lucy::Plan::Schema;
use Lucy::Plan::FullTextType;
use Lucy::Index::Indexer;
my $path_to_index = '/tmp/test.index';
# Create Schema.
my $schema = Lucy::Plan::Schema->new;
my $my_analyzer= MyAnalyzer->new();
my $raw_type = Lucy::Plan::FullTextType->new(
analyzer => $my_analyzer,
);
$schema->spec_field( name => 'body', type => $raw_type);
use DDP; p $schema->dump;
# Create an Indexer object.
my $indexer = Lucy::Index::Indexer->new(
index => $path_to_index,
schema => $schema,
create => 1,
# truncate => 1,
);
my $doc = { body => 'test' };
$indexer->add_doc($doc);
$indexer->commit;
print "Finished.\n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment