0

I am using python's nltk to do some interesting things to input strings from php, but I'm experiencing some difficulties in getting it to output to the browser. My code looks like this:

system("python test.py command line arguments");

which outputs correctly in the command line no matter what is in my test.py file. However, in the browser it will work unless specific code is present.

inside test.py:

import string
import sys
from nltk import word_tokenize as wt
from nltk.corpus import wordnet as wn

x = ""
for item in sys.argv: x += item+" "

i = wt(x)
result = ""
wn.morphy(i[0])
print "hello world"

This will not print "hello world" in the browser, but only if the preceding line of code is present. without

wn.morphy

it will run. Unfortunately I need to use wn.morphy. Why would this not work in the browser with that specific line of code in my test.py file? I have already turned off safe mode, which is what allows me to output to the browser with python at all

2
  • Can you show a complete PHP script which exhibits this problem? Jan 23, 2012 at 3:09
  • that is literally the entire php script that exhibits the problem. Running both of those on a server with php and python with the nltk should replicate this problem Jan 23, 2012 at 3:21

2 Answers 2

0

Actually, this code works for me on the PHP CLI, whether or not I include the wn.morphy line, so I think your problem is elsewhere, or version-related.

Did you remember to download the 'wordnet' corpus? If not, run

>>> import nltk
>>> nltk.download('wordnet')

in Python, and then try again. I could see the script erroring out because it can't get to the corpus.

1
  • I did download the wordnet corpus, that's why it works in the console -- it definitely errors in the python but I can't imagine why it would only do so in the browser. I checked the phpdoc and both are using the same ini. Jan 23, 2012 at 18:27
0

What is the error? Could it be due to the size of WordNet (which is pretty big) or the time it takes Python/NLTK takes to load it? A live web server is probably going to have tighter quotas than an interactive console session.

1
  • I don't know what the error could be. Importing wordnet works, but as soon as I use a function in it, it does not. It's possible that wordnet is too big to load with the browser itself just due to its different limitations, but the server itself is the same, as the command line I'm using is ssh'd into it. Jan 23, 2012 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.