| 1 | package Grammar::Improver; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | use LWP::UserAgent; | ||
| 7 | use JSON::MaybeXS; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Grammar::Improver - A Perl module for improving grammar using LanguageTool API. | ||
| 12 | |||
| 13 | =head1 VERSION | ||
| 14 | |||
| 15 | Version 0.02 | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | our $VERSION = '0.02'; | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use Grammar::Improver; | ||
| 24 | |||
| 25 | my $improver = Grammar::Improver->new( | ||
| 26 | api_url => 'https://api.languagetool.org/v2/check', | ||
| 27 | api_key => $ENV{'LANGUAGETOOL_KEY'}, | ||
| 28 | ); | ||
| 29 | |||
| 30 | my $text = 'This are a sample text with mistake.'; | ||
| 31 | my $corrected_text = $improver->improve_grammar($text); | ||
| 32 | |||
| 33 | print "Corrected Text: $corrected_text\n"; | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | The C<Grammar::Improver> module interfaces with the LanguageTool API to analyze and improve grammar in text input. | ||
| 38 | |||
| 39 | =head1 METHODS | ||
| 40 | |||
| 41 | =head2 new | ||
| 42 | |||
| 43 | my $improver = Grammar::Improver->new(%args); | ||
| 44 | |||
| 45 | Creates a new C<Grammar::Improver> object. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | # Constructor | ||
| 50 | sub new { | ||
| 51 | my ($class, %args) = @_; | ||
| 52 | |||
| 53 | return bless { | ||
| 54 | api_url => $args{api_url} || 'https://api.languagetool.org/v2/check', # LanguageTool API | ||
| 55 | api_key => $args{api_key}, # Optional API key | ||
| 56 | }, $class; | ||
| 57 | } | ||
| 58 | |||
| 59 | =head2 improve_grammar | ||
| 60 | |||
| 61 | my $corrected_text = $improver->improve_grammar($text); | ||
| 62 | |||
| 63 | Analyzes, improves and corrects the grammar of the input text. | ||
| 64 | Returns the corrected text. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub improve_grammar { | ||
| 69 | my ($self, $text) = @_; | ||
| 70 | |||
| 71 | Carp::croak('Text input is required') unless $text; | ||
| 72 | |||
| 73 | # Initialize the user agent | ||
| 74 | my $ua = LWP::UserAgent->new(); | ||
| 75 | $ua->ssl_opts( | ||
| 76 | SSL_verify_mode => 0, # I know, I know | ||
| 77 | verify_hostname => 0 | ||
| 78 | ); | ||
| 79 | |||
| 80 | # Create the request payload | ||
| 81 | my $payload = { | ||
| 82 | text => $text, | ||
| 83 | language => 'en-US', | ||
| 84 | }; | ||
| 85 | |||
| 86 | # Send the API key in the payload | ||
| 87 | if($self->{api_key}) { | ||
| 88 | $payload->{'apiKey'} = $self->{'api_key'}; | ||
| 89 | } | ||
| 90 | |||
| 91 | # Convert the payload to URL-encoded form data | ||
| 92 | my $response = $ua->post( | ||
| 93 | $self->{api_url}, | ||
| 94 | Content_Type => 'application/x-www-form-urlencoded', | ||
| 95 | Content => $payload, | ||
| 96 | ); | ||
| 97 | |||
| 98 | # Check for errors | ||
| 99 | if(!$response->is_success()) { | ||
| 100 | Carp::croak('Error: ', $response->status_line()); | ||
| 101 | } | ||
| 102 | |||
| 103 | my $response_content = $response->decoded_content; | ||
| 104 | my $response_data = decode_json($response_content); | ||
| 105 | |||
| 106 | # ::diag(Data::Dumper->new([$response_data])->Dump()); | ||
| 107 | |||
| 108 | # Apply corrections | ||
| 109 | foreach my $match (reverse @{ $response_data->{matches} }) { | ||
| 110 | my $offset = $match->{offset}; | ||
| 111 | my $length = $match->{length}; | ||
| 112 | my $replacement = $match->{replacements}[0]{value} || ''; | ||
| 113 | |||
| 114 | # print "offset = $offset, length = $length, replacement = $replacement\n"; | ||
| 115 | |||
| 116 | # Apply replacement to text | ||
| 117 | substr($text, $offset, $length, $replacement); | ||
| 118 | } | ||
| 119 | return $text; | ||
| 120 | } | ||
| 121 | |||
| 122 | =head1 AUTHOR | ||
| 123 | |||
| 124 | Nigel Horne <njh@bandsman.co.uk> | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | 1; | ||
| 129 | |||
| 130 | __END__ |