in reply to Re: bracket processing
in thread bracket processing

You could just use a loop, dealing with each bracketed part that is encountered. Here's a basic technique:

#!/usr/bin/env perl use strict; use warnings; use constant { EXTRACTED => 0, SUFFIX => 1, PREFIX => 2, }; use Text::Balanced 'extract_bracketed'; my $delim = '([{<'; my $prefix = qr{[^$delim]*}; my $string = 'The (use {of}) parentheses (indicates that the (writer [ +considered] the {information}) less <important—almost> an afterthough +t).'; my ($current, $wanted) = ($string, ''); while (1) { my @parts = extract_bracketed($current, $delim, $prefix); if (defined $parts[PREFIX]) { my ($trimmed_start) = $parts[PREFIX] =~ /^(.*?)\s*$/; $wanted .= $trimmed_start; } if (defined $parts[EXTRACTED]) { $current = $parts[SUFFIX]; } else { $wanted .= $parts[SUFFIX]; last; } } print "$wanted\n";

Output:

The parentheses.

I'll leave you to integrate that technique into your actual production code.

[The lack of sample data — as requested by myself and expanded upon by AnomalousMonk — was disappointing.]

— Ken

Replies are listed 'Best First'.
Re^3: bracket processing
by rajaman (Sexton) on Apr 02, 2020 at 02:16 UTC
    Excellent, thanks much Ken. That helps! Sorry I missed samples, attaching a few here.

    I am starting to learn text analysis (e.g. sentiment analysis on reviews/opinion). Here is the link to the full dataset: http://www.cs.jhu.edu/~mdredze/datasets/sentiment/ . Many are also available on google.

    Few samples are shown below. Seems text processing is far more complex than I realized. Lots of variations/edge cases. Just came across cases where brackets can't be removed as it makes the text pretty ackward, something like: 'Company is good because of: (1) quick service, (2) product range..'.

    #samples The largest of these (4 quart{16 cups}) will hold all but a little bit + of a bag of flour. I like the look of the stainless steel and the fa +ct of the seal around the lid. I {mistakenly} did not recieve my order (it was a different order i wa +s expecting), and they rushed me another collar. The rougher the machine the more wear on the fabric and more pilling, +lint, etc.... I would highly recommend these sheets for the price an +d the great quality for the price. [I would rate them 5 stars if the +y were heavier and soft as cashmere, but for the money definitely a g +ood buy.]

      Please let me suggest once again a reading of How to ask better questions using Test::More and sample data. What I and some other monks would like to see is something like:

      my @Tests = ( [ 'The largest (4 quart{16 cups}) will hold most of a bag of flour +.', # input string 'The largest will hold most of a bag of flour.', + # expected output ], [ 'I {mistakenly} did not recieve my order (it was a different ord +er), and they rushed me another.', 'I did not recieve my order, and they rushed me another.', ], [ 'Company is good because of: (1) quick service, (2) product rang +e.', 'Company is good because of: (1) quick service, (2) product rang +e.', 'parenthesized ordinals untouched', # optional comment ], [ '...', '...', ], # another test case ... # more test cases );
      This can integrate easily into a Test::More testing framework. Individual test cases should be as short as possible and should always be paired with exact expected output. Degenerate and trivial cases, e.g.,
          ''  ' '  '()'  ' ()'  '() '  'x'  'foo'  etc, etc...
      should also be given. (Update: If exceptions may be thrown, a test case or cases should ultimately be written for each exception; see Test::Exception.) Note that this is work you will have to do anyway because you must always build a testing framework for any serious project you undertake!

      What I, for one, do not like to see is an invitation to do a lot of work to generate test case input for which I must also guess the correct output; that's your job.


      Give a man a fish:  <%-{-{-{-<

        Thanks AnomalousMonk, appreciate your advice. Have never used Test::More, so will learn now. Will also post some interesting test cases that I might come across.