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.03

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']);

  # check if the assumed language ('hr') is confused with another one
  my $lang = identify( ".... text to be classified ...", assumed => 'hr' );

  # use a general-purpose identfier and check confusable langauges if necessary
  my $lang = identify( ".... text to be classified ...");

  # delect language in the given file (Unicode UTF-8 is assumed)
  my $lang = identify_file( $filename, langs => [...] );
  my $lang = identify_file( $filename, 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, langs = [...] );
  my @langs = identify_file( $filename, every_line => 1, assumed = '..' );
  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 );

Description

This module adds a blacklist classifier to a general purpose language identification tool. Related languages can easily be confused with each other and standard language detection tools do not work very well for distinguishing them. With this module one can train so-called blacklists of words for language pairs containing words that should not (or very rarely) occur in one language while being quite common in the other. These blacklists are then used to discriminate between those "confusable" related languages.

Since version 0.03 it also integrates a standard language identifier (Lingua::Identify::CLD) and can now be used for general language identification. It calls the blacklist classifier only for those languages that can be confused and for which appropriate blacklists are trained.

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
 $MAX_LINE_LENGTH  max line length when reading from files (default=2**16)
 $CLD_TEXT_SIZE    text size in characters used for language ident. with CLD
 $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

  assumed    => $assumed_lang
  langs      => \@list_of_possible_langs
  use_margin => $score

If langs are specified, it runs the classifier with blacklists for those languages (in a cascaded way, i.e. best1 = lang1 vs lang2, best2 = best1 vs lang3, ...). If use_margin is specified, it runs all versus all and returns the language that wins the most (with margin=$score).

If the assumed language is given, it runs the blacklist classifier for all languages that can be confused with $assumed_lang (if blacklist models exist for them).

If neither langs not assumed are specified, it first runs a general-purpose language identification (using Lingua::Identify::CLD and Lingua::Identify) and then checks with the blacklist classifier whether the detected language can be confused with another one. For example, CLD frequently classifies Serbian and Bosnian texts as Croatian but the blacklist classifier will detect that (and hopefully correct the decision).

$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 )

$langID = identify_stdin( [,%options] )

The same as identify_file but reads from STDIN

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.

%lists = available_blacklists()

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

 %lists = ( srclang1 => { trglang1a => blacklist1a, trglang1b => blacklist1b },
            srclang2 => { trglang2a => blacklist2a, ... }
            .... )

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).

 initialize()                     # reset the repository of blacklists
 identify_language($text)         # return lang-ID for $text (using CLD)
 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_blacklists($dir)                # load all blacklists available in $dir
 load_blacklist(\%list,$dir,      # load a lang-pair specific blacklist
                $lang1,$lang2)  
 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

SEE ALSO

This module is designed for the discrimination between closely related languages. For general-purpose language identification look at Lingua::Identify, Lingua::Identify::CLD and Lingua::Ident

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.