The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Lingua::Identify::Blacklists - Language identification for related languages based on blacklists

VERSION

Version 0.02

SYNOPSIS

  use Lingua::Identfy::Blacklists qw/:all/;

  # detect language for a given text 
  # (discriminate between Bosanian, Croatian and Serbian)
  my $lang = identify( ".... text to be classified ...", 
                       langs => ['bs','hr','sr']);

  # use all languages available
  my $lang = identify( ".... text to be classified ...");

  # delect language in the given file (Unicode UTF-8 is assumed)
  my $lang = identify_file( $filename );

  # delect language for every line separately from the given file 
  # (return a list of lang-IDs)
  my @langs = identify_file( $filename, every_line => 1 );


  # learn classifiers (blacklists) for all pairs of languages 
  # given some training data
  train( { cs => $file_with_cs_text,
           sk => $file_with_sk_text,
           pl => $file_with_pl_text } );

  # learn a blacklist from a given pair of texts (prints to STDOUT)
  train_blacklist( $filename1, $filename2 );

  # ... the same but write to outfile
  train_blacklist( $filename1, $filename2, outfile => $outfilename );

  # train and evaluate the classification using given training/test data
  my @traindata = ($trainfile1, $trainfile2, $trainfile3);
  my @evaldata  = ($testfile1, $testfile2, $testfile3);
  run_experiment(\@traindata, \@evaldata, $lang1 $lang2, $lang3);

  # train with different parameters (optional)
  my %para = ( 
      min_high => 5,      # minimal token frequency in one langusgae
      max_low  => 2,      # maximal token frequency in the other language
      min_diff => 0.7 );  # score difference threshold

  train( { cs => $file_with_cs_text, sk => $file_with_sk_text }, %para );

Settings

Module-internal variables that can be modified:

 $BLACKLISTDIR   directory with all blacklists (default: module-share-dir)
 $LOWERCASE      lowercase all data, yes/no (1/0), default: 1
 $TOKENIZE       tokenize all data, yes/no (1/0), default: 1
 $ALPHA_ONLY     don't use tokens with non-alphabetic characters, default: 1
 $VERBOSE        verbose output (default=0)

Tokenization is very simple and replaces all non-alphabetic characters with a white-space character.

Exported Functions

$langID = identify( $text[,%options] )

Analyses a given text and returns a language ID as the result of the classification. %options can be used to change the behaviour of the classifier. Possible options are

  langs => \@list_of_possible_langs
  use_margin => $score

$langID = identify_file( $filename[,%options] )

Does the same as identify but reads text from a file. It also takes the same options as the 'identify' function but allows two extra options:

  text_size  => $size,  # number of tokens to be used for classification
  every_line => 1

Using the every_line option, the classifier checks every input line seperately and returns a list of language ID's.

 @langIDs = identify_file( $filename, every_line => 1, %options )

train(\%traindata[,%options])

Trains classifiers by learning blacklisted words for pairwise language discrimination. Returns nothing. Blacklists are stored in Lingua::Identify::Blacklists::BLACKLISTDIR/. You may have to run the process as administrator if you don't have write permissions.

%traindata is a hash of training data files associated with their corresponding language IDs:

  'hr' => $croatian_text_file,
  'sr' => $serbian_text_file,
  ...

%options is a hash of optional parameters that change the behaviour of the learning algorithm. Possible parameters are:

  min_high => $freq1,      # minimal token frequency in one langusgae
  max_low  => $freq2,      # maximal token frequency in the other language
  min_diff => $score,      # score difference threshold
  text_size => $size,      # maximum number of tokens to be used per text

train_blacklist($file1,$file2,%options)

This function learns a blacklist of words to discriminate between the language given in $file1 and the language given in $file2. It takes the same arguments (%options) as the train function above with one additional parameter:

 outfile => $output_file

Using this parameter, the blacklist will be written to the specified file. Otherwise it will be printed to STDOUT.

The function returns nothing otherwise.

@langs = available_languages()

Returns a list of languages covered by the blacklists in the BLACKLISTDIR.

@langs = available_blacklists()

Resturns a hash of available language pairs (for which blacklists exist in the system).

run_experiment(\@trainfiles,\@testfiles,\%options,@langs)

This function allows to run experiments, i.e. training and evaluating classifiers for the given languages (@langs). The arrays of training data and test data need to be of the same size as @langs. The function prints the overall accurcy and a confusion table given the data sets and the classification. %options can be used to set classifier-specific parameters.

Module-internal functions

The following functions are not exported and are mainly used for internal purposes (but may be used from the outside if needed).

 classify(\%dic,%options)          # run the classifier
 classify_cascaded(\%dic,@langs)   # run a cascade of binary classifications

 # run all versus all and return the one that wins most binary decisions
 # (a score margin is used to adjust the reliability of the decisions)
 classify_with_margin(\%dic,$margin,@langs) 

 load_all_blacklists()        # load all blacklists available in BLACKLISTDIR
 load_blacklist(\%list,$dir,$lang1,$lang2) # load a lang-pair specific blacklist
 read_file($file,\%dic,$max)  # read a file and count token frequencies
 process_string($string)      # process a given string (lowercasing ...)

AUTHOR

Jörg Tiedemann, https://bitbucket.org/tiedemann

BUGS

Please report any bugs or feature requests to https://bitbucket.org/tiedemann/blacklist-classifier. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Lingua::Identify::Blacklists

LICENSE AND COPYRIGHT

Copyright 2012 Jörg Tiedemann.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see http://www.gnu.org/licenses/.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.