Lingua::ZH::ZhuYin::Dict - The backend dictionary for converting zhuyin


Lingua-ZH-ZhuYin documentation Contained in the Lingua-ZH-ZhuYin distribution.

Index


Code Index:

NAME

Top

Lingua::ZH::ZhuYin::Dict - The backend dictionary for converting zhuyin

VERSION

Top

Version 0.03

SYNOPSIS

Top

Quick summary of what the module does.

Perhaps a little code snippet.

    use Lingua::ZH::ZhuYin::Dict;

    my $zhuyin = Lingua::ZH::ZhuYin::Dict::QueryZuyin($word);
    ...

EXPORT

Top

A list of functions that can be exported. You can delete this section if you don't export anything, such as for a purely object-oriented module.

FUNCTIONS

Top

new

queryZhuYin

AUTHOR

Top

Cheng-Lung Sung, <clsung at cpan.org>

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


Lingua-ZH-ZhuYin documentation Contained in the Lingua-ZH-ZhuYin distribution.
package Lingua::ZH::ZhuYin::Dict;

use warnings;
use strict;

our $VERSION = 0.03;
use DBI qw(:sql_types);
use DBD::SQLite;

sub new {
    my $class = shift;
    my $self = {
	table => undef,
	dbh => undef,
    };
    my $dictFile = shift || 'moedict.db';
    if ( -f $dictFile ) {
	my $dsn = "dbi:SQLite:dbname=$dictFile";
	$self->{dbh} = DBI->connect($dsn, "","");
    }
    ($self->{table}) = ($dictFile =~ /(\w+)\.db/);
    $self->{table} ||= 'moedict';
    bless ($self, $class);
    return($self);
}

sub queryZhuYin {
    my $self = shift;
    my $word = shift;

    return unless $self->{dbh};

    my @zhuyins;
    my $table = $self->{table};
    my $sth = $self->{dbh}->prepare("SELECT zhuyin from $table WHERE word = ?");
    $sth->execute($word) || die DBI::err.": ".$DBI::errstr;
    while (my $hash_ref = $sth->fetchrow_hashref) {
	push @zhuyins, $hash_ref->{zhuyin};
    }
    return @zhuyins;
}

1; # End of Lingua::ZH::ZhuYin::Dict