Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: While two conditions

by welle (Beadle)
on May 01, 2012 at 08:56 UTC ( #968215=note: print w/ replies, xml ) Need Help??


in reply to Re: While two conditions
in thread While two conditions

I am adapting Lingua::Concordance. It takes all occurences of $query and show it with its context in a ordered way.

Here is the code

$text="Oldfield's career began fairly early, playing <1> acoustic guit +ar in local folk clubs. At this time, he already had two fifteen-minu +te instrumental pieces in which he would "go through all <2>sorts of +moods", precursors to his landmark 1970s' compositions. In his early +teens, Oldfield was involved in a 'beat group' <3>playing Shadows-sty +le music (he has often cited Hank Marvin as a major influence, and wo +uld later cover The Shadows' song "Wonderful Land")." $query="in"; while ( $text =~ /$query/gi ) { my $match = $1; my $pos = pos( $text ); my $start = $pos - $self->radius - length( $match ); my $extract = ''; if ( $start < 0 ) { $extract = substr( $text, 0, $width + $start + length( $ma +tch )); $extract = ( " " x -$start ) . $extract; } else { $extract = substr( $text, $start, $width + length( $match +)); my $deficit = $width + length( $match ) - length( $extract + ); if ( $deficit > 0 ) { $extract .= ( " " x $deficit ) } } push @lines, $extract; }

What I am trying to do is to read the <> tag and associate it to the corresponding $extract (for example: "guitar in local folk" seen after <1>)


Comment on Re^2: While two conditions
Download Code
Re^3: While two conditions
by GrandFather (Cardinal) on May 01, 2012 at 11:11 UTC

    It's hard to tell exactly what you want to do, but a first guess is that my original suggestion was exactly on the money. Consider:

    use strict; use warnings; use Lingua::Concordance; my $text = <<TEXT; Oldfield's career began fairly early, playing <1> acoustic guitar in l +ocal folk clubs. At this time, he already had two fifteen-minute instrumental pi +eces in which he would "go through all <2>sorts of moods", precursors to his l +andmark 1970s' compositions. In his early teens, Oldfield was involved in a 'b +eat group' <3>playing Shadows-style music (he has often cited Hank Marvin as a ma +jor influence, and would later cover The Shadows' song "Wonderful Land"). TEXT my $query = 'in'; my $concordance = Lingua::Concordance->new (); $concordance->radius(25); $concordance->text($text); $concordance->query($query); my @matches = $concordance->lines(); my @pairs; for my $match (@matches) { while ($match =~ /<(\d+)>.*?$query/g) { push @pairs, map {[$_, $match]} $1 if $-[0] < $concordance->r +adius(); } } print "$_->[0]: $_->[1]\n" for @pairs;

    Prints:

    1: ng <1> acoustic guitar in local folk clubs. At thi 3: a 'beat group' <3>playing Shadows-style music (he
    True laziness is hard work

      Thank you.

      I see, I didn't explain my problem in a clear way. The problem of your code is that the tag must come inside the range of RADIUS (you set it at 25) to be detected. That is fine, but not the goal of my implementation. What I need is to print out the concordance PLUS the last tag seen before finding that concordance (i.e. before matching the $query).

      I'd like to put this information in a table "concordance of $query PLUS last tag seen before encountering $query".

      That is why I was trying to change the module's code.

        I considered that that may be your requirement, but I thought there were too many unknowns for me to sensibly implement sample code. The most important is what should happen where the last tag seen may be associated with multiple concordance fragments? I chose to only consider tags within the current fragment. Another option would be to reuse a tag for all following fragments until a new tag is found, or a tag could be associated with the first following fragment, or the first following fragment so long as it is within some maximum distance of the tag.

        I would first split the text by tag entries, then perform the concordance match on each fragment:

        use strict; use warnings; use Lingua::Concordance; my $text = <<TEXT; Oldfield's career began fairly early, playing <1> acoustic guitar in l +ocal folk clubs. At this time, he already had two fifteen-minute instrumental pi +eces in which he would "go through all <2>sorts of moods", precursors to his l +andmark 1970s' compositions. In his early teens, Oldfield was involved in a 'b +eat group' <3>playing Shadows-style music (he has often cited Hank Marvin as a ma +jor influence, and would later cover The Shadows' song "Wonderful Land"). TEXT my $concordance = Lingua::Concordance->new(); $concordance->text($text); my @parts = split /\s*(<\d+>)\s*/, $concordance->text(); my @frags; unshift @parts, '' if $parts[0] !~ /^<\d+>$/; push @frags, map {$_->[0] =~ s/[<>]//; $_} [splice @parts, 0, 2] while + @parts; $concordance->query('in'); for my $fragment (@frags) { my ($tag, $fragText) = @$fragment; $concordance->text($fragText); print "$tag: $_\n" for $concordance->lines(); }

        Prints:

        : fairly early, playing 1>: acoustic guitar in local folk clubs. A 1>: had two fifteen-minute instrumental pie 1>: wo fifteen-minute instrumental pieces in 1>: strumental pieces in which he would "go 2>: 0s' compositions. In his early teens, Ol 2>: ens, Oldfield was involved in a 'beat gr 2>: ield was involved in a 'beat group' 3>: playing Shadows-style musi 3>: en cited Hank Marvin as a major influenc 3>: Marvin as a major influence, and would l

        It should be fairly trivial to modify the loop to meet whatever additional constraints you require.

        True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968215]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2014-07-12 07:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls