Terrible perl code

So I’ve got a deep interest in Unix systems and have always admired perl code (mainly due to it’s line noise appearance) – so it is very gratifying to be able to code something in it.

I wrote a perl script that finds all unique words in all files specified in the command line arguments – these have to be plain text files.

The original version would then prompt and ask you if you knew the definition of unique words not in the “vocab.txt” database. The new version asks sentence by sentence if you understand the meaning.

If you respond “n” it then processes all words in the sentence and asks if you know the meaning of each word not in the database “vocab.txt”.

Now I wrote this code when I had very basic perl knowledge and so it uses a few global variables. I now know references and a few other things so I would write it differently.

The code does work and will retrieve the definition or translation of any unknown word. It will then add it to a database “unknown.txt” for future review.

You will need to setup a Google Cloud API key to access the translate function.

Be sure to create the “vocab.txt” and “unknown.txt” files. It is currently set to translate English to Korean but that is easily modified.

BTW the Cisco training is still progressing. I’m up to chapter 16 in the CCNA book! It’s rather dry stuff but I’m slowly getting through it (when not distracted with shiny new things).vocab

#!/usr/bin/perl5.22-aarch64-linux-gnu
=for comment
Copyright <2017> <Wilyarti Howard>
 
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
=cut
use strict;
use warnings;
use 5.22.1;
use Lingua::Sentence;
use WWW::Dictionary;
use WWW::Google::Translate;
use Error qw(:try);
binmode STDOUT, ":utf8";

our $vocab = "vocab.txt";
our $unknown = "unknown.txt";  


our %wordlist;
our %vocablist;
our $line;
our @elements;
our @vocabulary;
our @sentencearray;
our $translatekey;
our $sourcelang = "en";
our $targetlang = "ko";

$SIG{INT} = sub { close(MYFILE); close(MYVOCAB); die "Caught signal! Cleaning up.\n"; };

my $count_yes = 0;
my $count_no = 0;
my $total = 0;
my $dictionary = WWW::Dictionary->new();
my $wgt = WWW::Google::Translate->new( { key => 'insert your key here' } );


buildvocablist();
buildsentences();

sub process {
	open (our $MYFILE, ">>$unknown") or die "Error opening $unknown";
	open (our $MYVOCAB, ">>$vocab") or die "Error opening $unknown";
	for (@sentencearray) {
		%wordlist = buildwordlist($_);	
		print $_ . "\n\n";
		print "Do you understand the above sentance completely?: ";
		chomp(my $input = <STDIN>);
		if ($input eq "n") {
			$translatekey = $_;
			translator($translatekey);	
			while ((my $key, my $value) = each %wordlist) {
				if (!exists $vocablist{$key}) {
					print "\nDo you know the word \"$key\": ";
					chomp(my $input = <STDIN>);
					if ($input eq "n") {
						$translatekey = $key;
						translator($translatekey);
						print $MYFILE "$key\n";
						try {
							my $meaning = $dictionary->get_meaning($key);
							$meaning =~ m/(1[^2]*2)/;
							$meaning = $1;
							$meaning =~ s/\n//g;
							$meaning =~ s/2//g;
							print "Dictionary definition:\n" . $meaning . "\n";
							return;
						} catch Error with {
							print "ERROR: Can't  retrieve word definition. Check internet connection or that word is correct.\n";
						};
						$count_no++;
					} else {
						print $MYVOCAB "$key\n";
						$count_yes++;
					}		

				} else {
					print "Known word! $key\n";
					$count_yes++;
				}
				$total++;

			}
		} else {
			while ((my $key, my $value) = each %wordlist) {
				if (!exists $vocablist{$key}) {
					print $MYVOCAB "$key\n";
					$count_yes++;
					$total++;
				} else {
					$total++;
					$count_yes++;
				}
			}
		}
	}
	my $percentage = ($count_yes / $total) * 100;
	print "Congratultions you know $count_yes out of $total words. You got %$percentage correct!\n";
	close($MYVOCAB);
	close($MYFILE);
}


sub buildsentences {
	my $splitter = Lingua::Sentence->new("en");
	my $sentences;
	while (<>) {
		$sentences .= $_;

	}
	my $lines = $splitter->split($sentences);
	our @sentencearray = split ("\n", $sentences);
	@sentencearray = grep /\S/, @sentencearray;
	process(@sentencearray);


}
sub buildwordlist {
	$_ =~ s/[()\n\"\'?\/-<>,\.\t]/ /g;
	$_ =~  s/[^a-zA-Z0-9,]/ /g;
	#$_ =~ s/[A-Z,a-z,0-9]//g;
	$line = lc $_;
	@elements = split (" ", $line);
	for (@elements) {
		$wordlist{$_}++;
	}
	@elements =  sort keys %wordlist;
	return %wordlist;
}
sub buildvocablist {
	$line = "";
	open (my $MYVOCAB, "$vocab") or die "Error opening $vocab";
	while (<$MYVOCAB>) {
		chomp;
		$_ =~ s/[()\"\'?\/-<>,\.\t]/ /g;
		$line .= $_;
		$line .= "\n";
	}
	my @vocabelements = split ("\n", $line);
	for (@vocabelements) {
		$vocablist{$_}++;
	}
	@vocabelements = sort keys %vocablist;
#3	while ((my $key, my $value) = each %vocablist) {
#		print "key $key and value $value \n";
#	}
	close $MYVOCAB;
}

sub translator {
	print "\nTranslation: ";
	try {
		my $r = $wgt->translate(
		      {   q           => $translatekey, 
			  source      => $sourcelang,
			  target      => $targetlang,
			  model       => 'nmt',
			  format      => 'text',
			  prettyprint => 1,
		      }
		  );

		for my $trans_rh (@{ $r->{data}->{translations} }) {
			 print $trans_rh->{translatedText}, "\n";
		}				

	} catch Error with {
		print "ERROR: Can't  retrieve word definition. Check internet connection o
	r that word is correct.\n";
	};	

}

	

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s