Regular expression for matching IPA characters in Unicode?

  • Thread starter =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=
  • Start date
?

=?ISO-8859-1?Q?Mickel_Gr=F6nroos?=

Hi Pythoneers,

Which is the best way of checking that a given unicode string only
contains IPA characters, e.g. characters in the range \u0250-\u02AF?
I guess a regular expression would do it, just can't figure out how to
implement that expression.

Code snippets are most welcome.

Best regards,

Mickel Grönroos
 
M

Michael Hoffman

Mickel said:
Which is the best way of checking that a given unicode string only
contains IPA characters, e.g. characters in the range \u0250-\u02AF?

Well, I'll give you an example that only includes characters in the
range [\u0250, \u02AF] but those are just the IPA *extensions.* You also
need to include basic latin and greek characters from other blocks.

See: http://www.unicode.org/charts/PDF/U0250.pdf

And why do you want to do this anyway?

This example uses the itertools example all() which tells you whether a
predicate is true for every item in an iterable. The predicate here is
whether the item is contained in IPA_CHARS, which you can expand...

=====

import itertools
from sets import Set # set() is a built-in in 2.4

IPA_CHARS = Set(map(unichr, xrange(0x250, 0x2b0)))

def all(seq, pred=bool):
# http://www.python.org/doc/current/lib/itertools-example.html
"Returns True if pred(x) is True for every element in the iterable"
return False not in itertools.imap(pred, seq)

def is_ipa(iterable):
return all(iterable, IPA_CHARS.__contains__)

print is_ipa(u"aeiou") # this is valid IPA, but not in the extensions block
print is_ipa(u"\u0260\u02af") # valid IPA in the extensions block

====output===

False
True
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Mickel said:
Which is the best way of checking that a given unicode string only
contains IPA characters, e.g. characters in the range \u0250-\u02AF?

The regular expression for that is [\u0250-\u02AF]. You can either make
the regular expression a Unicode string itself, or you can make it a
normal (byte) string, and put the backslash-u-number sequence into it
(e.g. with double-backslash quotation).

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top