passing data between python -> java? [solved]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tegdim
    New Member
    • Oct 2006
    • 9

    passing data between python -> java? [solved]

    Hello,
    I'm writing an application that takes pen data and tries to recognize the character and therefore predict the word you are writing. A java file will be returning text to stdout that contains the possible characters that the user just wrote.

    I would like to be able to use nltk_lite in python to find possible words based on the bigram and return these possibilities to the java GUI to allow the user to choose the correct word.

    I plan on the java and python programs to be sending data and possible next words back and forth between each other. Someone told me about pipes(?) and I couldn't find a simple tutorial online.

    All i want to do is pass simple strings between the 2 programs. If anyone could recommend a python module or other way to achieve this it would be really helpful,

    Thank you for any help :)

    ps: I'm using linux if that makes any difference
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    You might find something at
    aspn.activestat e.com/ASPN/Cookbook/Python/

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      I found recipes which use standard library modules that would work if your java GUI can be an XML-RPC server & client.

      Comment

      • kudos
        Recognized Expert New Member
        • Jul 2006
        • 127

        #4
        I supply you with the "calling python from java part". (Thats the one you need right?)

        Here is the python code (i.e. the one that should be called from the program)
        Code:
        import sys
        
        # take the first input argument
        
        mystring = sys.argv[1]
        
        # reverses the string, but you can do anything you like here..
        #
        
        print mystring[::-1]
        It simply reverses a string and output it to stdout. Now we would like to have a java program that get that info.

        Code:
        import java.io.*;
        
        class SomeJavaProgram{
        
        public static void main(String a[]) throws IOException{
        
        Process process = Runtime.getRuntime().exec("python SomePythonProgram.py hello");
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        System.out.println(reader.readLine());
        reader.close();
        
        }
        
        }
        Save the python program as "SomePythonProg ram.py" and the java program as "SomeJavaProgra m.java". Compile the java program, and run it.

        Its not really a "pretty way" to do it, but it works. I recall vaugly that there was something called Jython, that (I think) was a python interpreter written in java. Maybe you could use this to embedd python in your java programs? (I have never used Jython)

        -kudos



        Originally posted by tegdim
        Hello,
        I'm writing an application that takes pen data and tries to recognize the character and therefore predict the word you are writing. A java file will be returning text to stdout that contains the possible characters that the user just wrote.

        I would like to be able to use nltk_lite in python to find possible words based on the bigram and return these possibilities to the java GUI to allow the user to choose the correct word.

        I plan on the java and python programs to be sending data and possible next words back and forth between each other. Someone told me about pipes(?) and I couldn't find a simple tutorial online.

        All i want to do is pass simple strings between the 2 programs. If anyone could recommend a python module or other way to achieve this it would be really helpful,

        Thank you for any help :)

        ps: I'm using linux if that makes any difference

        Comment

        • tegdim
          New Member
          • Oct 2006
          • 9

          #5
          Thanks kudos,
          that's just what I was looking for :)
          Thanks again for the fast responses,
          tegdim

          Originally posted by kudos
          I supply you with the "calling python from java part". (Thats the one you need right?)

          Here is the python code (i.e. the one that should be called from the program)
          Code:
          import sys
          
          # take the first input argument
          
          mystring = sys.argv[1]
          
          # reverses the string, but you can do anything you like here..
          #
          
          print mystring[::-1]
          It simply reverses a string and output it to stdout. Now we would like to have a java program that get that info.

          Code:
          import java.io.*;
          
          class SomeJavaProgram{
          
          public static void main(String a[]) throws IOException{
          
          Process process = Runtime.getRuntime().exec("python SomePythonProgram.py hello");
          BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
          System.out.println(reader.readLine());
          reader.close();
          
          }
          
          }
          Save the python program as "SomePythonProg ram.py" and the java program as "SomeJavaProgra m.java". Compile the java program, and run it.

          Its not really a "pretty way" to do it, but it works. I recall vaugly that there was something called Jython, that (I think) was a python interpreter written in java. Maybe you could use this to embedd python in your java programs? (I have never used Jython)

          -kudos

          Comment

          Working...