From 3528a81816f3293fd86002758bf5744595ca213b Mon Sep 17 00:00:00 2001 From: Jakob Moser <moser@cl.uni-heidelberg.de> Date: Wed, 13 Apr 2022 11:22:09 +0200 Subject: [PATCH] Receive input via base64 encoded JSON Drop reading grammar from input, because we'd never use it anyway --- Dockerfile | 1 + README.md | 20 ++++++++++++++------ entrypoint.sh | 15 +++++++++------ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 23f9402..bfbf785 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ WORKDIR /workdir # Install dependencies RUN apt-get update && apt-get install -y \ ca-certificates \ + jq \ xvfb \ && rm -rf /var/lib/apt/lists/* diff --git a/README.md b/README.md index faa5818..b056de2 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,9 @@ To parse a sentence using xle-docker, you need two things: You can then execute the following command: ```bash -sudo docker run -i xle "in der Arena veranstalten die Ritter ein Turnier ." < /home/mustermann/grammatik.lfg +sudo docker run \ + --mount type=bind,source=/home/mustermann/grammatik.lfg,target=/grammar.lfg \ + xle "in der Arena veranstalten die Ritter ein Turnier ." ``` This will take a few seconds. The parses will then be output to the command line using XLE's own XML export format. If you want to store the output in a file, append `> name_of_your_file.xml` at the end of the command. @@ -38,15 +40,21 @@ Note that the `xle` specified in the `docker run` command is the name of the tag </details> <details> -<summary>Alternative using mounts</summary> +<summary>Alternative using a Base64-encoded JSON</summary> + +If you don't want to use mounts, you can also create a JSON file that contains the attributes `sentence` and `grammar`, like this (shortened version, we just want to illustrate how to deal with newlines): + +```json +{"sentence":"in der Arena veranstalten die Ritter ein Turnier", "grammar":"DEMO GERMAN CONFIG (1.0)\n ROOTCAT IP.\n LEXENTRIES (DEMO GERMAN).\n RULES (DEMO GERMAN).\n GOVERNABLERELATIONS SUBJ OBJ OBJ-TH OBL-TH.\n SEMANTICFUNCTIONS ADJ.\n NONDISTRIBUTIVES NUM PERS GEND.\n----\n\nDEMO GERMAN RULES (1.0)\n\nIP --> NP: (^ SUBJ) = !;\n I'\n (PUNCT).\n\nNP --> { Det N | NP PP: ! $ (^ ADJ); }.\n\nI' --> I VP.\n\n..."} +``` -The container also allows the Grammar file to be mounted as `/grammar.lfg`, if this is more useful for your specific use case: +Store the file as `data.json`. You can then run the container like this: ```bash -sudo docker run \ - --mount type=bind,source=/home/mustermann/grammatik.lfg,target=/grammar.lfg \ - xle "in der Arena veranstalten die Ritter ein Turnier ." +sudo docker container run xle $(cat data.json | base64 -w0) ``` + +The idea to implement it that way came from [Christian Heusel](https://christian.heusel.eu/), thank you very much! </details> ## :page_with_curl: About diff --git a/entrypoint.sh b/entrypoint.sh index 6c678cb..f672076 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,19 +2,22 @@ # # Entrypoint for the xle Docker image. # -# You need to provide the following information: -# 1. A sentence to parse as first (and only) command line argument ($1) -# 2. A grammar to parse with. The grammar is searched first at /grammar.lfg. If no such file exists, the grammar is read from stdin. +# XLE needs a sentence and a grammar. You can use either of the following two ways to provide them to an xle container: +# 1) Mount grammar as /grammar.lfg, pass sentence as first and only command line argument ($1) +# 2) Pass a base64-encoded JSON containing the attributes "sentence" and "grammar" as first and only command line argument ($1) if [[ ! -f /grammar.lfg ]] then - # Use "cat" to read everything stdin has to offer and then use Bash to write it to a file - cat > /grammar.lfg + JSON=$(echo $1 | base64 -d) + SENTENCE=$(echo $JSON | jq -r .sentence) + echo $JSON | jq -r .grammar > /grammar.lfg +else + SENTENCE=$1 fi # Write xlerc containing instructions to parse echo "create-parser /grammar.lfg" > xlerc -echo "packed-xml-fs { $1 } packed-parses.xml" >> xlerc +echo "packed-xml-fs { $SENTENCE } packed-parses.xml" >> xlerc echo "exit" >> xlerc # Start XLE (under the X server), this will automatically load xlerc, let it complete the parse and end -- GitLab