Make project a Perl package

Change-Id: I9cf3c99a1b849d939ace9c9a3b265267e45ab21c
diff --git a/.gitignore b/.gitignore
index aec92ac..9ca8f68 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,5 @@
 log
 blib
-script*
 MYMETA*
 Makefile
 pm_to_blib
@@ -16,3 +15,4 @@
 npm-debug.log
 package-lock.json
 _Inline
+!.gitlab-ci.yml
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644
index 0000000..bd46e23
--- /dev/null
+++ b/Makefile.PL
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+use v5.16;
+use strict;
+use warnings;
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+    NAME           => 'derekovecs::server',
+    AUTHOR         => 'Marc Kupietz',
+    ABSTRACT       => 'Visualization of paradigmatic and syntagmatic word relations.',
+    VERSION_FROM   => 'script/derekovecs-server',
+    LICENSE        => 'apache',
+    BUILD_REQUIRES => {},
+    PREREQ_PM      => {
+        'Inline::C'                               => '0.82',
+        'Getopt::Std'                             => 0,
+        'Getopt::Long'                            => 0,
+        'Log::Any'                                => '1.701',
+        'Encode'                                  => "3.17",
+        'Mojolicious'                             => '9.23',
+        'Mojolicious::Plugin::Util::RandomString' => '0.09',
+        'Mojolicious::Plugin::CHI'                => '0.20',
+        'Mojolicious::Plugin::ClientIP'           => '0.02',
+        'Mojolicious::Plugin::AutoSecrets'        => '0.006',
+        'Mojolicious::Plugin::RequestBase'        => '0.3',
+        'Mojolicious::Plugin::RemoteAddr'         => 0,
+        'Mojolicious::Plugin::Log::Access'        => '0.041',
+        'Mojolicious::Plugin::Piwik'              => '1.00',
+        'Mojo::JSON'                              => 0,
+
+        # Required for older perl bundles
+        'List::Util' => 1.45,
+
+        # Currently on GitHub only (github.com/akron)
+        'Mojolicious::Plugin::Localize' => '0.21'
+
+    },
+    MIN_PERL_VERSION => '5.016',
+    EXE_FILES        => ['./script/derekovecs-server']
+);
diff --git a/lib/IDS/DeReKoVecs/Read.pm b/lib/IDS/DeReKoVecs/Read.pm
new file mode 100644
index 0000000..d76ddf0
--- /dev/null
+++ b/lib/IDS/DeReKoVecs/Read.pm
@@ -0,0 +1,93 @@
+use utf8;
+package IDS::DeReKoVecs::Read;
+use strict;
+use warnings;
+use Config;
+
+my $src_file      = undef;
+
+our $mergedEnd=0;
+our %cache;
+our %cccache; # classic collocator cache
+our %spcache; # similar profile cache
+our $opt_p = 5676;
+our $opt_C;
+
+BEGIN {
+  $src_file = __FILE__;
+  $src_file =~ s/Read.pm/derekovecs-server.c/;
+}
+
+use Inline C => "$src_file" => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags =>  $Config{ccflags} . "-Wall -fno-rtti -O4 -I/usr/local/kl/include", libs => "-L/usr/local/kl/lib64 -l:libcollocatordb.so.1.3.0";
+#use Inline C => Config => BUILD_NOISY => 1, CFLAGS => $Config{cflags}." -O4 -mtune k9";
+#use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -Ofast -march k8 -mtune k8 ";
+
+use Mojo::JSON qw(decode_json encode_json to_json);
+use Exporter qw(import);
+
+our @EXPORT = qw(init_net load_sprofiles getCollocationAssociation getClassicCollocatorsCached getClassicCollocatorsCached getBiggestMergedDifferences filter_garbage get_neighbours);
+
+
+sub getCollocationAssociation {
+  my ($c, $word, $collocate) = @_;
+  return getCollocationScores($word, $collocate)
+}
+
+sub getClassicCollocatorsCached {
+  my ($c, $word) = @_;
+  my $s2 = "";
+  if($word > $mergedEnd) {
+    $word-=$mergedEnd;
+  }
+
+  if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
+      open PIPE, "GET http://corpora.ids-mannheim.de/openlab/derekovecs/getClassicCollocators?w=$word  |";
+  }
+  if($opt_C || !$cccache{$word}) {
+    $c->app->log->info("Getting classic collocates of $word.");
+    $cccache{$word} = getClassicCollocators($word);
+    $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g;
+  } else {
+    $c->app->log->info("Getting classic collocates for $word from cache.");
+  }
+  if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
+    while(<PIPE>) {
+      $s2 .= $_;
+    }
+    close(PIPE);
+  }
+
+  if(length($s2) > 2000) {
+    my $d1 = decode_json($cccache{$word});
+    my $d2 = decode_json($s2);
+    my %d2ld;
+    my $minLd = 14;
+    foreach my $i (@{$d2->{collocates}}) {
+      $d2ld{$i->{word}}=$i->{ld};
+      $minLd=$i->{ld} if($i->{ld} < $minLd);
+    }
+    foreach my $i (@{$d1->{collocates}}) {
+      my $w = $i->{word};
+      $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1);
+    }
+    return(encode_json($d1));
+  } else {
+    my $d1 = decode_json($cccache{$word});
+    foreach my $i (@{$d1->{collocates}}) {
+      $i->{delta} = 0;
+    }
+    return(encode_json($d1));
+  }
+}
+
+sub getSimilarProfilesCached {
+  my ($c, $word) = @_;
+  if(!$spcache{$word}) {
+    $spcache{$word} = getSimilarProfiles($word);
+  } else {
+    $c->app->log->info("Getting similar profiles for $word from cache:");
+  }
+  return $spcache{$word};
+}
+
+return 1;
diff --git a/derekovecs-server.c b/lib/IDS/DeReKoVecs/derekovecs-server.c
similarity index 100%
rename from derekovecs-server.c
rename to lib/IDS/DeReKoVecs/derekovecs-server.c
diff --git a/derekovecs-server.pl b/script/derekovecs-server
similarity index 81%
rename from derekovecs-server.pl
rename to script/derekovecs-server
index 7df39d0..8f71355 100755
--- a/derekovecs-server.pl
+++ b/script/derekovecs-server
@@ -1,7 +1,7 @@
 #!/usr/local/bin/perl
-use Inline C => "./derekovecs-server.c" => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags =>  $Config{ccflags} . "-Wall -fno-rtti -O4 -I/usr/local/kl/include", libs => "-L/usr/local/kl/lib64 -l:libcollocatordb.so.1.3.0";
-#use Inline C => Config => BUILD_NOISY => 1, CFLAGS => $Config{cflags}." -O4 -mtune k9";
-#use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -Ofast -march k8 -mtune k8 ";
+our $VERSION = '0.90';
+
+use IDS::DeReKoVecs::Read qw(init_net load_sprofiles getCollocationAssociation getClassicCollocatorsCached getClassicCollocatorsCached getBiggestMergedDifferences filter_garbage get_neighbours);
 use Mojolicious::Lite;
 use Mojo::JSON qw(decode_json encode_json to_json);
 use base 'Mojolicious::Plugin';
@@ -11,7 +11,7 @@
 #use Mojo::Server::Daemon;
 use Cwd;
 
-my $mojo_config = $ENV{MOJO_CONFIG} // 'derekovecs-server.conf';
+my $mojo_config = $ENV{MOJO_CONFIG} // '../derekovecs-server.conf';
 plugin Config => {file => $mojo_config};
 
 my $DEFAULT_VECS = app->config->{w2v}->{vecs} // "../models/dereko-2021-i.vecs";
@@ -40,26 +40,27 @@
   dict => {
     _  => sub { $_->locale },
   },
-  resources => ['derekovecs-server.dict']
+  resources => ['../derekovecs-server.dict']
 };
 our $opt_i = 0; # latin1-input?
 our $opt_l = undef;
-our $opt_p = 5676;
 our $opt_m;
 our $opt_M;
 our $opt_n = $DEFAULT_NET;
 our $opt_d;
 our $opt_D;
 our $opt_G = 1;
+
+our $mergedEnd=0;
+our %cache;
+our %cccache; # classic collocator cache
+our %spcache; # similar profile cache
+our $opt_p = 5676;
 our $opt_C;
 
 my %marked;
 my $title="";
 my $training_args="";
-my $mergedEnd=0;
-my %cache;
-my %cccache; # classic collocator cache
-my %spcache; # similar profile cache
 
 getopts('d:D:Gil:p:m:n:M:C');
 
@@ -148,67 +149,6 @@
   }
 }
 
-sub getCollocationAssociation {
-  my ($c, $word, $collocate) = @_;
-  return getCollocationScores($word, $collocate)
-}
-
-sub getClassicCollocatorsCached {
-  my ($c, $word) = @_;
-  my $s2 = "";
-  if($word > $mergedEnd) {
-    $word-=$mergedEnd;
-  }
-
-  if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
-      open PIPE, "GET http://corpora.ids-mannheim.de/openlab/derekovecs/getClassicCollocators?w=$word  |";
-  }
-  if($opt_C || !$cccache{$word}) {
-    $c->app->log->info("Getting classic collocates of $word.");
-    $cccache{$word} = getClassicCollocators($word);
-    $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g;
-  } else {
-    $c->app->log->info("Getting classic collocates for $word from cache.");
-  }
-  if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
-    while(<PIPE>) {
-      $s2 .= $_;
-    }
-    close(PIPE);
-  }
-
-  if(length($s2) > 2000) {
-    my $d1 = decode_json($cccache{$word});
-    my $d2 = decode_json($s2);
-    my %d2ld;
-    my $minLd = 14;
-    foreach my $i (@{$d2->{collocates}}) {
-      $d2ld{$i->{word}}=$i->{ld};
-      $minLd=$i->{ld} if($i->{ld} < $minLd);
-    }
-    foreach my $i (@{$d1->{collocates}}) {
-      my $w = $i->{word};
-      $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1);
-    }
-    return(encode_json($d1));
-  } else {
-    my $d1 = decode_json($cccache{$word});
-    foreach my $i (@{$d1->{collocates}}) {
-      $i->{delta} = 0;
-    }
-    return(encode_json($d1));
-  }
-}
-
-sub getSimilarProfilesCached {
-  my ($c, $word) = @_;
-  if(!$spcache{$word}) {
-    $spcache{$word} = getSimilarProfiles($word);
-  } else {
-    $c->app->log->info("Getting similar profiles for $word from cache:");
-  }
-  return $spcache{$word};
-}
 
 post '/derekovecs/getVecsByRanks' => sub {
   my $self = shift;
@@ -432,6 +372,7 @@
       }
 );
 
+app->renderer->paths([app->home->rel_file('../templates')]);
 app->start;
 #$daemon->run;
 # app->start;