Alt-CWB-ambs
view release on metacpan or search on metacpan
its location with the --config option:
perl Makefile.PL --config=~/cwb-3.0/bin/cwb-config
PACKAGE OVERVIEW
The CWB/Perl package contains the following Perl modules
CWB general utility functions, registry editor
CWB::CQP run CQP process in the background
CWB::Encoder stream-lined encoding and indexing of CWB corpora
CWB::CEQL an implementation of the CEQL simple query language
See the manual pages (e.g. "perldoc CWB::CQP") of these modules for further
information. The package also installs a number of command-line utility
programs:
cwb-make stream-lined indexing & compression of attributes
cwb-regedit a simple registry editor for the command line
cwb-align-import import existing sentence alignment into the CWB
data/vrt/VeryShortStories.vrt view on Meta::CPAN
; : ;
no DT no
liquid-crystal JJ liquid-crystal
display NN display
, , ,
no DT no
fancy JJ fancy
picture NN picture
in IN in
the DT the
background NN background
. SENT .
</s>
<s>
Tick VB tick
, , ,
tick VB tick
, , ,
tick VB tick
. SENT .
</s>
lib/CWB/CQP.pm view on Meta::CPAN
package CWB::CQP;
# -*-cperl-*-
=head1 NAME
CWB::CQP - Interact with a CQP process running in the background
=head1 SYNOPSIS
B<TODO: Update synopsis!>
use CWB::CQP;
# start CQP server process in the background
$cqp = new CWB::CQP;
$cqp = new CWB::CQP("-r /corpora/registry", "-I /global/init.cqp");
# check for specified or newer CQP version
$ok = $cqp->check_version($major, $minor, $beta);
# execute CQP command (blocking mode) and check for error
@lines = $cqp->exec($my_cmd);
unless ($cqp->ok) {
@cqp_error_message = $cqp->error_message;
my_error_handler();
}
# it's easier to use an automatic error handler
$cqp->set_error_handler(\&my_error_handler); # user-defined
$cqp->set_error_handler('die'); # built-in, useful for one-off scripts
# read TAB-delimited table from count, group, tabulate, ...
@table = $cqp->exec_rows($my_cmd);
# run CQP command in background (non-blocking mode)
$cqp->run($my_cmd);
if ($cqp->ready) { # specify optional timeout in seconds
my $line = $cqp->getline;
my @fields = $cqp->getrow; # TAB-delimited output
}
@lines = $cqp->getlines(10); # reads 10 lines, blocking if necessary
# execute in query lock mode (to improve security of CGI scripts)
$cqp->begin_query;
# execute untrusted CQP queries
lib/CWB/CQP.pm view on Meta::CPAN
$cqp->progress_off;
$cqp->set_progress_handler(\&my_progress_handler); # user-defined handler
# close down CQP server (exits gracefully)
undef $cqp;
=head1 DESCRIPTION
A B<CWB::CQP> object represents an instance of the corpus query processor CQP
running as a background process. By calling suitable methods on this object,
arbitrary CQP commands can be executed and their output can be captured.
The C<STDERR> stream of the CQP process is monitored for error messages,
which can automatically trigger an error handler.
Every B<CWB::CQP> object has its own CQP background process and communication is
fully asynchronous. This enables scripts to perform other actions while a long
CQP command is executing, or to run multiple CQP instances in parallel.
=cut
use warnings;
use strict;
use sigtrap qw(die PIPE); # catch write errors to background CQP process
## $SIG{'CHLD'} = 'IGNORE'; # it would be nice to reap child processes automatically, but this seems to mess up closing pipes
use CWB;
use Carp;
use FileHandle;
use IPC::Open3;
use IO::Select;
## package global variables
our @CQP_options = "-c"; # always run CQP in child mode
lib/CWB/CQP.pm view on Meta::CPAN
=head1 METHODS
The following methods are available:
=over 4
=item I<$cqp> = B<new> CWB::CQP;
=item I<$cqp> = B<new> CWB::CQP '-r /corpora/registry', '-l /data/cqpresults';
Spawn new CQP background process. The object I<$cqp> can then be used to communicate with
this CQP instance. Optional arguments of the B<new> method are passed as command-line
options to CQP. Use at your own risk.
=cut
## CWB::CQP object constructor
sub new {
my $class = shift; # class name
my $self = {}; # namespace for new CQP class object
my @options = @_; # CQP command-line options (use at your own risk)
# split options with values, e.g. "-r /my/registry" => "-r", "/my/registry" (doesn't work for multiple options in one string)
@options = map { (/^(--?[A-Za-z0-9]+)\s+(.+)$/) ? ($1, $2) : $_ } @options;
## run CQP server in the background
my $in = $self->{'in'} = new FileHandle; # stdin of CQP
my $out = $self->{'out'} = new FileHandle; # stdout of CQP
my $err = $self->{'err'} = new FileHandle; # stderr of CQP
my $pid = open3($in, $out, $err, $CWB::CQP, @CQP_options, @options);
$self->{'pid'} = $pid; # child process ID (so process can be killed if necessary)
$in->autoflush(1); # make sure that commands sent to CQP are always flushed immediately
my ($need_major, $need_minor, $need_beta) = split /\./, $CQP_version; # required CQP version
$need_beta = 0 unless $need_beta;
lib/CWB/CQP.pm view on Meta::CPAN
bless($self, $class);
## the following command will collect and ignore any output which may have been produced during startup
$self->exec("set PrettyPrint off"); # pretty-printing should be turned off for non-interactive use
return $self;
}
=item B<undef> I<$cqp>;
Exit CQP background process gracefully by issuing an C<exit;> command.
This is done automatically when the variable I<$cqp> goes out of scope.
Note that there may be a slight delay while B<CWB::CQP> waits for the CQP
process to terminate.
=cut
sub DESTROY {
my $self = shift;
if ($self->{'command'}) {
lib/CWB/CQP.pm view on Meta::CPAN
my $out = $self->{'out'};
if (defined $out) {
$out->print("exit"); # exit CQP backend
$out->close;
}
my $in = $self->{'in'};
if (defined $in) {
$in->close;
}
my $pid = $self->{'pid'};
waitpid $pid, 0; # wait for CQP to exit and reap background process
## **TODO** -- this may hang in some cases; is there a safe workaround?
}
=item I<$ok> = I<$cqp>->B<check_version>(I<$major>, I<$minor>, I<$beta>);
Check for minimum required CQP version, i.e. the background process has
to be CQP version I<$major>.I<$minor>.I<$beta> or newer.
I<$minor> and I<$beta> may be omitted, in which case they default to 0.
Note that the B<CWB::CQP> module automatically checks whether the CQP version
is compatible with its own requirements when a new object is created.
The B<check_version> method can subsequently be used to check for a more
recent release that provides functionality needed by the Perl script.
=cut
sub check_version {
lib/CWB/CQP.pm view on Meta::CPAN
) {
return 1;
}
else {
return 0;
}
}
=item I<$version_string> = I<$cqp>->B<version>;
Returns formatted version string for the CQP background process, e.g. C<2.2.99> or C<3.0>.
=cut
sub version {
my $self = shift;
my $version = $self->{'major_version'}.".".$self->{'minor_version'};
my $beta = $self->{'beta_version'};
$version .= ".$beta"
if $beta > 0;
return $version;
lib/CWB/CQP.pm view on Meta::CPAN
if ($stderr_buffer ne "") {
$self->{'status'} = 'error'; # any output on stderr indicates that something went wrong
push @{$self->{'error_message'}}, split /\n/, $stderr_buffer; # append output on stderr to error message
$self->error(@{$self->{'error_message'}}); # may call error handler and abort, or print message and continue
}
return $lines;
}
=item I<$cqp>->B<run>(I<$cmd>);
Start a single CQP command I<$cmd> in the background. This method returns immediately.
Command output can then be read with the B<getline>, B<getlines> and B<getrow> methods.
If asynchronous communication is desired, use B<ready> to check whether output is available.
It is an error to B<run> a new command before the output of the previous command has completely
been processed.
=cut
sub run {
croak 'USAGE: $cqp->run($cmd]);'
( run in 1.090 second using v1.01-cache-2.11-cpan-d8267643d1d )