From d8a1dc2ffd90cc0fd9f7a1e4168a6f7ce78acdf5 Mon Sep 17 00:00:00 2001 From: Nigel Horne Date: Tue, 7 Jan 2025 14:24:40 -0500 Subject: [PATCH] Use pod2markdown to create README.md --- Changes | 4 ++ MANIFEST | 2 + Makefile.PL | 3 +- README.md | 45 ++++++++++++++- lib/Grammar/Improver.pm | 121 ++++++++++++++++++++++------------------ t/30-basics.t | 20 +++---- t/eof.t | 12 ++++ t/eol.t | 11 ++++ 8 files changed, 152 insertions(+), 66 deletions(-) create mode 100644 Changes create mode 100644 t/eof.t create mode 100644 t/eol.t diff --git a/Changes b/Changes new file mode 100644 index 0000000..bb5065e --- /dev/null +++ b/Changes @@ -0,0 +1,4 @@ +Revision history for Grammar-Improver - Use the LanguageTool API to rewrite text + +0.01 + First draft diff --git a/MANIFEST b/MANIFEST index 054dd4e..7b275d4 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5,3 +5,5 @@ Makefile.PL MANIFEST This list of files README.md t/30-basics.t +t/eof.t +t/eol.t diff --git a/Makefile.PL b/Makefile.PL index b677111..972ecf0 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -11,10 +11,11 @@ WriteMakefile( ? ('LICENSE'=> 'GPL') : ()), PREREQ_PM => { # Dependencies (with versions) + 'Carp' => 0, 'ExtUtils::MakeMaker' => 6.64, # Minimum version for TEST_REQUIRES 'LWP::UserAgent' => 0, 'LWP::Protocol::https' => 0, - 'JSON::XS' => 0 + 'JSON::XS' => 0 }, TEST_REQUIRES => { 'Test::DescribeMe' => 0, 'Test::Most' => 0, diff --git a/README.md b/README.md index 30cadad..956a7b3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,43 @@ -# Grammar-Improver -Use the LanguageTool API to rewrite text +# NAME + +Grammar::Improver - A Perl module for improving grammar using LanguageTool API. + +# VERSION + +Version 0.01 + +# SYNOPSIS + + use Grammar::Improver; + + my $improver = Grammar::Improver->new( + api_url => 'https://api.languagetool.org/v2/check', + ); + + my $text = 'This are a sample text with mistake.'; + my $corrected_text = $improver->improve_grammar($text); + + print "Corrected Text: $corrected_text\n"; + +# DESCRIPTION + +The `Grammar::Improver` module interfaces with the LanguageTool API to analyze and improve grammar in text input. + +# AUTHOR + +Nigel Horne + +# METHODS + +## new + + my $improver = Grammar::Improver->new(%args); + +Creates a new `Grammar::Improver` object. + +## improve\_grammar + + my $corrected_text = $improver->improve_grammar($text); + +Analyzes, improves and corrects the grammar of the input text. +Returns the corrected text. diff --git a/lib/Grammar/Improver.pm b/lib/Grammar/Improver.pm index ff6db45..20d4520 100644 --- a/lib/Grammar/Improver.pm +++ b/lib/Grammar/Improver.pm @@ -2,30 +2,81 @@ package Grammar::Improver; use strict; use warnings; +use Carp; use LWP::UserAgent; use JSON::MaybeXS; +=head1 NAME + +Grammar::Improver - A Perl module for improving grammar using LanguageTool API. + +=head1 VERSION + +Version 0.01 + +=cut + +our $VERSION = '0.01'; + +=head1 SYNOPSIS + + use Grammar::Improver; + + my $improver = Grammar::Improver->new( + api_url => 'https://api.languagetool.org/v2/check', + ); + + my $text = 'This are a sample text with mistake.'; + my $corrected_text = $improver->improve_grammar($text); + + print "Corrected Text: $corrected_text\n"; + +=head1 DESCRIPTION + +The C module interfaces with the LanguageTool API to analyze and improve grammar in text input. + +=head1 AUTHOR + +Nigel Horne + +=head1 METHODS + +=head2 new + + my $improver = Grammar::Improver->new(%args); + +Creates a new C object. + +=cut + # Constructor sub new { my ($class, %args) = @_; - my $self = { - api_url => $args{api_url} || 'https://api.languagetool.org/v2/check', # LanguageTool API - api_key => $args{api_key} || '', # Optional API key - }; - bless $self, $class; - return $self; + + return bless { + api_url => $args{api_url} || 'https://api.languagetool.org/v2/check', # LanguageTool API + api_key => $args{api_key}, # Optional API key + }, $class; } -# Method to improve grammar +=head2 improve_grammar + + my $corrected_text = $improver->improve_grammar($text); + +Analyzes, improves and corrects the grammar of the input text. +Returns the corrected text. + +=cut + sub improve_grammar { my ($self, $text) = @_; - die 'Text input is required' unless $text; + Carp::croak('Text input is required') unless $text; # Initialize the user agent my $ua = LWP::UserAgent->new(); - $ua->ssl_opts( # I know, I know - SSL_verify_mode => 0, + $ua->ssl_opts( + SSL_verify_mode => 0, # I know, I know verify_hostname => 0 ); @@ -35,11 +86,16 @@ sub improve_grammar { language => 'en-US', }; + # Send the API key in the payload + if($self->{api_key}) { + $payload->{'apiKey'} = $self->{'api_key'}; + } + # Convert the payload to URL-encoded form data my $response = $ua->post( $self->{api_url}, Content_Type => 'application/x-www-form-urlencoded', - Content => $payload, + Content => $payload, ); # Check for errors @@ -62,51 +118,10 @@ sub improve_grammar { } return $text; } else { - die 'Error: ', $response->status_line; + Carp::croak('Error: ', $response->status_line()); } } 1; __END__ - -=head1 NAME - -Grammar::Improver - A Perl module for improving grammar using LanguageTool API. - -=head1 SYNOPSIS - - use Grammar::Improver; - - my $improver = Grammar::Improver->new( - api_url => 'https://api.languagetool.org/v2/check', - ); - - my $text = 'This are a sample text with mistake.'; - my $corrected_text = $improver->improve_grammar($text); - - print "Corrected Text: $corrected_text\n"; - -=head1 DESCRIPTION - -The C module interfaces with the LanguageTool API to analyze and improve grammar in text input. - -=head1 METHODS - -=head2 new - - my $improver = Grammar::Improver->new(%args); - -Creates a new C object. - -=head2 improve_grammar - - my $corrected_text = $improver->improve_grammar($text); - -Analyzes and corrects the grammar of the input text. Returns the corrected text. - -=head1 AUTHOR - -Your Name - -=cut diff --git a/t/30-basics.t b/t/30-basics.t index 9d533a8..e50f217 100644 --- a/t/30-basics.t +++ b/t/30-basics.t @@ -15,11 +15,11 @@ isa_ok($improver, 'Grammar::Improver', 'Constructor returns a Grammar::Improver # Test grammar improvement functionality subtest 'Grammar improvement tests' => sub { - my $text = 'There are four light.'; - my $corrected_text = $improver->improve_grammar($text); + my $text = 'There is four lights.'; + my $corrected_text = $improver->improve_grammar($text); - ok($corrected_text, 'Corrected text is returned'); - is($corrected_text, 'There are four lights.', 'Grammar is improved correctly'); + ok($corrected_text, 'Corrected text is returned'); + is($corrected_text, 'There are four lights.', 'Grammar is improved correctly'); }; # Test invalid input @@ -29,13 +29,13 @@ subtest 'Error handling' => sub { # Test edge cases subtest 'Edge cases' => sub { - my $short_text = 'Hi.'; - my $corrected_text = $improver->improve_grammar($short_text); - is($corrected_text, $short_text, 'Handles short input gracefully'); + my $short_text = 'Hi.'; + my $corrected_text = $improver->improve_grammar($short_text); + is($corrected_text, $short_text, 'Handles short input gracefully'); - my $complex_text = 'He go to the store and buys some apples.'; - $corrected_text = $improver->improve_grammar($complex_text); - like($corrected_text, qr/He goes to the store/, 'Corrects verb agreement'); + my $complex_text = 'He go to the store and buys some apples.'; + $corrected_text = $improver->improve_grammar($complex_text); + like($corrected_text, qr/He goes to the store/, 'Corrects verb agreement'); }; done_testing(); diff --git a/t/eof.t b/t/eof.t new file mode 100644 index 0000000..a50018d --- /dev/null +++ b/t/eof.t @@ -0,0 +1,12 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Test::DescribeMe qw(author); +use Test::Most; +use Test::Needs 'Test::EOF'; + +Test::EOF->import(); +all_perl_files_ok({ minimum_newlines => 1, maximum_newlines => 4 }); +done_testing(); diff --git a/t/eol.t b/t/eol.t new file mode 100644 index 0000000..fbe9b40 --- /dev/null +++ b/t/eol.t @@ -0,0 +1,11 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Test::DescribeMe qw(author); +use Test::Most; +use Test::Needs 'Test::EOL'; + +Test::EOL->import(); +all_perl_files_ok({ trailing_whitespace => 1 });