4 Jun 2012

Howto build deb package with Perl module (Lingua::Identify)

What is Lingua::Identify?

Lingua::Identify is Perl module used for text language identification - remember that language identification is not 100% accurate.

Why use Lingua::Identify?

Here is list of most important (for me) features of this module
  • it's free and it's open-source;
  • it has unicode support in standard;
  • it's a module, which means you can easily write your own application
  • it supports big inputs
  • it's maintained

How to create deb package of Lingua::Identify?

# Install packages required to build *.deb
apt-get install dh-make-perl devscripts apt-file
apt-file update

# Install Lingua::Identify dependences from packages
apt-get install libclass-factory-util-perl libtext-affixes-perl

# Lingua::identify depends on Text::Ngram (but this is not packaged)
# http://search.cpan.org/~ambs/Text-Ngram-0.14/lib/Text/Ngram.pm
wget http://search.cpan.org/CPAN/authors/id/A/AM/AMBS/Text/Text-Ngram-0.14.tar.gz

# http://search.cpan.org/~ambs/Lingua-Identify-0.51/lib/Lingua/Identify.pm
wget http://search.cpan.org/CPAN/authors/id/A/AM/AMBS/Lingua/Lingua-Identify-0.51.tar.gz

# Make subdirectory
mkdir test
mv *.tar.gz test/
cd test

# Build Text::Ngram
tar -pzxvf Text-Ngram-0.14.tar.gz 
mv Text-Ngram-0.14.tar.gz libtext-ngram-perl_0.14.orig.tar.gz
dh-make-perl Text-Ngram-0.14
cd Text-Ngram-0.14
debuild -us -uc

# Test package (install it)
cd ..
sudo dpkg -i libtext-ngram-perl_0.14-1_i386.deb
perl -e 'use Text::Ngram qw(ngram_counts add_to_counts);'

# Build Lingua::Identify (depends on Text::Ngram)
tar -pzxvf Lingua-Identify-0.51.tar.gz
mv Lingua-Identify-0.51.tar.gz liblingua-identify-perl_0.51.orig.tar.gz
dh-make-perl Lingua-Identify-0.51
cd Lingua-Identify-0.51/
debuild

# Install our new package
cd ..
sudo dpkg -i liblingua-identify-perl_0.51-1_all.deb 

Finish!

Your first Perl Ubuntu/Debian package is ready and installed, it is not perfect but it works! If you want to push your deb package to repository, you should learn more, more and some more.

Supplement material

If you are interested in writing your own lingua identifier using ngram method here are some links that could be helpful:

TP-LINK TD-8910G - restart połączenia

Ponad dwa lata temu opisywałem router TP-LINK TD-W8910G urządzenie wciąż sprawuje się wyśmienicie.

Ostatnimi czasy chciałem dokonać zmiany adresu IP w regularnych odstępach czasu, oczywiście jak się można łatwo domyślić router TP-LINK nie posiada wbudowanej takiej opcji. Ale nic straconego, jako że urządzenie ma dosyć prosty interfejs do zarządzania dostępny poprzez telnet oraz www, napisałem dwa proste skrypty resetujące połączenie z moim ISP.

Pierwszy z nich dokonuje tylko restartu połączenia - do poprawnego działania wymaga zainstalowanego narzędzia expect (apt-get install expect).

Drugi natomiast służy do wykonania pełnego restartu routera i wymaga narzędzia curl (apt-get install curl).

Skrypt restartujący połączenie w TP-LINK

#!/usr/bin/expect

# variables
set router 192.168.1.1
set username admin
set password xxxxx

# code
set timeout 100
spawn telnet "$router"
expect "Login:" 
send "$username\r" 
expect "Password:" 
send "$password\r" 
expect ">"
send "adsl configure\r" 
expect "Hit <enter> to continue" 
send ">" 
expect ">"

Skrypt restartujący router TP-LINK

#!/bin/bash

router="192.168.1.1"
username="admin"
password="xxxxx"

for URL in "/", "/resetrouter.html", "/rebootinfo.cgi" ; do
 curl -H "GET ${URL} HTTP/1.1" \
   -H "Host: ${router}" \
   --basic -u "${username}:${password}" \
   "http://${router}${URL}"
 if [ $? -ne 0 ] ; then
  echo "Problem restarting router - ${router}!"
  exit
 fi
done
Co ciekawe przy przy mojej wersji oprogramowania, router się restartuje nawet podczas podania nieprawidłowego hasła w skrypcie nr #2.

Drobne porady dla zaawansowanego użytkownika

ps: Polecam czasami aktualizować oprogramowanie, dochodzą nowe funkcjonalności :).

25 Dec 2011

List all installed and available Perl modules

Code below provides list of modules which perl interpreter can find in runtime.

Code

#!/usr/bin/perl
#
# Author:  Tomasz Gaweda
# Date:    2011.12.23 17:00
# Purpose: Print all modules installed 
#          in perl distribution
#
# Usage:   perl showmodules.pl

use strict;
use warnings;
use File::Find;

my @modules;
my $IgnorePatt = join("|",@INC) ;#$IgnorePatt =~ s/\|\.// ;
find(
 sub {
  my $f = $File::Find::name;
  if ( $f =~ s/^(${IgnorePatt})\/(.*?)\.pm$/$2/ ) 
  {
   $f =~ s/[\/\\]/::/g;
   push @modules, $f;
  }
 }
 , grep { -d } @INC
);
print "There are [".scalar(@modules)."] modules that perl can locate on runtime...\n";
print "Printing List in Minute\n" ;
sleep 3;
print join("\n", sort{ lc $a cmp lc $b} @modules)."\n";

Sample output

There are [2346] modules that perl can locate on runtime...
Printing List in Minute
AcidRip::acidrip
AcidRip::interface
AcidRip::messages
AcidRip::signals
AE
Algorithm::C3
Algorithm::Diff
Algorithm::Diff::XS
Algorithm::DiffOld
Algorithm::Merge
Alien::Package
...