With the help of some community members I have successfully used a script to parse a huge html file and add alternate spellings. However the substitution was too greedy and gave me unuseable inflection data "infl=". I need to limit the copy to only the value attribute (head word of a dictionary entry). Below is the the original version, the version created by the script, the desired output, and the script. Is there anyone who can suggest a change to this script that will prevent it from creating incorrect inflection data?

Original version

<idx:short><div height="4"><a name="83"/><div><idx:orth value="abänder +n" infl="abändere,abänderen,abänderest,abänderet,abändern,abänderst,a +bändert,abänderte,abänderten,abändertest,abändertet,abgeändert,abzuän +dern"/>

Result given from the script

<idx:short><div height="4"><a name="83"/><div><idx:orth value="abänd +ern" infl="abändere,abänderen,abänderest,abänderet,abändern,abänderst +,abändert,abänderte,abänderten,abändertest,abändertet,abgeändert,abzu +ändern"/> <idx:orth value="abaendern" infl="abaendere,abaenderen,abaenderest,aba +enderet,abaendern,abaenderst,abaendert,abaenderte,abaenderten,abaende +rtest,abaendertet,abgeaendert,abzuaendern"

Desired output

<idx:short><div height="4"><a name="83"/><div><idx:orth value="abänder +n" infl="abändere,abänderen,abänderest,abänderet,abändern,abänderst,a +bändert,abänderte,abänderten,abändertest,abändertet,abgeändert,abzuän +dern"/><idx:orth value="abaendern"/>

Script as it currently is

#!/usr/bin/perl use strict; use HTML::Parser; # set up a hash containing the umlauted characters and their replaceme +nts: my %replace = ( "\xC4" => 'Ae', "\xCF" => 'Ie', "\xD6" => 'Oe', "\xDC" => 'Ue', "\xE4" => 'ae', "\xEF" => 'ie', "\xF6" => 'oe', "\xFC" => 'ue', ); my $um = join '', keys %replace; binmode STDIN, ':utf8'; binmode STDOUT, ':utf8'; $/ = undef; my $input = <>; my $output = ''; my $p = HTML::Parser->new( api_version => 3, start_h => [ \&fix_umlaut, 'tagname, attr, +text' ], default_h => [ \&copy, 'text' ], ); $p->empty_element_tags( 1 ); $p->parse( $input ); print $output; sub fix_umlaut { my ( $tagname, $attr, $text ) = @_; $output .= $text; if ( $tagname eq 'idx:orth' and $$attr{value} =~ /[$um]/ ) { $text =~ s/([$um])/$replace{$1}/g; $output .= $text; # repeat the tag with modified umlauts } } sub copy { $output .= $_[0]; }

In reply to Limit substitution in html parsing by pcerda

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.