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 | |
by Anonymous Monk on Feb 01, 2012 at 16:45 UTC | |
by moritz (Cardinal) on Feb 01, 2012 at 17:41 UTC | |
by Anonymous Monk on Feb 01, 2012 at 18:03 UTC | |
|
Re: Reading IPA characters in Perl (Unicode?)
by choroba (Cardinal) on Feb 01, 2012 at 16:17 UTC | |
by Anonymous Monk on Feb 01, 2012 at 16:39 UTC | |
|
Re: Reading IPA characters in Perl (Unicode?)
by graff (Chancellor) on Feb 02, 2012 at 04:19 UTC | |
|
Re: Reading IPA characters in Perl (Unicode?)
by Anonymous Monk on Feb 01, 2012 at 15:58 UTC |