Last active
December 15, 2015 07:19
-
-
Save mugifly/5222608 to your computer and use it in GitHub Desktop.
Bayon Simple WebAPI (with Mojolicious::Lite)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Bayon Simple WebAPI (with Mojolicious::Lite) | |
# https://gist.github.com/mugifly/5222608 | |
use Mojolicious::Lite; | |
use utf8; | |
use Mojo::JSON; | |
use Mojo::ByteStream; | |
use Mojo::Asset::File; | |
use Text::Bayon; | |
use Encode; | |
app->config(hypnotoad => {listen => ['http://*:8080'], workers => 1, pid_file => 'bayon_simple_webapi.pid' }); | |
# Requires | |
# * Bayon - http://code.google.com/p/bayon/ | |
# * "Text::Bayon" module - http://search.cpan.org/~miki/Text-Bayon/ | |
# * "Mojolicious" module | |
# About the methods and options | |
# http://search.cpan.org/~miki/Text-Bayon/ | |
# thankyou. | |
# Clustering | |
post '/clustering' => sub { | |
my $self = shift; | |
if(defined $self->param('input')){ | |
my $js = Mojo::JSON->new(); | |
my $input = $js->decode($self->param('input')); | |
my $results = {}; | |
my $clvector = {}; | |
my $options = {}; | |
$options->{idf} = $self->param('idf') || undef; | |
$options->{number} = $self->param('number') || undef; | |
$options->{limit} = $self->param('limit') || undef; | |
$options->{point} = $self->param('point') || undef; | |
$options->{clvector} = $self->param('clvector') || undef; | |
$options->{clvector_size} = $self->param('clvector_size') || undef; | |
$options->{method} = $self->param('method') || undef; | |
$options->{seed} = $self->param('seed') || undef; | |
# Clustering with Bayon | |
eval{ | |
my $bayon; | |
if(-f 'bayon'){ | |
$bayon = Text::Bayon->new(); | |
}else{ | |
$bayon = Text::Bayon->new(bayon_path => '/usr/local/bin/bayon'); | |
} | |
if($options->{clvector} == 1){ | |
($results, $clvector) = $bayon->clustering($input, $options); | |
}else{ | |
$results = $bayon->clustering($input, $options); | |
} | |
}; | |
if($@){ # Error throw | |
$self->render_json({error => $@}, status => 400); | |
return; | |
} | |
# Output results | |
$self->res->headers->content_type("application/json; charset=UTF-8"); | |
if($options->{clvector} == 1){ | |
# Results and clvector | |
$self->render_json({ | |
results => $results, | |
clvector => $clvector, | |
}, status => 200); | |
} else { | |
# Results | |
$self->render_json({ | |
results => $results, | |
}, status => 200); | |
} | |
} else { | |
# Null sentence | |
$self->render_json({}, status => 400); | |
} | |
}; | |
# Classify | |
post '/classify' => sub { | |
my $self = shift; | |
if(defined $self->param('input') && defined $self->param('classify')){ | |
my $js = Mojo::JSON->new(); | |
my $input = $js->decode($self->param('input')); | |
my $results = {}; | |
my $options = {}; | |
$options->{inv_keys} = $self->param('inv_keys') || undef; | |
$options->{inv_size} = $self->param('inv_size') || undef; | |
$options->{classify_size} = $self->param('classify_size') || undef; | |
# Save classify-input to temporary file | |
my $classify = $js->decode($self->param('classify')); | |
my $classify_file_asset = Mojo::Asset::File->new; | |
my $classify_file_name = time().'_'. Mojo::ByteStream->new($classify)->sha1_sum(); | |
foreach my $key(keys %{$classify}){ | |
my $line = $key; | |
foreach my $keyword_hash(@{$classify->{$key}}){ | |
foreach my $keyword(keys %{$keyword_hash}){ | |
$line .= "\t". $keyword ."\t". $keyword_hash->{$keyword}; # keyword score | |
} | |
} | |
$classify_file_asset->add_chunk($line . "\n"); | |
} | |
$classify_file_asset->move_to('bayon_tmp_'.$classify_file_name.'.txt'); | |
$options->{classify} = $classify_file_asset->path(); | |
# Classify with Bayon | |
eval{ | |
my $bayon; | |
if(-f 'bayon'){ | |
$bayon = Text::Bayon->new(); | |
}else{ | |
$bayon = Text::Bayon->new(bayon_path => '/usr/local/bin/bayon'); | |
} | |
$results = $bayon->classify($input, $options); | |
}; | |
# Cleanup the temporary file | |
$classify_file_asset->cleanup(1); | |
if($@){ # Error throw | |
$self->render_json({error => $@}, status => 400); | |
return; | |
} | |
# Output results | |
$self->res->headers->content_type("application/json; charset=UTF-8"); | |
$self->render_json({ | |
results => $results, | |
}, status => 200); | |
} else { | |
# Null sentence | |
$self->render_json({}, status => 400); | |
} | |
}; | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment