[ 
https://issues.apache.org/jira/browse/NLPCRAFT-498?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17690363#comment-17690363
 ] 

Richard Skeggs commented on NLPCRAFT-498:
-----------------------------------------

 
 
## Introduction

To provide a python interface into the Scala code that makes up the NLPCraft 
source code. To achieve this connectivity there are a number of options which 
enables this connectivity.
- Jypthon - provides implementations of Python in Java, providing to Python the 
benefits of running on the JVM and access to classes written in Java.
- JPype - a Python module to provide full access to Java from within Python. It 
allows Python to make use of Java only libraries,
- Py4J - enables Python programs running in a Python interpreter to dynamically 
access Java objects in a Java Virtual Machine.

I found that JPype provided the simplest installation and interface between 
python and the Scala code. To begin with installation of JPype is like any 
other Python library, it can be installed through `pip` with the command `pip 
install jpype1`.

## Example

Below is a simple Scala class `Message` which has two methods `set_message` 
which takes a string as an input variable. The function `get_message` simply 
returns that message.

```
package test

class Message()
{
var str_msg = ""

def get_message(): String = str_msg

def set_message(msg: String)
{
str_msg = msg;
}

}
```


The Python code used to call the Scala Message class is:

````
import jpype
from jpype import *

# Launch the JVM
jpype.startJVM()

# reference the scala package test
testPkg = JPackage('test')

# instanciate the Scala class Message
t = testPkg.Message()

# using a standard java function to print a simple message
jpype.java.lang.System.out.println("HELLOE")

# set the message for the Scala function using JString to create a java string.
t.set_message(JString('Hello from scala'))

# Using Python print to display the message.
print(t.get_message())
```

The above code ran with the following local environment.

Scala version: Scala code runner version 2.11.12 -- Copyright 2002-2017, 
LAMP/EPFL
Python version: Python 3.8.10
JPype1: 1.4.1

> Python 2 scala bridge.
> ----------------------
>
>                 Key: NLPCRAFT-498
>                 URL: https://issues.apache.org/jira/browse/NLPCRAFT-498
>             Project: NLPCraft
>          Issue Type: Task
>          Components: Main API
>            Reporter: Sergey Kamov
>            Assignee: Rahul Padmanabhan
>            Priority: Major
>             Fix For: 1.0.1
>
>
> Investigation possibility to use Scala library from Python side.
> It has sense only there is some simple and elegant solution without 
> intermediate proxy servers etc
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to