Using Python for a demonstration in historical linguistics

Peter Otten __peter__ at web.de
Sat Nov 6 07:18:39 EDT 2010


Peter Otten wrote:

>>>> s = """
> ... In the framework of a project on evolutionary linguistics I wish to
> ... have a program to process words and simulate the effect of sound
> ... shift, for instance following the Rask's-Grimm's rule. I look to have
> ... python take a dictionary file or a string input and replace the
> ... consonants in it with the Grimm rule equivalent. For example:
> ... """
>>>> rules = ["bpf", ("d", "t", "th"), "gkx"]
>>>> for rule in rules:
> ...     rule = rule[::-1] # go back in time
> ...     for i in range(len(rule)-1):
> ...             s = s.replace(rule[i], rule[i+1])
> ...

Warning: this simple-minded approach somewhat limits the possible rules.
E. g. it fails for

a --> b
b --> a

>>> "abba".replace("a", "b").replace("b", "a")
'aaaa'

while unicode.translate() can deal with it:

>>> u"abba".translate({ord(u"a"): u"b", ord(u"b"): u"a"})
u'baab'

Or, if you are using Python 3.x as Steven suggested:

>>> "abba".translate({ord("a"): "b", ord("b"): "a"})
'baab'

Peter



More information about the Python-list mailing list