-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWordzilla
executable file
·209 lines (169 loc) · 5.83 KB
/
Wordzilla
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Getopt::Long qw(:config ignore_case pass_through);
use Term::ANSIColor;
use Text::Wrap;
use Pod::Usage;
# Set the DB location and connection parameters
my $db_file = "$ENV{HOME}/Wordzilla";
my $dbh = DBI->connect( "dbi:SQLite:database=$db_file", "", "" );
my ( $meanings_required, $part_of_speech_option, $color_required, $help, $man );
$Text::Wrap::columns = 80;
GetOptions(
"m=i" => \$meanings_required,
"p=s" => \$part_of_speech_option,
"c!" => \$color_required,
'help|?' => \$help,
'man!' => \$man
);
sub dictionary {
my $word = shift;
$word = lc($word);
my $char = substr( $word, 0, 1 );
my $sth;
# Search with the part of speech if defined or search for all meanings.
if ( defined($part_of_speech_option) ) {
$part_of_speech_option = ucfirst lc $part_of_speech_option;
my $query = "SELECT * from $char where word = ? and part = ? ";
$sth = $dbh->prepare($query);
$sth->execute( $word, $part_of_speech_option );
}
else {
my $query = "SELECT * from $char where word = ? ";
$sth = $dbh->prepare($query);
$sth->execute($word);
}
# Push the part of speech and meaning into an array
my $meaning_count = 0;
my @meanings;
while ( my @row = $sth->fetchrow_array ) {
$meaning_count++;
my ( $part_of_speech, $meaning ) = @row[ 1, 2 ];
push @meanings, [ $part_of_speech, $meaning ];
}
#Check for number of meanings found and also the number of meanings required
if ( $meaning_count != 0 ) {
if ( defined($meanings_required) ) {
while ( $meanings_required != 0 and @meanings ) {
$meanings_required--;
my ( $part_of_speech, $meaning ) = @{ shift @meanings };
print_part_of_speech_and_meaning( $part_of_speech, $meaning );
}
}
else {
while (@meanings) {
my ( $part_of_speech, $meaning ) = @{ shift @meanings };
print_part_of_speech_and_meaning( $part_of_speech, $meaning );
}
}
}
else {
if ( defined($part_of_speech_option) ) {
print "No meanings were found for $word as $part_of_speech_option \n";
}
else {
print "No words found \n";
}
}
}
# Print the part_of_speech and meaning
# print the word if the part of speech is option is set and matches the given one
sub print_part_of_speech_and_meaning() {
my ( $part_of_speech, $meaning ) = @_;
if ( defined($part_of_speech_option) ) {
if ( $part_of_speech =~ /$part_of_speech_option/i ) {
print_part_of_speech($part_of_speech);
print_meaning($meaning);
}
}
else {
print_part_of_speech($part_of_speech);
print_meaning($meaning);
}
}
# Print the part of speech in red color if the color option is set
sub print_part_of_speech {
my $part_of_speech = shift @_;
print color('red') if $color_required;
print "$part_of_speech \n";
}
# Print the meaning in green color if the color option is set
sub print_meaning {
my $meaning = shift @_;
print color('green') if $color_required;
print wrap( "\t", "\t", "$meaning" );
print "\n\n";
print color('reset');
}
# Check if the word has only letters and is defined
sub check_word {
my ($word) = @_;
return ( defined $word and length($word) != 0 and $word =~ /^[a-z]+$/i );
}
# Print the help text for help option
pod2usage(1) if $help;
pod2usage(-exitval => 0, -verbose => 2) if $man;
# Check the part of speech switch and die if its invalid
if ( defined($part_of_speech_option) ) {
my $part_of_speech_regex = qr/noun|verb|adjective|adverb|preposition|conjunction|pronoun|interjection/i;
die "Unknown part of speech option\n" unless $part_of_speech_option =~ $part_of_speech_regex;
}
# @ARGV contains the invalid switches and options in addition to the word.
# If its more than one then print the unknown option error and die.
if ( @ARGV > 1 ) {
die "Number of meanings should be a number\n" if $ARGV[0] =~ m/m/;
die "Unknown option $ARGV[0]\n";
}
# If word is passed as argument and valid then search the dictionary
# Prompt for the word if not found
if (@ARGV) {
chomp( my $word = $ARGV[-1] );
die "Please enter a valid word\n" unless defined $word;
$word =~ s/^\s+|\s+$//gs;
if ( check_word($word) ) {
dictionary($word);
}
else {
print "Please enter a valid word\n";
}
}
else {
print "Enter your word : \n";
my $word = <STDIN>;
die "Please enter a valid word\n" unless defined $word;
chomp($word);
$word =~ s/^\s+|\s+$//gs;
if ( check_word($word) ) {
dictionary($word);
}
else {
print "\nPlease enter a valid word\n";
}
}
__END__
=head1 NAME
Wordzilla - A command line dictionary
=head1 SYNOPSIS
wordzilla [options] word
-help print the help text
-c print the meaning and part of speech in color
-p takes the part of speech as the argument
-m takes the number of meanings to be displayed as argument
-man display the man page
=head1 ACKNOWLEDGEMENTS
Thanks to Wikitionary, Larry wall and SQLite.
=head1 DEDICATION
In dedication to my parents, friends and fond sister.
=head1 COPYRIGHTS & LICENSE
This software is copyright (c) 2014 by tir.karthi@gmail.com
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
The database is available under the Creative Commons license.
=head1 AUTHOR
Karthikeyan, L<tir.karthi@gmail.com>
=head1 BUGS
Please report any bugs at https://github.com/tirkarthi/Wordzilla-Perl
=head1 SUPPORT
Support for installation and other issues at https://github.com/tirkarthi/Wordzilla-Perl
Its also available as an Android app at Google Play Store by the name Wordzilla.