Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

As part of an experiment I am running, I am interested in analyzing the phonological similarity between words. There are a number of ways to go about doing this, but for my purposes, the easiest seems to be calculating the (Damerau-Levenshtein) edit distance between two strings of IPA characters representing each word. I have the following perl code calculates the distance, pulling the words from a list in an excel spreadsheet, and spitting the output into a new excel spreadsheet.

Which is all well and good. For the most part, it works beautifully. The problem is that some phonemes in IPA are represented with two symbols (for example, ʣ), and perl is treating these as separate characters when calculating the edit distance. The best way around this seems to me to be to feed the IPA symbols into Perl as unicode strings ... however, after reading the online documentation on perlunicode, I am mostly just confused and not entirely sure how to implement it.

Is unicode the best way to do this? Are there any easier ideas that I am simply missing? Do I have to "use Encode"? I have seen some sample scripts that use it, and others that don't. Do I convert them to unicode in Perl, or should I do that in Excel BEFORE Perl reads them? Sorry this is a lot of questions thrown at you. My code is below ...

#!/usr/bin/perl use strict; use warnings; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; use Text::Levenshtein::Damerau qw(edistance); my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse ( 'input.xls' ); #Choose input file if ( !defined $workbook ) { die $parser->error(), ".\n"; } #Create output file my $workbook1 = Spreadsheet::WriteExcel->new('phon.xls'); #name of ou +tput file my $worksheet1 = $workbook1->add_worksheet('data'); $worksheet1->set_column(0, 4, 18); my $header_format = $workbook1->add_format( bold => 1, valign => 'vcenter', ); my $heading1 = 'Target'; my $heading2 = 'Response'; my $heading3 = 'Lev-Dam Distance'; $worksheet1->write('A1', $heading1, $header_format); $worksheet1->write('B1', $heading2, $header_format); $worksheet1->write('C1', $heading3, $header_format); WORKSHEET: #this allows perl to read Excel for my $worksheet ( $workbook->worksheet('PerlInput') ) { my $sheetname = $worksheet->get_name('PerlInput'); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); my $target_col; my $response_col; # Skip worksheet if it doesn't contain data if ( $row_min > $row_max ) { warn "\tWorksheet $sheetname doesn't contain data. \n"; next WORKSHEET; } # Check for column headers COLUMN: for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row_min, $col ); next COLUMN unless $cell; $target_col = $col if $cell->value() eq 'Target'; $response_col = $col if $cell->value() eq 'Response'; } if ( defined $target_col && defined $response_col ) { # Pull values from cells ROW: for my $row ( $row_min + 1 .. $row_max ) { my $target_cell = $worksheet->get_cell( $row, $target_co +l); my $response_cell = $worksheet->get_cell( $row, $response_ +col); if ( defined $target_cell && defined $response_cell ) { my $target = $target_cell->value(); my $response = $response_cell->value(); # Calculate Levenshtein-Damerau distance my $distance = edistance("$target","$response"); # Copy output to Excel spreadhseet, 'phon.xls' foreach ($target) { $row++; $worksheet1->write( $row-1, 0, "$target\n"); $worksheet1->write( $row-1, 1, "$response\n"); $worksheet1->write( $row-1, 2, "$distance\n"); } } # Error messages if something goes wrong else { warn "\tWorksheet $sheetname, Row = $row doesn't conta +in target and response data.\n"; next ROW; } } } else { warn "\tWorksheet $sheetname: Didn't find Target and Response +headings.\n"; next WORKSHEET; } }

Replies are listed 'Best First'.
Re: Reading IPA characters in Perl (Unicode?)
by moritz (Cardinal) on Feb 01, 2012 at 16:08 UTC
    Which is all well and good. For the most part, it works beautifully. The problem is that some phonemes in IPA are represented with two symbols (for example, ʣ), and perl is treating these as separate characters when calculating the edit distance.

    I've just tested Text::Levenshtein::Damerau with the ʣ digraph, and it treats it as a single character:

    use lib 'lib'; use strict; use warnings; use 5.010; use Text::Levenshtein::Damerau qw/edistance/; use charnames ":full"; use utf8; say edistance("e\N{LATIN SMALL LETTER DZ DIGRAPH}gar", 'edgar'); # output: 1

    So the problem seems to be that you don't pass properly decoded strings to edistance

    Is unicode the best way to do this? Are there any easier ideas that I am simply missing? Do I have to "use Encode"? I have seen some sample scripts that use it, and others that don't.

    Unicode is the best way to do this, yes.

    If your data comes in as a byte string, yes, you'll need use Encode and decode the data before passing it to edistance. And yes, some scripts use it, and others not, because not all example scripts do the same thing in the same way.

    I hope this article answers some of your questions. In the end you'll just have to learn about character encodings and how perl handles them, and then fix your problem with that knowledge.

      When you use:

      say edistance("e\N{LATIN SMALL LETTER DZ DIGRAPH}gar", 'edgar');

      What is the difference between writing out {LATIN SMALL LETTER DZ DIGRAPH} and using the actual Unicode number, in this case {0x02A3}? Are they functionally equivalent?

Re: Reading IPA characters in Perl (Unicode?)
by choroba (Cardinal) on Feb 01, 2012 at 16:17 UTC
    Maybe a bit off topic: In my opinion, distinctive features would be more appropriate than IPA characters. There is more similarity between p and b than between p and a.

      Distinctive features would be more accurate, but they are very complicated. I don't know how I would code that at all.

Re: Reading IPA characters in Perl (Unicode?)
by graff (Chancellor) on Feb 02, 2012 at 04:19 UTC
    So, is it the case that the input excel spreadsheet does not use Unicode characters? (Seems like it should, if it's trying to convey IPA symbols.) If there's unicode in the input spreadsheet, I expect it'll be encoded as UTF-16BE (go figure) -- here's an example for handling xls input with unicode content: xls2tsv.

    If the input isn't "pure unicode IPA", you'll probably end up with a look-up table (i.e. a hash) for transliterating various digraph character sequences (e.g. 'dz' as two characters) into the corresponding singular unicode code point. You do regex substitutions with each of those first, before doing your edit-distance computation -- e.g.

    my %translate = ( 'dz' => "\x{02a3}", 'ts' => "\x{02a6}" ... ); $_ = "string with ts and dz digraphs" for my $digraph ( keys %translate ) { s/$digraph/$translate{$digraph}/g; }

    As for the notion of using distinctive features instead, yeah, it's an attractive idea, but very tricky. Each original letter (phonemic segment) needs to become a "word", such that every "word" is the same length, comprising a fixed sequence of feature symbols. Creating the lookup table of letters/phonemes to feature symbol "word" strings will be half the work, and then trying to make sense of the edit-distance results on those strings will be the other half. (It's a lot of work.)

    Actually, I think just using the original phonetic/phonemic segment letters will suffice, since the diffs between related language varieties will tend to cluster around particular pairings of related phonemes, and from those pairings, the relationships of distinctive feature patterns will tend to be fairly obvious.

Re: Reading IPA characters in Perl (Unicode?)
by Anonymous Monk on Feb 01, 2012 at 15:58 UTC
    Provide your Excel file, else one cannot reproduce the problem.