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; } }

In reply to Reading IPA characters in Perl (Unicode?) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.