~jtv/corpusfiltergraph/cross-python

« back to all changes in this revision

Viewing changes to trunk/lib/corpusfg/graphs/sa-champollion/bin/load_dict

  • Committer: tahoar
  • Date: 2012-05-02 15:46:23 UTC
  • Revision ID: svn-v4:bc069b21-dff4-4e29-a776-06a4e04bad4e::266
new layout. need to update code to use the new layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
#
 
3
# Load the translation dictionary
 
4
# Each line of the dictionary files should look like:
 
5
#  src_word <> tgt_word
 
6
#
 
7
# Any entry which has a stop words as the source word
 
8
# would be discarded
 
9
#
 
10
 
 
11
# $dict_fn:    file name of the translation dictionary
 
12
# $xstop_href: reference to a hash table of the source language stop words
 
13
# $dict_href:  reference to the hash table of loaded dictionary;
 
14
#              the hash table is a hash of arrays with the keys being
 
15
#              the the source word, the value being an array of possible
 
16
#              translations
 
17
$| = 1; # disable Perl output buffering
 
18
sub load_dict {
 
19
    my ($dict_fn, $xstop_href, $dict_href) = @_;
 
20
 
 
21
    print STDERR "Reading seed translation lexicon...";
 
22
    open D, "<$dict_fn" || die "$0: Couldn't open $dict_fn!\n";
 
23
    while (<D>) {
 
24
        chomp;
 
25
        if (/^\s*(.+?)\s*<>\s*(.+?)\s*$/) {
 
26
            $source = $1;
 
27
            $translation = $2;
 
28
 
 
29
            # lowercase the source words; should be comment out
 
30
            # for multiple-byte encodings (such as Big5 Chinese) 
 
31
            # which overlaps with ASCII
 
32
            $source =~ tr/[A-Z]/[a-z]/;
 
33
 
 
34
            # discard entries with stop words
 
35
            next if defined $$xstop_href{$source};
 
36
 
 
37
            #$translation =~ s/\W/\\$&/g;
 
38
            push  @{$dict{"$source"}}, $translation;
 
39
        } else {
 
40
            print STDERR "invalid dictionary entry:\n$_\n";
 
41
        }
 
42
    }
 
43
    close D;
 
44
    print STDERR " done.\n";
 
45
    print STDERR "Number of entries: ", scalar keys %dict, "\n";
 
46
 
 
47
}
 
48
 
 
49
1;