4

I need to use the Java-based OpenNLP library in my PHP code. For example, I need to use its Sentence Detector component (en-sent.bin) for analysing text variables in my PHP code.

In its documentation, that API can be accessed from a Java code as follows:

InputStream modelIn = new FileInputStream("en-sent.bin");

try {
  SentenceModel model = new SentenceModel(modelIn);
}
catch (IOException e) {
  e.printStackTrace();
}
finally {
  if (modelIn != null) {
    try {
      modelIn.close();
    }
    catch (IOException e) {
    }
  }
}

How can do the same thing in PHP?

In other words, what is the PHP-equivalent to the above Java code?

nicael
  • 18,550
  • 13
  • 57
  • 90
Orion
  • 1,104
  • 3
  • 16
  • 40

2 Answers2

2

The solution I'm about to implement is writing a java based backend (because just the OpenNLP tool's output just won't cut it) and using PHP's execute function to run it. If you want it to be even faster, I suggest making it a daemon, making PHP connect to it and send it data to return (it'd probably be a unix socket). Although it's kinda complex, it seems less error prone than a PHP/Java Bridge.

Osmium USA
  • 1,751
  • 19
  • 37
1

There would have to be a PHP API for accessing OpenNLP. A quick search doesn't show anything. The only other thing I can think of is using a PHP/Java Bridge of some sort, but that's more involved. See http://php-java-bridge.sourceforge.net/pjb/, for example.

ScoPi
  • 1,193
  • 9
  • 14