Read python in processing
Question: Does anyone know how to read a python file in processing without using
processing.py or other third party library or platform? I have a python file
that can generate a text and want my processing read it real time. But it
seems like there is something wrong with the “loadStrings” stuff since my
three-line text are not generated at the same time, my third line is always
showed a little bit slower than my first two lines so the processing sketch
messed it up at some point. How to deal with the problem?

    
    
    String[] lines;
    PFont font;
    
    void setup() {
      size(800, 600);
      font = createFont("Arial", 16);
      frameRate(2);
      //lines = loadStrings("output.txt");
    }
    
    void draw() {
      background(255);
      textFont(font);
      fill(0);
      lines = loadStrings("output.txt");
      for (int i = 0; i < 3; i++) {
    
        String word = lines[i];
    
        text(word, random(width), random(height));
      }
     // noLoop();
    }
    

My python sketch:

    
    
    class MarkovGenerator(object):
    
      def __init__(self, n, max):
        self.n = n # order (length) of ngrams
        self.max = max # maximum number of elements to generate
        self.ngrams = dict() # ngrams as keys; next elements as values
        beginning = tuple(["China", "is"]) # beginning ngram of every line
        beginning2 = tuple(["But", "it"])
        self.beginnings = list()
        self.beginnings.append(beginning)
        self.beginnings.append(beginning2)
    
    
    
      def tokenize(self, text):
        return text.split(" ")
    
      def feed(self, text):
    
        tokens = self.tokenize(text)
    
        # discard this line if it's too short
        if len(tokens) < self.n:
          return
    
        # store the first ngram of this line
        #beginning = tuple(tokens[:self.n])
        #self.beginnings.append(beginning)
    
        for i in range(len(tokens) - self.n):
    
          gram = tuple(tokens[i:i+self.n])
          next = tokens[i+self.n] # get the element after the gram
    
          # if we've already seen this ngram, append; otherwise, set the
          # value for this key as a new list
          if gram in self.ngrams:
            self.ngrams[gram].append(next)
          else:
            self.ngrams[gram] = [next]
    
      # called from generate() to join together generated elements
      def concatenate(self, source):
    
          haha = list()
          kk = list()
    
          haha = " ".join(source)
          ouou = haha.split(".")
          kk = ouou[0]
    
          return kk
        # return " ".join(source)
    
      # generate a text from the information in self.ngrams
      def generate(self,i):
    
        from random import choice
    
        # get a random line beginning; convert to a list. 
          #current = choice(self.beginnings)
        current = self.beginnings[i]
        output = list(current)
    
        for i in range(self.max):
          if current in self.ngrams:
            possible_next = self.ngrams[current]
            next = choice(possible_next)
            output.append(next)
            # get the last N entries of the output; we'll use this to look up
            # an ngram in the next iteration of the loop
            current = tuple(output[-self.n:])
          else:
            break
    
        output_str = self.concatenate(output)
        return output_str
    
    
      def search_facebook_posts(self):
          import json
          import urllib
          import time
          FB = list()
          query = {'q': "feel", 'limit': 200}
          resp = urllib.urlopen('http://graph.facebook.com/search?' + urllib.urlencode(query))
          data = json.loads(resp.read())
          posts = list()
          for item in data['data']:
              if 'message' in item:
                  posts.append(item)
    
    
          for post in posts:
              FB.append(post['message'].encode('ascii', 'replace'))
    
          return FB
    
      def together(self):
          import re
          sentences = list()
          manysentences = list()
          togetherlist = self.search_facebook_posts()
          for line in togetherlist:
              line = line.replace(".", "\n")
              line = line.replace(",", "\n")
              line = line.replace("?", "\n")
              line = line.replace(";", "\n")
              line = line.replace("!", "\n")
              line = line.replace("...", "\n")
              line = line.replace(":", "\n")
              sentenca = line.split("\n")
              for i in range(len(sentenca)):
                sentences.append(sentenca[i])
    
          for sentence in sentences:
              if "feel" in sentence:
                  for matching in re.findall(r'\b[Ff]eel(.*)$',sentence):
                    manysentences.append(matching)
    
          sentencesnew = random.choice(manysentences)
          haha = "I feel" + sentencesnew
          return haha
    
    
      def namelist(self):
          import random 
          namelisty = list()
          for line in open("namelist"):
              namelisty.append(line+"said")
    
          thisname = random.choice(namelisty)
    
          return thisname 
    
    
    if __name__ == '__main__':
    
      import sys
      import random
      import codecs
    
    
      generator = MarkovGenerator(n=2, max=16)
      for line in open("china"):
        line = line.strip()
        generator.feed(line) 
    
    
      print generator.together()+"."
    


Answer: You could use java's Runtime and
[Process](http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html)
class:

    
    
      import java.io.*;
    
      void setup() {
        try {
          Process p = Runtime.getRuntime().exec("python /path/to/your/script.py arguments");
    
          BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
          String line=null;
    
          while((line=input.readLine()) != null) {
              System.out.println(line);
          }
    
          int exitWith = p.waitFor();
          System.out.println("Exited with error code "+exitWith);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    

so for example with a minimal ten.py:

    
    
    import sys
    
    if len(sys.argv) > 0:
        print sys.argv[1] * 10
    

I'd see a message printed 10 times:

    
    
      import java.io.*;
    
      void setup() {
        try {
          Process p = Runtime.getRuntime().exec("python /Users/hm/Documents/Processing/tests/CMD/ten.py hello");
    
          BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
          String line=null;
    
          while((line=input.readLine()) != null) {
              System.out.println(line);
          }
    
          int exitWith = p.waitFor();
          System.out.println("Exited with error code "+exitWith);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    

