Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
222 lines (151 sloc) 10.2 KB

Exploring NLP concepts from inside a Java-based Jupyter notebook

Getting started

You will need the following from this point forward:

  • Git client 2.x or higher (an account on GitHub to fork the repo)
  • Java 11 or higher (suggest also install GraalVM CE 19.x or higher)
  • Docker CE 19.x or higher and check it is running before going further
  • Ability to run shell scripts (also see scripts in this folder) from the CLI
  • Understand reading/writing shell scripts (optional)
  • Open an account on Valohai, its FREE!
  • And thereafter create a Token from the Dashboard in order to be able to communicate with the cloud service from the command-line (using the Valohai CLI client)
    • Once your account is created and you are logged in
    • Go to the Authentication tab via your Profile page
    • Click on Manage Tokens under API Tokens
    • Click on the [Generate a new token] button
    • The token appears near the address bar of the browser (copy it quickly)
    • Save this token in an environment variable in .bash_profile or .bashrc or .zshrc
    • source the above startup script for the setting to reflect in your environment

Note: At the time of the writing version 1.9.1 of Apache OpenNLP was available.

We have put together scripts (also see scripts in this folder) to make these steps easy for everyone.

Once inside the nlp-java-jvm-example folder we will see the following files:

LICENSE.txt     <=== only this one concerns us at startup
shared               <=== created just when you run the container

Once done with the above steps, please return to the previous page/section.

Running the Jupyter notebook container

At your local machine command prompt while at the root of the project, do this:

$ export VALOHAI_TOKEN=<your generated token from one of the above steps>
### The above is optional is you have already saved it in your startup scripts and applied it to the environment

$ ./ --notebookMode --runContainer

### the above script will automatically detect the token and proceed

You will see this on startup if you have set the VALOHAI_TOKEN:

The expected environment variable VALOHAI_TOKEN has been set.

Or there is a chance you get this, with a prompt to enter your token (or paste it) if it is not set:

Enter (or paste) the Valohai token you generated from your account on _

Running container neomatrix369/nlp-java:0.2

        1.08 real         0.06 user         0.03 sys
Running container in detached mode

Once the container kicks in, you will see something like this, in the Docker container console:

Running container in detached mode

Displaying the missed log messages for container 61c9b25cb529


Set username to: jovyan
usermod: no changes
Executing the command: jupyter notebook --notebook-dir=/home/jovyan/work --NotebookApp.token=
[I 20:39:56.056 NotebookApp] The Jupyter Notebook is running at:
[I 20:39:56.056 NotebookApp] http://61c9b25cb529:8888/
[I 20:39:56.056 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-jvmci-19.3-b05-LTS)
OpenJDK 64-Bit GraalVM CE 19.3.0 (build 11.0.5+10-jvmci-19.3-b05-LTS, mixed mode, sharing)

Attaching back to container, with ID 61c9b25cb529

You can terminate your Jupyter session with a Ctrl-C

[W 17:48:44.278 NotebookApp] 404 GET /static/components/react/react-dom.production.min.js ( 2.29ms referer=http://localhost:8888/tree?

Once done with the above steps, please return to the previous page/section.

Installing Apache OpenNLP in the container

From the command-line interface

Firstly note the container id mentioned in the running Docker container console (see above), in the case of my console window:

Opening Jupyter Notebook in a browser:

Attaching back to container, with ID 1bc4ba4e01f8   <== container id embedded

Open a new console window, and then run this command to start a new session into the same container (you know this by checking with the docker ps command, there is only one container with the Jupyter notebook running):

$ docker exec -it 1bc4ba4e01f8 /bin/bash

### the container id in your case will differ in the above command

and it will give you this prompt, where you can proceed with the commands:

root@1bc4ba4e01f8:~$ cd opennlp
root@1bc4ba4e01f8:~$ ./

You will see the apache-opennlp-1.9.1-bin.tar.gz artifact being downloaded and expanded into the shared folder (in case it has not been run previously):

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 10.6M  100 10.6M    0     0  4225k      0  0:00:02  0:00:02 --:--:-- 4225k

Once done with the above steps, please return to the previous page/section.

From inside the Jupyter notebook

Once you have the Java notebook opened (or a new one created), it’s easy to do almost anything from inside it using the %system Java cell magic (thanks to the author who was behind this feature).

We suggest open the MyFirstJupyterNLPJavaNotebook.ipynb notebook and take a look at how we do things in there.

For e.g. imagine the below cell and actions performed in one of the cells:

%system ../opennlp/

Finished downloading and unpacking apache-opennlp-1.9.1-bin.tar.gz in /home/jovyan/work/apache-opennlp-1.9.1-bin

The above one-liner command in the cell results in the Apache OpenNLP installed in the expected target folder.

Once done with the above steps, please return to the previous page/section.

Viewing and accessing the shared folder

Just as you run the container, a shared folder is created (or already present from previous executions), it won’t be empty but more files and folders will appear in there as we go along.

It’s also where you will find the downloaded models and the Apache OpenNLP binary exploded into its own folder (by the name apache-opennlp-1.9.1).

You can access and see the contents of it from your local machine command-prompt (outside the container) as well:

### Open a new command prompt
$ cd nlp-java-jvm-example
$ cd images/java/opennlp
$ ls ..
Dockerfile       opennlp     shared version.txt

$ ls ../shared
apache-opennlp-1.9.1   en-ner-date.bin        en-sent.bin
en-chunker.bin         en-parser-chunking.bin langdetect-183.bin

### In your case the contents of the shared folder may vary but the way to get to the folder is above.

From inside the container this is what you see (use the docker exec command to log into the current container session, see above):

root@1bc4ba4e01f8:~$ ls   opennlp    shared     work

root@1bc4ba4e01f8:~$ ls shared
apache-opennlp-1.9.1   en-ner-time.bin        en-pos-perceptron.bin  langdetect-183.bin
en-chunker.bin         en-parser-chunking.bin en-sent.bin            notebooks
en-ner-date.bin        en-pos-maxent.bin      en-token.bin

root@1bc4ba4e01f8:~$ ls shared/notebooks

### In your case the contents of the above folders may vary but the way to get to the folder is above.

Once done with the above steps, please return to the previous page/section.


Contributions are very welcome, please share back with the wider community (and get credited for it)!

Please have a look at the CONTRIBUTING guidelines, also have a read about our licensing policy.

Back to main page (table of contents)

You can’t perform that action at this time.