<commit_before>package PanLex;
use strict;
use base 'Exporter';
use JSON;
use HTTP::Request;
use LWP::UserAgent;

use vars qw/@EXPORT/;
@EXPORT = qw/panlex_query panlex_query_all/;

$PanLex::ARRAY_MAX = 10000;

my $API_URL = "http://api.panlex.org";

# Send a query to the PanLex API at $url, with request body in $body.
# $body will automatically be converted to JSON, and the JSON response
# will be parsed and returned.
# If the request fails, the response will be undef.
sub panlex_query {
    my ($url, $body) = @_;
    
    $url = $url =~ m{^/} ? $API_URL . $url : $url;
    my $req = HTTP::Request->new(POST => $url);
    $req->content_type('application/json');
    $req->accept_decodable;
    $req->content(encode_json($body || {}));
    
    my $ua = LWP::UserAgent->new;
    my $res = $ua->request($req);
    
    if ($content ne '') {
        eval { $content = decode_json($content) };
        die $@ if $@;        
    }
    
    if ($res->code == 200) {
        return $content;
    } else {
        if (ref $content) {
            die "PanLex API returned $content->{code}: $content->{message}";
        } else {
            die "PanLex API returned status " . $res->code;
        }  
    }
}
<commit_msg>fix bug in last commit<commit_after>package PanLex;
use strict;
use base 'Exporter';
use JSON;
use HTTP::Request;
use LWP::UserAgent;

use vars qw/@EXPORT/;
@EXPORT = qw/panlex_query panlex_query_all/;

$PanLex::ARRAY_MAX = 10000;

my $API_URL = "http://api.panlex.org";

# Send a query to the PanLex API at $url, with request body in $body.
# $body will automatically be converted to JSON, and the JSON response
# will be parsed and returned.
# If the request fails, the response will be undef.
sub panlex_query {
    my ($url, $body) = @_;
    
    $url = $url =~ m{^/} ? $API_URL . $url : $url;
    my $req = HTTP::Request->new(POST => $url);
    $req->content_type('application/json');
    $req->accept_decodable;
    $req->content(encode_json($body || {}));
    
    my $ua = LWP::UserAgent->new;
    my $res = $ua->request($req);

    my $content = $res->decoded_content;
    
    if ($content ne '') {
        eval { $content = decode_json($content) };
        die $@ if $@;        
    }
    
    if ($res->code == 200) {
        return $content;
    } else {
        if (ref $content) {
            die "PanLex API returned $content->{code}: $content->{message}";
        } else {
            die "PanLex API returned status " . $res->code;
        }  
    }
}

{'fraction_non_alphanumeric': 0.15749296906388108, 'fraction_numerical': 0.0064282844515869825, 'mean_word_length': 2.5724533715925393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '173893', 'n_tokens_mistral': 878, 'n_tokens_neox': 838, 'n_words': 241}