patternMinor
Verb tense parser
Viewed 0 times
tenseparserverb
Problem
I have written this Perl code, and there is still more to add, however I was hoping that I could get some opinions on whether it could be written better.
Specifically, there is a central
Also, look at the 2nd block of code, and let me know if it's worth it to change this to a subroutine (either put entire
Generally, I'm just wondering if there are some better tricks to get the same job done. Perhaps using a array of hashes instead of array of arrays? For easier read. Would this prevent eq matches from appearing?
First, the declarations:
```
#!/usr/bin/perl
use strict;
use warnings FATAL => "all";
require 'verbTenseChanger.pl';
## -JUST FOR TESTING- ##
my $chapter_section = "chpt"."31_4";
my $search_key = "move";
my $category_id = "all";
# --- Files --- #
open(my $parse_corpus, ';
my $chapternumber;
my $sentencenumber;
my $sentence;
my $grammar_relation;
my $argument1;
my $argument2;
my @all_matches;
my $entirechapter = join ('',@entirechapter); ##Flatten file (make one big string)
#To get each sent. and info in one string:
my @sentblocks = split (/Parsing\s/,$entirechapter);##Remove "Parsing" which is on the line of the chptnumber
$chapternumber = $sentblocks[1]; ## file: chpt... will always be at [1]
# --- Retrieve necessary info from text --- #
#Loop through all the sentences, and for each check every form of searchverb
foreach my $sentblock (@sentblocks) {
foreach my $verbform (@verbforms) {
##"next" skips to next iteration of loop, substitute for an over-arching if statement
next unless ($sentblock =~ /\b$verbform\b/i); ##Ensure the sentence contains the searchkey
next unless ($sentblock =~ /\(VB\w*\s+\b$verbform\b\)\s+/i); ##Ensure searchkey is a verb
#Sent.number and Sentence:
next unless ($sentblock =~ /\[(sent. \d+) len. \d+\]: \[(.+)\]/); ##Remember, talking about th
Specifically, there is a central
if-elsif structure. Should I make this a subroutine or not? What's best practice? Also, look at the 2nd block of code, and let me know if it's worth it to change this to a subroutine (either put entire
if-else into sub, or just the inner portion).Generally, I'm just wondering if there are some better tricks to get the same job done. Perhaps using a array of hashes instead of array of arrays? For easier read. Would this prevent eq matches from appearing?
First, the declarations:
```
#!/usr/bin/perl
use strict;
use warnings FATAL => "all";
require 'verbTenseChanger.pl';
## -JUST FOR TESTING- ##
my $chapter_section = "chpt"."31_4";
my $search_key = "move";
my $category_id = "all";
# --- Files --- #
open(my $parse_corpus, ';
my $chapternumber;
my $sentencenumber;
my $sentence;
my $grammar_relation;
my $argument1;
my $argument2;
my @all_matches;
my $entirechapter = join ('',@entirechapter); ##Flatten file (make one big string)
#To get each sent. and info in one string:
my @sentblocks = split (/Parsing\s/,$entirechapter);##Remove "Parsing" which is on the line of the chptnumber
$chapternumber = $sentblocks[1]; ## file: chpt... will always be at [1]
# --- Retrieve necessary info from text --- #
#Loop through all the sentences, and for each check every form of searchverb
foreach my $sentblock (@sentblocks) {
foreach my $verbform (@verbforms) {
##"next" skips to next iteration of loop, substitute for an over-arching if statement
next unless ($sentblock =~ /\b$verbform\b/i); ##Ensure the sentence contains the searchkey
next unless ($sentblock =~ /\(VB\w*\s+\b$verbform\b\)\s+/i); ##Ensure searchkey is a verb
#Sent.number and Sentence:
next unless ($sentblock =~ /\[(sent. \d+) len. \d+\]: \[(.+)\]/); ##Remember, talking about th
Solution
You, sir, need a Strategy pattern -- at least to resolve your if->elseif problem. I find a Gang of Four style reference like http://www.blackwasp.co.uk/Strategy.aspx to be a little easier on the brain for initial consumption of a new pattern than the perl deep-dive, but YMMV.
Context
StackExchange Code Review Q#2911, answer score: 2
Revisions (0)
No revisions yet.