Skip to content

Commit

Permalink
Use pod2markdown to create README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Jan 7, 2025
1 parent 078078d commit d8a1dc2
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 66 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Revision history for Grammar-Improver - Use the LanguageTool API to rewrite text

0.01
First draft
2 changes: 2 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Makefile.PL
MANIFEST This list of files
README.md
t/30-basics.t
t/eof.t
t/eol.t
3 changes: 2 additions & 1 deletion Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <njh@bandsman.co.uk>

# 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.
121 changes: 68 additions & 53 deletions lib/Grammar/Improver.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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<Grammar::Improver> module interfaces with the LanguageTool API to analyze and improve grammar in text input.
=head1 AUTHOR
Nigel Horne <njh@bandsman.co.uk>
=head1 METHODS
=head2 new
my $improver = Grammar::Improver->new(%args);
Creates a new C<Grammar::Improver> 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
);

Expand All @@ -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
Expand All @@ -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<Grammar::Improver> 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<Grammar::Improver> 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 <your.email@example.com>
=cut
20 changes: 10 additions & 10 deletions t/30-basics.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
12 changes: 12 additions & 0 deletions t/eof.t
Original file line number Diff line number Diff line change
@@ -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();
11 changes: 11 additions & 0 deletions t/eol.t
Original file line number Diff line number Diff line change
@@ -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 });

0 comments on commit d8a1dc2

Please sign in to comment.