Python Forum
Empty output for NLTK function in CGI script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Empty output for NLTK function in CGI script
#1
Hi! I use python NLTK library in CGI script to tokenize some text in XAMPP web server. If i simply use:
someamountoftext = "someamountoftext someamountoftext someamountoftext" and then nltk.word_tokenize(someamountoftext) or nltk.sent_tokenize(someamountoftext), the print(someamountoftext) It will work.
BUT! If I use this:
#!C:\Users\HP\AppData\Local\Programs\Python\Python310-32\python.exe
import os
import urllib.parse
import nltk
query_dict = urllib.parse.parse_qs(os.environ['QUERY_STRING'])

def tknz_wrd(someamountoftext):
    return(nltk.word_tokenize(someamountoftext))


print("Content-Type: text/html\n")
print (tknz_wrd)
It does not print anything on screen. No erros, just white browser screen.
But if I use (tknz_wrd(someamountoftext)) I get error, since there is no such variable(someamountoftext), but I DONT need to tokenize static text. I want to process data from WEB. That's why it CGI script!
What am I doing wrong, guys? How to make function def tknz_wrd() work? Wall

I use: Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/7.4.28
python 3.10.4
Larz60+ write Apr-27-2022, 08:42 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#2
Problem solved. So i made few mistakes, and the correct code is:

#!C:\Users\HP\AppData\Local\Programs\Python\Python310-32\python.exe

import os
import urllib.parse
import nltk

query_dict = urllib.parse.parse_qs(os.environ[‘QUERY_STRING’])
input_something = str(query_dict[‘someamountoftext’])[2: -2]
def tknz_wrd(someamountoftext):
return(nltk.word_tokenize(someamountoftext))

print(“Content-Type: text/html\n”)

print (tknz_wrd(input_something))
It processes data, recieved from browser line, like: http://localhost/speciallocation/local/tokenize/morgot.py?someamountoftext=Enter%20your%20text%20here

morgot.py - name of CGI file.

'?someamountoftext=' This is how you enter data into variable

%20 - spacebar in browser line.

Output is:
['Enter', 'your', 'text', 'here']
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020