Skip to content

Commit 121fdfe

Browse files
committed
Add the API to harvest each language resource
1 parent 4f160de commit 121fdfe

File tree

9 files changed

+187
-66
lines changed

9 files changed

+187
-66
lines changed

src/main/js/frontend/package-lock.json

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<logs></logs>

src/main/xar-resources/modules/load-languages.xq

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
xquery version "3.1";
22

3-
module namespace ll = "http://exist-db.org/xquery/stanford-nlp/load-language";
3+
import module namespace config = "http://exist-db.org/apps/stanford-nlp/config";
44

55
import module namespace http = "http://expath.org/ns/http-client";
66
import module namespace compression = "http://exist-db.org/xquery/compression";
7-
import module namespace console = "http://exist-db.org/xquery/console";
87
import module namespace functx = "http://www.functx.com";
98
import module namespace util = "http://exist-db.org/xquery/util";
109
import module namespace map = "http://www.w3.org/2005/xpath-functions/map";
1110
import module namespace xmldb = "http://exist-db.org/xquery/xmldb";
1211

13-
declare function ll:mkcol-recursive($collection, $components) {
12+
13+
declare variable $local:language external;
14+
15+
16+
declare function local:mkcol-recursive($collection, $components) {
1417
if (exists($components)) then
1518
let $newColl := concat($collection, "/", $components[1])
1619
return (
17-
if (xmldb:collection-available($newColl))
18-
then ()
20+
if (xmldb:collection-available($newColl))
21+
then ()
1922
else xmldb:create-collection($collection, $components[1]),
20-
ll:mkcol-recursive($newColl, subsequence($components, 2))
23+
local:mkcol-recursive($newColl, subsequence($components, 2))
2124
)
2225
else
2326
()
2427
};
2528

26-
declare function ll:mkcol($collection, $path) {
27-
ll:mkcol-recursive($collection, tokenize($path, "/"))
29+
declare function local:mkcol($collection, $path) {
30+
local:mkcol-recursive($collection, tokenize($path, "/"))
2831
};
2932

3033

31-
declare function ll:entry-data($path as xs:anyURI, $type as xs:string, $data as item()?, $param as item()*) as item()?
34+
declare function local:entry-data($path as xs:anyURI, $type as xs:string, $data as item()?, $param as item()*) as item()?
3235
{
3336
let $path-before := functx:substring-before-last($path, "/")
3437
let $resource-name := functx:substring-after-last($path, "/")
35-
let $coll := ll:mkcol("/db/apps/stanford-nlp/data", $path-before)
38+
let $coll := local:mkcol("/db/apps/stanford-nlp/data", $path-before)
3639
let $decided :=
37-
if (fn:ends-with($resource-name, ".properties"))
38-
then
40+
if (fn:ends-with($resource-name, ".properties"))
41+
then
3942
let $nl := "&#10;"
4043
let $lines := fn:tokenize(util:binary-to-string($data, "UTF-8"), $nl)
4144
let $content := map:merge(
@@ -62,34 +65,51 @@ declare function ll:entry-data($path as xs:anyURI, $type as xs:string, $data as
6265
else ()
6366
)
6467
return xmldb:store(
65-
"/db/apps/stanford-nlp/data/" || $path-before,
66-
fn:replace($resource-name, ".properties", ".json"),
68+
"/db/apps/stanford-nlp/data/" || $path-before,
69+
fn:replace($resource-name, ".properties", ".json"),
6770
fn:serialize($content, map { "method": "json", "indent": true() })
68-
)
71+
)
6972
else ()
7073
let $stored := xmldb:store-as-binary("/db/apps/stanford-nlp/data/" || $path-before, $resource-name, $data)
71-
let $log := console:log("item path=[" || $path || "] type=[" || $type || "]")
74+
let $log := local:log("item path=[" || $path || "] type=[" || $type || "]")
7275
return ()
73-
};
76+
};
7477

75-
declare function ll:entry-filter($path as xs:anyURI, $type as xs:string, $param as item()*) as xs:boolean
78+
declare function local:entry-filter($path as xs:anyURI, $type as xs:string, $param as item()*) as xs:boolean
7679
{
7780
$type = "resource"
7881
};
7982

80-
declare function ll:process($path as xs:string) {
81-
let $log0 := console:log($path)
82-
83+
declare function local:log($message as xs:string)
84+
{
85+
update insert
86+
element { 'log' } {
87+
attribute { 'timestamp' } { util:system-dateTime() },
88+
attribute { 'language' } { $local:language },
89+
$message
90+
}
91+
into
92+
fn:doc("/db/apps/stanford-nlp/data/log.xml")//logs
93+
};
94+
95+
declare function local:process($path as xs:string) {
96+
let $log0 := local:log($path)
97+
8398
let $req := <http:request href="{$path}" method="get"/>
84-
99+
85100
let $zip := http:send-request($req)[2]
86-
let $log := console:log(functx:atomic-type($zip))
87-
101+
let $log := local:log(functx:atomic-type($zip))
102+
88103
return compression:unzip(
89-
$zip,
90-
util:function(xs:QName("ll:entry-filter"), 3),
91-
(),
92-
util:function(xs:QName("ll:entry-data"), 4),
104+
$zip,
105+
util:function(xs:QName("local:entry-filter"), 3),
106+
(),
107+
util:function(xs:QName("local:entry-data"), 4),
93108
()
94109
)
95110
};
111+
112+
(local:log("start"),
113+
local:process($config:corenlp-model-url || $local:language || ".jar"),
114+
local:log("end")
115+
)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
xquery version "3.1";
2+
3+
module namespace api = "http://exist-db.org/xquery/stanford-nlp/api";
4+
import module namespace scheduler = "http://exist-db.org/xquery/scheduler";
5+
import module namespace map = "http://www.w3.org/2005/xpath-functions/map";
6+
7+
declare namespace rest = "http://exquery.org/ns/restxq";
8+
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
9+
10+
11+
declare function api:schedule-language($language as xs:string*) as map(*)
12+
{
13+
let $a :=
14+
scheduler:schedule-xquery-periodic-job(
15+
"/db/apps/stanford-nlp/modules/load.xq",
16+
500,
17+
"nlp-load-" || $language || "-" || util:uuid(),
18+
<parameters>
19+
<param name="language" value="{$language}" />
20+
</parameters>,
21+
1000,
22+
0
23+
)
24+
return
25+
map {
26+
"language": $language,
27+
"status": $a
28+
}
29+
};
30+
31+
(:~
32+
Start the loading of a language resource through a background process
33+
@param $language The language to be loaded
34+
@author Loren Cahlander
35+
@version 1.0
36+
@since 1.0
37+
@custom:openapi-tag Natural Language Processing
38+
:)
39+
declare
40+
%rest:GET
41+
%rest:path("/stanford/nlp/load/{$language}")
42+
%rest:produces("application/json")
43+
%output:media-type("application/json")
44+
%output:method("json")
45+
function api:load-language(
46+
$language as xs:string*
47+
) as map(*)
48+
{
49+
switch ($language)
50+
case "arabic"
51+
return api:schedule-language($language)
52+
53+
case "chinese"
54+
return api:schedule-language($language)
55+
56+
case "english"
57+
return api:schedule-language($language)
58+
59+
case "english-kbp"
60+
return api:schedule-language($language)
61+
62+
case "french"
63+
return api:schedule-language($language)
64+
65+
case "german"
66+
return api:schedule-language($language)
67+
68+
case "spanish"
69+
return api:schedule-language($language)
70+
71+
default
72+
return
73+
map {
74+
"status": fn:false(),
75+
"languages": array {('arabic', 'chinese', 'english', 'english-kbp', 'french', 'german', 'spanish')}
76+
}
77+
};
78+
79+
(:~
80+
Start the loading of a language resource through a background process
81+
@author Loren Cahlander
82+
@version 1.0
83+
@since 1.0
84+
@custom:openapi-tag Natural Language Processing
85+
:)
86+
declare
87+
%rest:GET
88+
%rest:path("/stanford/nlp/logs")
89+
%rest:produces("application/json")
90+
%output:media-type("application/json")
91+
%output:method("json")
92+
function api:logs(
93+
) as array(*)
94+
{
95+
array {
96+
for $log in fn:doc("/db/apps/stanford-nlp/data/log.xml")//logs/log
97+
let $timestamp := xs:string($log/@timestamp)
98+
let $language := xs:string($log/@language)
99+
let $text := $log/text()
100+
order by $timestamp ascending
101+
return
102+
map {
103+
'timestamp': $timestamp,
104+
'language': $language,
105+
'message': $text
106+
}
107+
}
108+
};

src/main/xar-resources/modules/schedule-language.xq

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/xar-resources/modules/schedule-load.xq

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/xar-resources/post-install.xq

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,27 @@ declare variable $dir external;
2222
(: the target collection into which the app is deployed :)
2323
declare variable $target external;
2424

25+
let $modules := (
26+
"load.xq",
27+
()
28+
)
29+
30+
let $a := (
31+
for $module in $modules
32+
return
33+
(
34+
sm:chown(xs:anyURI($target || "/modules/" || $module), "nlp:nlp"),
35+
sm:chmod(xs:anyURI($target || "/modules/" || $module), "r-sr-xr-x"),
36+
()
37+
),
38+
sm:chown(xs:anyURI($target || "/modules/rest-api.xqm"), "admin:dba"),
39+
sm:chmod(xs:anyURI($target || "/modules/rest-api.xqm"), "r-sr-xr-x"),
40+
()
41+
)
42+
2543
(:
2644
collection configuration was copied to the system config collection by pre-install.xq
2745
so we can now remove it from the app colllection
2846
:)
29-
xmldb:remove($target, "collection.xconf")
47+
return
48+
xmldb:remove($target, "collection.xconf")

xar-assembly.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
<prepare>pre-install.xq</prepare>
2626
<finish>post-install.xq</finish>
27+
<permissions user="nlp" password="nlp" group="nlp" mode="rw-rw-r--"/>
2728

2829
<!-- includes everything in src/main/xar-resources, README.md, and LICENSE -->
2930
<fileSets>

0 commit comments

Comments
 (0)