| 1 |
#! /usr/bin/env perl |
| 2 |
# |
| 3 |
# insert.pl |
| 4 |
# |
| 5 |
# Dictionary maintenance tool - insert words into dictionary. |
| 6 |
# Given a list of words and a dictionary file, this script |
| 7 |
# will insert the words into the file, in alphabetical order, |
| 8 |
# with a minimum of disruption of the file itself. |
| 9 |
# |
| 10 |
# It is assumed that the word-list holds one word per line, |
| 11 |
# and that the word-list is already in alphabetical order. |
| 12 |
# |
| 13 |
# Example usage: |
| 14 |
# ./insert.pl wordlist_file en/words/words.adj.1 |
| 15 |
# |
| 16 |
# Copyright (C) 2009 Linas Vepstas <[email protected]> |
| 17 |
# |
| 18 |
|
| 19 |
if ($#ARGV < 1) |
| 20 |
{ |
| 21 |
die "Usage: insert.pl wordlist-file dict-file\n"; |
| 22 |
} |
| 23 |
|
| 24 |
# Get the word-list, and the dictionary, from the command line |
| 25 |
my $wordlist_file = @ARGV[0]; |
| 26 |
my $dict_file = @ARGV[1]; |
| 27 |
|
| 28 |
open(WORDS, $wordlist_file); |
| 29 |
my @words = (); |
| 30 |
while (<WORDS>) |
| 31 |
{ |
| 32 |
# Get rid of newline at end of word. |
| 33 |
chop; |
| 34 |
push @words, $_; |
| 35 |
} |
| 36 |
close(WORDS); |
| 37 |
|
| 38 |
my $word = shift @words; |
| 39 |
|
| 40 |
open (DICT, $dict_file); |
| 41 |
while (<DICT>) |
| 42 |
{ |
| 43 |
chop; |
| 44 |
my @entries = split; |
| 45 |
|
| 46 |
# Loop over the entries |
| 47 |
my $linelen = 0; |
| 48 |
foreach (@entries) |
| 49 |
{ |
| 50 |
my $wd = $_; |
| 51 |
$linelen += length $_; |
| 52 |
while (($_ gt $word) && ($word ne "")) |
| 53 |
{ |
| 54 |
print "$word "; |
| 55 |
$linelen += length $word; |
| 56 |
$word = shift @words; |
| 57 |
|
| 58 |
# Insert a newline if the resulting line is too long. |
| 59 |
if ($linelen > 62) |
| 60 |
{ |
| 61 |
print "\n"; |
| 62 |
$linelen = 0; |
| 63 |
} |
| 64 |
} |
| 65 |
print "$_ "; |
| 66 |
} |
| 67 |
print "\n"; |
| 68 |
} |
| 69 |
close (DICT); |
| 70 |
|
| 71 |
|