diff --git a/Dockerfile b/Dockerfile index 23f9402c2f5cead68bb0f4505193412933e7e1cf..bfbf785bbc2fe461b2e3a3d7c0846d4f500c1688 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 faa581897b313988a01232a1df788cb427a379ed..b056de2ad5b18bad24040e77b666948a86db5c2b 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 6c678cb36668e8aba2468d54a8688ca17ed85275..f67207681c07c7b1f68454c399059eae1746a54a 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