1 | #! /bin/bash |
---|
2 | |
---|
3 | set -e |
---|
4 | |
---|
5 | if [ $# -lt 2 ] |
---|
6 | then |
---|
7 | echo "Usage:" |
---|
8 | echo " compdic-update-fst <dictionary> <difference> <difference> ..." |
---|
9 | echo "where" |
---|
10 | echo " <dictionary> - fst format automaton" |
---|
11 | echo " <difference> - diff format file containing paths to be added/removed from dictionary" |
---|
12 | exit 0 |
---|
13 | fi |
---|
14 | |
---|
15 | tempdir=`mktemp -d /tmp/compdic.XXXXXX` |
---|
16 | |
---|
17 | dict=$1 |
---|
18 | shift |
---|
19 | |
---|
20 | dicplus=$tempdir/plus.dic |
---|
21 | fstplus=$tempdir/plus.fst |
---|
22 | dicminus=$tempdir/minus.dic |
---|
23 | fstminus=$tempdir/minus.fst |
---|
24 | |
---|
25 | touch $dicplus |
---|
26 | touch $dicminus |
---|
27 | |
---|
28 | while (($#)) |
---|
29 | do |
---|
30 | echo processing $1 ... |
---|
31 | cat $1 | egrep '^>' | sed -r 's/^> *//;s/[[:space:]].*$//' | canonize >> $dicplus |
---|
32 | cat $1 | egrep '^<' | sed -r 's/^< *//;s/[[:space:]].*$//' | canonize >> $dicminus |
---|
33 | shift |
---|
34 | done |
---|
35 | |
---|
36 | echo updating $dict ... |
---|
37 | |
---|
38 | if ! (( `cat $dicminus $dicplus | wc -l` )) |
---|
39 | then |
---|
40 | echo "Empty diff." |
---|
41 | exit 1 |
---|
42 | fi |
---|
43 | |
---|
44 | if (( `cat $dicminus | wc -l` )) |
---|
45 | then |
---|
46 | tmpfst1=$tempdir/tmp1.fst |
---|
47 | echo "running compdic-dic-to-fst $dicminus $fstminus" |
---|
48 | compdic-dic-to-fst $dicminus $fstminus |
---|
49 | echo "running fstdifference $dict $fstminus | fstdeterminize > $tmpfst1" |
---|
50 | fstdifference $dict $fstminus > $tmpfst1 |
---|
51 | else |
---|
52 | tmpfst1=$dict |
---|
53 | fi |
---|
54 | |
---|
55 | if (( `cat $dicplus | wc -l` )) |
---|
56 | then |
---|
57 | tmpfst2=$tempdir/tmp2.fst |
---|
58 | echo "running compdic-dic-to-fst $dicplus $fstplus" |
---|
59 | compdic-dic-to-fst $dicplus $fstplus |
---|
60 | echo "running fstunion $tmpfst1 $fstplus | fstdeterminize | fstminimize > $tmpfst2" |
---|
61 | fstunion $tmpfst1 $fstplus > $tmpfst2 |
---|
62 | else |
---|
63 | tmpfst2=$tmpfst1 |
---|
64 | fi |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | echo "running mv ${dict} ${dict}~" |
---|
69 | mv ${dict} ${dict}~ |
---|
70 | |
---|
71 | echo "cat ${tmpfst2} | fstrmepsilon | fstdeterminize | fstminimize > ${dict}" |
---|
72 | cat ${tmpfst2} | fstrmepsilon | fstdeterminize | fstminimize > ${dict} |
---|
73 | chmod a+r ${dict} |
---|
74 | |
---|
75 | rm -r $tempdir |
---|