blob: c5f48ceec03123db7d3e91e231fb9f99b05f78bf [file] [log] [blame]
Marc Kupietz3e3e3262022-04-12 23:11:45 +02001use strict;
2use warnings;
Marc Kupietz565274e2026-09-06 13:17:17 +02003use Test::More tests=>13;
Marc Kupietz3e3e3262022-04-12 23:11:45 +02004use Mojo::JSON qw(decode_json encode_json to_json);
5use REST::Client;
6use Data::Dump qw(dump);
7
8my $pid = fork();
9my $res;
10unless ($pid) {
11 unless (fork) {
12 exec "MOJO_CONFIG=../example.conf morbo script/derekovecs-server";
13 die "exec failed!";
14 }
15 exit(0);
16}
17sleep(20);
18
19my $client = REST::Client->new();
20$client->GET('http://localhost:3000/?word=Grund&json=1');
21$res = decode_json($client->responseContent());
22is( $res->{list}->[0]->[1]->{word}, "Reaktion", "primary paradigmatic neighbour of Grund" );
23is( $res->{collocators}->[0]->{word}, "Hitchcock", "primary syntagmatic neighbour of Grund" );
24
Marc Kupietz565274e2026-09-06 13:17:17 +020025# "König - Mann + Frau": the neighbours of a position that belongs to no
26# single word, and no syntagmatic answer for it.
27$client->GET('http://localhost:3000/?word=K%C3%B6nig+-+Mann+%2B+Frau&json=1');
28$res = decode_json($client->responseContent());
29is( $res->{list}->[0]->[0]->{word}, "Dareios", "primary neighbour of a vector expression" );
30ok( $res->{collocators} && @{$res->{collocators}} > 0, "a vector expression has syntagmatic neighbours too" );
31
32# The syntagmatic side used to read the input weights of the first operand
33# alone: "Haus Auto" answered with the collocators of "Haus", "Auto Haus" with
34# those of "Auto". It combines them now, so the order no longer matters and
35# neither single word answer comes back unchanged.
36$client->GET('http://localhost:3000/?word=Haus&json=1');
37my $haus = decode_json($client->responseContent());
38$client->GET('http://localhost:3000/?word=Haus+Auto&json=1');
39my $haus_auto = decode_json($client->responseContent());
40$client->GET('http://localhost:3000/?word=Auto+Haus&json=1');
41my $auto_haus = decode_json($client->responseContent());
42is_deeply( [map { $_->{word} } @{$haus_auto->{collocators}}],
43 [map { $_->{word} } @{$auto_haus->{collocators}}],
44 "the syntagmatic side does not depend on the order of the operands" );
45isnt( $haus_auto->{collocators}->[0]->{max}, $haus->{collocators}->[0]->{max},
46 "and combines them rather than answering for the first" );
47
48$client->GET('http://localhost:3000/?word=Grund+Blahfasel&json=1');
49$res = decode_json($client->responseContent());
50is( $res->{unknown}, "Blahfasel", "operands outside the vocabulary are reported" );
51
Marc Kupietz3e3e3262022-04-12 23:11:45 +020052$client->GET('http://localhost:3000/getClassicCollocators?w=Grund');
53#print STDERR dump($res);
54$res = decode_json($client->responseContent());
55is( $res->{collocates}->[0]->{word}, "diesem", "primary collocate of Grund" );
56
57$client->GET('http://localhost:3000/getClassicCollocators?w=Grund');
58$res = decode_json($client->responseContent());
59is( $res->{collocates}->[0]->{word}, "diesem", "primary collocate of Grund (cached)" );
60
61$client->GET('http://localhost:3000/getCollocationAssociation?w=Grund&c=diesem');
62$res = decode_json($client->responseContent());
63is($res->{f1}, 29, "collocation association");
64
Marc Kupietzaf708c22023-11-05 11:20:20 +010065$client->GET('http://localhost:3000/getVersion');
66$res = $client->responseContent();
Marc Kupietzb36bc742023-11-05 17:46:11 +010067like($res, qr/^"\d+\.?\d*\.?\d*"$/, "version ok");
Marc Kupietzaf708c22023-11-05 11:20:20 +010068
Marc Kupietzdea505e2023-11-05 11:42:36 +010069$client->GET('http://localhost:3000/getModelName');
70$res = $client->responseContent();
Marc Kupietzb36bc742023-11-05 17:46:11 +010071is($res, '"wpd19_10000"', "model name ok");
72
Marc Kupietz043db152023-11-05 17:47:53 +010073$client->GET('http://localhost:3000/getVocabSize');
74$res = $client->responseContent();
75ok($res > 1000, "vocab size ok");
Marc Kupietzdea505e2023-11-05 11:42:36 +010076
Marc Kupietz3e3e3262022-04-12 23:11:45 +020077for (my $i=0; $i<4; $i++) {
78 $pid++;
79 print STDERR "killing PID $pid\n";
80 system("kill -9 $pid");
81}
82done_testing;