python wsgi pymorphy2 error iternal - python

trying to return value of pymorphy2 using apache with wsgi module & getting error 500
log says TypeError: sequence of byte string values expected, value of type Parse found
i dont know what to do! in Python im rookie
my python code is
import pymorphy2
import cgi
morph = pymorphy2.MorphAnalyzer()
morphid = morph.parse(u'конь')
def app(environ, start_response):
words = morphid
start_response('200 OK', [('Content-Type', 'text/html')])
return [words]
but in shell it works... :(
please help i dont understand what is form or type of var words need to me.
or may be that is all wrong
in shell result is
morph = pymorphy2.MorphAnalyzer()
words = morph.parse(u'конь')
print "words"
[Parse(word=u'\xf1\xf2\xe0\xeb\xe8', tag=OpencorporaTag('LATN'), normal_form=u'\xf1\xf2\xe0\xeb\xe8', score=1.0, methods_stack=((<LatinAnalyzer>, u'\xf1\xf2\xe0\xeb\xe8'),))]
Thanks to everyone!

As you can see, you need to return sequence of byte string but you are returning word which is of type Parse. if you want the response to be exactly the same as the thing you get from the console, try
words = str(morphid[0]).encode()
It will return the string of the result which is encoded() thus can be used as a response.

Related

Python: Using SSML with SAPI (comtypes)

TL;DR: I'm trying to pass an XML object (using ET) to a Comtypes (SAPI) object in python 3.7.2 on Windows 10. It's failing due to invalid chars (see error below). Unicode characters are read correctly from the file, can be printed (but do not display correctly on the console). It seems like the XML is being passed as ASCII or that I'm missing a flag? (https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ee431843(v%3Dvs.85)). If it is a missing flag, how do I pass it? (I haven't figured that part out yet..)
Long form description
I'm using Python 3.7.2 on Windows 10 and trying to send create an XML (SSML: https://www.w3.org/TR/speech-synthesis/) file to use with Microsoft's speech API. The voice struggles with certain words and when I looked at the SSML format and it supports a phoneme tag, which allows you to specify how to pronounce a given word. Microsoft implements parts of the standard (https://learn.microsoft.com/en-us/cortana/skills/speech-synthesis-markup-language#phoneme-element) so I found a UTF-8 encoded library containing IPA pronunciations. When I try to call the SAPI, with parts of the code replaced I get the following error:
Traceback (most recent call last):
File "pdf_to_speech.py", line 132, in <module>
audioConverter(text = "Hello world extended test",outputFile = output_file)
File "pdf_to_speech.py", line 88, in __call__
self.engine.speak(text)
_ctypes.COMError: (-2147200902, None, ("'ph' attribute in 'phoneme' element is not valid.", None, None, 0, None))
I've been trying to debug, but when I print the pronunciations of the words the characters are boxes. However if I copy and paste them from my console, they look fine (see below).
həˈloʊ,
ˈwɝːld
ɪkˈstɛndəd,
ˈtɛst
Best Guess
I'm unsure whether the problem is caused by
1) I've changed versions of pythons to be able to print unicode
2) I fixed problems with reading the file
3) I had incorrect manipulations of the string
I'm pretty sure the problem is that I'm not passing it as a unicode to the comtype object. The ideas I'm looking into are
1) Is there a flag missing?
2) Is it being converted to ascii when its being passed to comtypes (C types error)?
3) Is the XML being passed incorrectly/ am I missing a step?
Sneak peek at the code
This is the class that reads the IPA dictionary and then generates the XML file. Look at _load_phonemes and _pronounce.
class SSML_Generator:
def __init__(self,pause,phonemeFile):
self.pause = pause
if isinstance(phonemeFile,str):
print("Loading dictionary")
self.phonemeDict = self._load_phonemes(phonemeFile)
print(len(self.phonemeDict))
else:
self.phonemeDict = {}
def _load_phonemes(self, phonemeFile):
phonemeDict = {}
with io.open(phonemeFile, 'r',encoding='utf-8') as f:
for line in f:
tok = line.split()
#print(len(tok))
phonemeDict[tok[0].lower()] = tok[1].lower()
return phonemeDict
def __call__(self,text):
SSML_document = self._header()
for utterance in text:
parent_tag = self._pronounce(utterance,SSML_document)
#parent_tag.tail = self._pause(parent_tag)
SSML_document.append(parent_tag)
ET.dump(SSML_document)
return SSML_document
def _pause(self,parent_tag):
return ET.fromstring("<break time=\"150ms\" />") # ET.SubElement(parent_tag,"break",{"time":str(self.pause)+"ms"})
def _header(self):
return ET.Element("speak",{"version":"1.0", "xmlns":"http://www.w3.org/2001/10/synthesis", "xml:lang":"en-US"})
# TODO: Add rate https://learn.microsoft.com/en-us/cortana/skills/speech-synthesis-markup-language#prosody-element
def _rate(self):
pass
# TODO: Add pitch
def _pitch(self):
pass
def _pronounce(self,word,parent_tag):
if word in self.phonemeDict:
sys.stdout.buffer.write(self.phonemeDict[word].encode("utf-8"))
return ET.fromstring("<phoneme alphabet=\"ipa\" ph=\"" + self.phonemeDict[word] + "\"> </phoneme>")#ET.SubElement(parent_tag,"phoneme",{"alphabet":"ipa","ph":self.phonemeDict[word]})#<phoneme alphabet="string" ph="string"></phoneme>
else:
return parent_tag
# Nice to have: Transform acronyms into their pronunciation (See say as tag)
I've also added how the code writes to the comtype object (SAPI) in case the error is there.
def __call__(self,text,outputFile):
# https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms723606(v%3Dvs.85)
self.stream.Open(outputFile + ".wav", self.SpeechLib.SSFMCreateForWrite)
self.engine.AudioOutputStream = self.stream
text = self._text_processing(text)
text = self.SSML_generator(text)
text = ET.tostring(text,encoding='utf8', method='xml').decode('utf-8')
self.engine.speak(text)
self.stream.Close()
Thanks in advance for your help!
Try to use single quotes inside ph attrubute.
Like this
my_text = '<speak><phoneme alphabet="x-sampa" ph=\'v"e.de.ni.e\'>ведение</phoneme></speak>'
also remember to use \ to escape single quote
UPD
Also this error could mean that your ph cannot be parsed. You can check docs there: https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/speech-synthesis-markup
this example will work
<speak version="1.0" xmlns="https://www.w3.org/2001/10/synthesis" xml:lang="en-US">
<voice name="en-US-Jessa24kRUS">
<s>His name is Mike <phoneme alphabet="ups" ph="JH AU"> Zhou </phoneme></s>
</voice>
</speak>
but this doesn't
<speak version="1.0" xmlns="https://www.w3.org/2001/10/synthesis" xml:lang="en-US">
<voice name="en-US-Jessa24kRUS">
<s>His name is Mike <phoneme alphabet="ups" ph="JHU AUA"> Zhou </phoneme></s>
</voice>
</speak>

Getting an internal server error on flask web server

I'm newbie for raspberry pi and python coding. I'm working on a school project. I've already looked for some tutorials and examples but maybe I'm missing something. I want to build a web server based gpio controller. I'm using flask for this. For going into this, I've started with this example. Just turning on and off the led by refreshing the page.
So the problem is, I can't see the response value on the web server side. It's turning on and off the led. But I want to see the situation online. But I just couldn't. I'm getting and internal server error. I'm giving the python and html codes. Can you help me with solving the problem.
from flask import Flask
from flask import render_template
import RPi.GPIO as GPIO
app=Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.output(4,1)
status=GPIO.HIGH
#app.route('/')
def readPin():
global status
global response
try:
if status==GPIO.LOW:
status=GPIO.HIGH
print('ON')
response="Pin is high"
else:
status=GPIO.LOW
print('OFF')
response="Pin is low"
except:
response="Error reading pin"
GPIO.output(4, status)
templateData= {
'title' : 'Status of Pin' + status,
'response' : response
}
return render_template('pin.html', **templateData)
if __name__=="__main__":
app.run('192.168.2.5')
And basically just this line is on my html page.
<h1>{{response}}</h1>
I think "response" doesn't get a value. What's wrong on this?
Firstly it helps to run it in debug mode:
app.run(debug=True)
This will help you track down any errors which are being suppressed.
Next have a look at the line where you are building the title string:
'title' : 'Status of Pin' + status
If you enable the debug mode, then you should see something saying that an int/bool can't be converted to str implicitly. (Python doesn't know how to add a string and an int/bool).
In order to fix this, you should explicitly cast status to a string:
'title' : 'Status of Pin' + str(status)
Or better yet:
'title' : 'Status of Pin: {}'.format(status)
Your server was probably throwing an exception when trying to create your dictionary, therefore the templateData value was being sent as an empty value.
Notice in this example, the TypeError which is thrown when trying to concatenate 2 variables of different type.
Hence, wrapping your variable in the str(status) will cast the status variable to it's string repersentation before attempting to combine the variables.
[root#cloud-ms-1 alan]# cat add.py
a = 'one'
b = 2
print a + b
[root#cloud-ms-1 alan]# python add.py
Traceback (most recent call last):
File "add.py", line 6, in <module>
print a + b
TypeError: cannot concatenate 'str' and 'int' objects
[root#cloud-ms-1 alan]# cat add.py
a = 'one'
b = str(2)
print a + b
[root#cloud-ms-1 alan]# python add.py
one2

CGI with Python

I'm beginning to use CGI with Python.
After running the following piece of code:
#!c:\python34\python.exe
import cgi
print("Content-type: text/html\n\n") #important
def getData():
formData = cgi.FieldStorage()
InputUN = formData.getvalue('username')
InputPC = formData.getvalue('passcode')
TF = open("TempFile.txt", "w")
TF.write(InputUN)
TF.write(InputPC)
TF.close()
if __name__ =="__main__":
LoginInput = getData()
print("cgi worked")
The following error occurs:
Traceback (most recent call last):
File "C:\xampp\htdocs\actual\loginvalues.cgi", line 21, in <module>
LoginInput = getData()
File "C:\xampp\htdocs\actual\loginvalues.cgi", line 16, in getData
TF.write(InputUN)
TypeError: must be str, not None
>>>
I'm trying to write the values, inputted in html, to a text file.
Any help would be appreciated :)
Your calls to getValue() are returning None, meaning the form either didn't contain them, had them set to an empty string, or had them set by name only. Python's CGI module ignores inputs that aren't set to a non-null string.
Works for Python CGI:
mysite.com/loginvalues.cgi?username=myname&pass=mypass
Doesn't work for Python CGI:
mysite.com/loginvalues.cgi?username=&pass= (null value(s))
mysite.com/loginvalues.cgi?username&pass (Python requires the = part.)
To account for this, introduce a default value for when a form element is missing, or handle the None case manually:
TF.write('anonymous' if InputUN is None else InputUN)
TF.write('password' if InputPC is None else InputUN)
As a note, passwords and other private login credentials should never be used in a URL. URLs are not encrypted. Even in HTTPS, the URL is sent in plain text that anyone on the network(s) between you and your users can read.
The only time a URL is ever encrypted is over a tunneled SSH port or an encrypted VPN, but you can't control that, so never bank on it.

Error running basic python-gearman example

I am trying to run a basic example of gearman using python-gearman library available here. I am running python 2.7.3
Worker:
import gearman
gm_worker = gearman.GearmanWorker(['localhost:4730'])
def task_listener_reverse(gearman_worker, gearman_job):
print 'reporting status'
return reversed(gearman_job.data)
gm_worker.set_client_id('testclient')
gm_worker.register_task('reverse', task_listener_reverse)
gm_worker.work()
Client:
import gearman
gm_client = gearman.GearmanClient(['localhost:4730'])
print 'Sending job...'
request = gm_client.submit_job('reverse', 'Hello World!')
print "Result: " + request.result
I am getting the following error (full trace available here)
File "/Users/developer/gearman/connection_manager.py", line 27, in _enforce_byte_string
raise TypeError("Expecting byte string, got %r" % type(given_object))
TypeError: Expecting byte string, got <type 'reversed'>
Any help would be appreciated!
Thanks.
reversed() returns an iterator, not a bytestring. Use the negative stride slicing trick instead:
return gearman_job.data[::-1]
This returns a reversed string instead.
Compare:
>>> reversed('somedata')
<reversed object at 0x100480e50>
>>> 'somedata'[::-1]
'atademos'
For the sake of other people facing similar errors, you need to return a string from worker. If you do not return explicitly or return data of any other type, scrapy throws an error. Reason is simple that Gearman's protocol is text based.

How do I search for text in a page using regular expressions in Python?

I'm trying to create a simple module for phenny, a simple IRC bot framework in Python. The module is supposed to go to http://www.isup.me/websitetheuserrequested to check is a website was up or down. I assumed I could use regex for the module seeing as other built-in modules use it too, so I tried creating this simple script although I don't think I did it right.
import re, urllib
import web
isupuri = 'http://www.isup.me/%s'
check = re.compile(r'(?ims)<span class="body">.*?</span>')
def isup(phenny, input):
global isupuri
global cleanup
bytes = web.get(isupuri)
quote = check.findall(bytes)
result = re.sub(r'<[^>]*?>', '', str(quote[0]))
phenny.say(result)
isup.commands = ['isup']
isup.priority = 'low'
isup.example = '.isup google.com'
It imports the required web packages (I think), and defines the string and the text to look for within the page. I really don't know what I did in those four lines, I kinda just ripped the code off another phenny module.
Here is an example of a quotes module that grabs a random quote from some webpage, I kinda tried to use that as a base: http://pastebin.com/vs5ypHZy
Does anyone know what I am doing wrong? If something needs clarified I can tell you, I don't think I explained this enough.
Here is the error I get:
Traceback (most recent call last):
File "C:\phenny\bot.py", line 189, in call
try: func(phenny, input)
File "C:\phenny\modules\isup.py", line 18, in isup
result = re.sub(r'<[^>]*?>', '', str(quote[0]))
IndexError: list index out of range
try this (from http://docs.python.org/release/2.6.7/library/httplib.html#examples):
import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("HEAD","/index.html")
res = conn.getresponse()
if res.status >= 200 and res.status < 300:
print "up"
else:
print "down"
You will also need to add code to follow redirects before checking the response status.
edit
Alternative that does not need to handle redirects but uses exceptions for logic:
import urllib2
request = urllib2.Request('http://google.com')
request.get_method = lambda : 'HEAD'
try:
response = urllib2.urlopen(request)
print "up"
print response.code
except urllib2.URLError, e:
# failure
print "down"
print e
You should do your own tests and choose the best one.
The error means your regexp wasn't found anywhere on the page (the list quote has no element 0).

Categories