Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Best way to print variables in regex

by Kenosis (Priest)
on Jan 20, 2014 at 23:22 UTC ( [id://1071393]=note: print w/replies, xml ) Need Help??


in reply to Best way to print variables in regex

Perhaps the following will be helpful:

use strict; use warnings; use Lingua::EN::Tagger qw(add_tags); my %tags; my $postagger = new Lingua::EN::Tagger; my $text = "the quick brown fox jumped over the lazy dog"; my $tagged = $postagger->add_tags($text); print $tagged, "\n\n"; $tags{ uc $1 }++ while $tagged =~ m!<([^/]+?)>!g; print "$_: $tags{$_}\n" for sort keys %tags;

Output:

<det>the</det> <jj>quick</jj> <jj>brown</jj> <nn>fox</nn> <vbd>jumped< +/vbd> <in>over</in> <det>the</det> <jj>lazy</jj> <nn>dog</nn> DET: 2 IN: 1 JJ: 3 NN: 2 VBD: 1

Replies are listed 'Best First'.
Re^2: Best way to print variables in regex
by Mordan (Initiate) on Jan 21, 2014 at 16:42 UTC

    Thank you Kenosis, your method seems the most straightforward. Thanks everyone who answered here and on the other thread.

    Are there any recommendations on how best to put this into a spreadsheet? I want to run this on a few phrases so think it would be a good idea to put it in a spreadsheet in a consistent way rather than copy and paste from terminal. So DET would values would always be in column 1, IN in 2.

      One way would be to create a CSV file and then import that into your spreadsheet:
      my $filename = '/path/to/file.csv'; open (my $fh, '>', $filename) or die "Could not open $filename, $!"; my @headers = qw( DET IN JJ NN VBD ); print $fh join(',',@headers) . "\n"; # then, for each of your phrases print $fh join(',', map($tags{$_} || 0, @headers) ) . "\n"; close $fh;
      However, if you intend to do the tagging at different times you will need a way to update the data. You could use Spreadsheet::WriteExcel but there is a learning curve and probably overkill. Alternatively, you can keep your spreadsheet data as a CSV file and append to that file, or use Tie::Array::CSV to append:
      use Tie::Array::CSV; my $filename = '/path/to/file.csv'; tie my @file, 'Tie::Array::CSV', $filename; # (this bit has been fixed - see comment below) # for each of your phrases my @row = map { $tags{$_} || 0 } @headers; push(@file,\@row); untie @file;

        That's great, thanks very much for your help

        One question on the append function - it puts it all into one cell. I can use text to columns in Excel to fix this but is there a way to do this in the append itself?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found