Using Python for a demonstration in historical linguistics

Peter Otten __peter__ at
Sat Nov 6 12:18:39 CET 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")

while unicode.translate() can deal with it:

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

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

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


More information about the Python-list mailing list