Theiling Online    Sitemap    Conlang Mailing List HQ   

Reverse wordlist perl script

From:BP Jonsson <bpj@...>
Date:Sunday, August 12, 2001, 17:52
I have written a small perl script for reversing a wordlist -- e.g. turning
a Swedish-English wordlist into a English-Swedish wordlist, **not** a
reverse-alphabetically sorted list (tho I might write that script someday
too! :-)  The script handles multiple glosses and synonyms correctly, which
is what I wanted it for (database applications can't do that...)

I hope someone finds it useful!  It requires perl 5, BTW.

#! /usr/bin/perl -w

# Make a reverse word list: heads and glosses are separated by tabs,
# alternative glosses are separated by comma+space. Like this:
#
# apa   ape, monkey
# skog  wood, forest
# trä   wood
#
# which should be turned into:
#
# ape   apa
# forest        skog
# monkey        apa
# wood  skog, trä
#

# (c) BP Jonsson, bpj@netg.se, August -01

open(IN, 'wordlist.txt');
open(OUT, '>reverselist.txt');

# Initialize the hash to hold the reverse list
%revlist = ();

while(<IN>){
    chomp;

    # make sure each comma is followed by exactly one space
    s/, */,/g;  s/,/, /g;

    # split words from glosses at the tab
    ($word,$glosses) = split(/\t/,$_);
    # split the glosses into a list
    @glosses = split(/, /,$glosses);

    # go thru the glosses
    foreach $g (@glosses){
        if($g ne ''){  # make sure the gloss isn't empty!
            unless(exists($revlist{$g})){$revlist{$g} =$word}
            else{
                # add the new word to the value of the entry
                $val = $revlist{$g};
                $val = "$val, $word";
                $revlist{$g} = $val;
            }
        }
    }
}

foreach $key (sort(keys(%revlist))){
    print OUT "$key\t$revlist{$key}\n"
}

__END__

/BP 8^)>
--
B.Philip Jonsson mailto:bpX@netg.se (delete X)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~__
                A h-ammen ledin i phith!                \ \
     __  ____ ____    _____________ ____ __   __ __     / /
     \ \/___ \\__ \  /___  _____/\ \\__ \\ \  \ \\ \   / /
     / /   / /  /  \    / /Melroch\ \_/ // /  / // /  / /
    / /___/ /_ / /\ \  / /Melarokko\_  // /__/ // /__/ /
   /_________//_/  \_\/ /Eowine __  / / \___/\_\\___/\_\
Gwaedhvenn Angeliniel\ \______/ /a/ /_h-adar Merthol naun
  ~~~~~~~~~Kuinondil~~~\________/~~\__/~~~Noolendur~~~~~~
|| Lenda lenda pellalenda pellatellenda kuivie aiya! ||
"A coincidence, as we say in Middle-Earth" (JRR Tolkien)

Reply

Aidan Grey <frterminus@...>