Quick and Dirty Text-to-Speech in Python

I looked around for some python bindings for Festival, but neither pyfestival nor pyfest seem to be maintained. I was too tired to use swig or similar to wrap the C++ library. Instead, I wrote a thin shim between python and the Scheme-based command interpreter. (You must have festival installed.)


import os

BIN="/usr/bin/festival"

class Festival(object):
    def __init__(self):
        self.p = os.popen("%s --pipe" % BIN, "w")

    def eval(self, scm):
        self.p.write(scm + "\n")
        self.p.flush()

    def say(self, text):
        text = text.replace('"', '')
        self.eval('(SayText "%s")' % str(text))

Import the module, instantiate a Festival object, and call its "say" method to create utterances.


>>> import festival
>>> tts = festival.Festival()
>>> tts.say("Hello, world.")

It's quick. It's dirty. It works.

Sat, 20 Jun 2009 23:25

Comments:

Tagging Old Entries

As part of the current upgrade effort, I decided it was time to go through and tag older blog entries, the ones written before the introduction of tags. These entries were categorized, and the filesystem was used to manage the categorization. Each directory represented a category, and each subdirectory a subcategory. The directories were given descriptive names, suitable for use with the pycategories plugin for pyblosxom.

An obvious approach to automating the retagging of these old entries was to use the category hierarchy itself to provide the tags. I wrote a python program to walk the directory tree, and add tags to the old entries.

I chose to ignore the general category, though, because as a tag general doesn't provide much information. I think I might try a more sophisticated approach to those entries, analyzing the content of the entry to choose tags. I haven't worked out all the details, yet, but I'm considering building a map of tagged entries, based on the frequency of certain words or phrases that appear in them, then applying that map to a histogram of the entries currently categorized as general.

Tue, 26 Feb 2008 09:21

Comments:

Stupid Python Trick

From the source code is free speech department: python source code expresses meaning by outwardly contradicting that meaning.


__import__('sys',map(lambda x: getattr(globals(),'setdefault')(*x),[('_',getattr),('__',__import__),('__l', list('sd'))])).stdout=open('/dev/null','w')
_(__('os', map(lambda x: _(globals(),'setdefault')(*x),zip(__l,map(lambda x:_(__('this'),x),__l)))),'write')(1,''.join([d.get(c,c) for c in s]).split('\n')[int(_(__('sys'), 'argv')[1])+2]+'\n')

irony.py

Example usage:


$ python irony.py 0
Beautiful is better than ugly.
$

Mon, 30 Apr 2007 22:54

Comments:

Stackless Python, Lunchtime Lightning, Dev Panel

After SWIG, I just stayed put for a talk on stackless python. I knew nothing about stackless before the talk, and I only know a little more now. I'm still digesting the ideas; it's exciting.

Over lunch, conference sponsors gave lightning talks, five minute mini-sessions packing loads of information into a little time (with the overhanging threat of the mic being cut off when the timer reaches 0:00). We heard interesting things about scientific programming (SciPy) and development tools (bazaar, komodo), but the room was electrified by the CCP Games talk. EVE Online is built on a python (stackless!) infrastructure. The demo they showed, though prerendered, earned the only ovation offered during the lightning talks.

I'm now listening to the python dev panel, and the biggest discussion is about how python can be made to take advantage of multiple cores. The global interpreter lock makes this hard, and the limitations are not unique to python. The panel seemed to have the consensus that python that can move away from the GIL will be something other than CPython, maybe PyPy. (The panelists are CPython developers.) Argh, the pypy talk is going on opposite this one.

Question: What about bloat? (This was prompted by the keynote, which I'll write about later.)

One panelist (Andrew?) lamented that the language is changing, with new features being added, leading inevitably to larger size. He wishes that they could "quit monkeying with the language" and work instead on the standard library. (Is python finished? I wondered this in 2003, when list comprehensions were new. I mean, they're keen, but... I wonder if this is the attraction of LISP. Its been done a while, ARC notwithstanding.)

Speaking of core bloat, question: What needs to happen to get numeric stuff, like the array interface into the core? The numeric community needs to take the lead, submit patches, etc. Incremental changes are good, easier to review than large sweeping changes. Start as a library, popular, stable, that can be included in the "batteries included" distribution.

Question: What's the process to get a package accepted into the standard library? Step 1: Write it. Don't ask if you should write it, just write it. Get it accepted by the community. Follow the coding standards. Be ready to be the maintainer. It must be a general solution that is useful to the community at large. Documentation and tests must also be included.

Question: There's no mechanism for unloading a C extension. Should there be? There's not much call. There's no way to know of the extension is actually unused.

Question: Standard library vs. eggs. Setup tools will support eggs in 2.6. Should eggs replace large swaths of the standard library modules? Mmmmaybe.

Fri, 23 Feb 2007 14:32

Comments:

PyCon 2007 Live Blog: Swig Talk

Presented by Monty Taylor of MySQL.

Comparing other solutions, he only really talked about Boost.python. He doesn't like the huge ugly error messages.

Swig, he claims, is no longer slow, hard, or just for perl. It does make a lot of code, which can be interesting to look at.


%module simple
%include "simple.h"
%{
#include "simple.h"
%}

setuptools is Swig aware. That's kind of cool.

C Sucks. ...because of error checking requirements. Maybe so, but it doesn't become less complicated at the boundaries between C++ and python. Swig doen't make things easier here.

Swig allows extension of classes (monkeypatching).


class C {
public:
    class Inner {};
};

Cons: Swig doesn't handle inner classes at all. Overloaded methods are pretty awkward, going from Python back to C++.

typemaps: powerful; need their own talk.

Ran into Shane Geiger. I guess he's living in Austin now.

Swig docs swig python very complete.

Fri, 23 Feb 2007 11:59

Comments:

 Page   of 2