Debian Codegraph @

debian/condense

raw
1 #!/usr/bin/perl
2
3 # condense -- condense cross-reference only foldoc entries
4 #
5 # Takes as sole argument a raw foldoc Dictionary data file.
6 #
7 # Prints to stdout the file with cross-reference only entries
8 # removed and their headword appended with $separator onto their
9 # cross-referenced entry headword.
10 #
11 # Assumes that headwords are unindented.
12
13 # Changelog:
14 $version = 0.1; # Thu Aug 28 09:39:18 EDT 2003
15 $version = 0.2; # Thu Aug 28 09:57:07 EDT 2003
16 # Added a $separator variable.
17
18 $separator = "%%%";
19
20 # verbosity level controls report to strerr:
21 # 0 no report
22 # 1 counts reported
23 # 2 quirks also listed
24 # 3 condensed headwords also listed
25
26 $verbose = 1;
27 die "Usage: $0 data_file\n" unless @ARGV == 1;
28
29 open DATA, $ARGV[0] or die "Can't open file $ARGV[0]\n";
30
31 # prime $new_headword
32 while($_=<DATA> and ! /^\S/) {}
33 chomp($new_headword = $_);
34
35 # scan the dictionary
36 while (defined $new_headword) {
37 $hw = $new_headword;
38 undef $new_headword;
39 $entry = "";
40 ++$headword{$hw};
41 while($_=<DATA> and ! /^\S/) { $entry .= $_; }
42 chomp($new_headword = $_);
43 if($entry =~ /^\s*\{\s*(.*\S)\s*\}\s*$/) {
44 $xref{$hw} = $1;
45 push @ordered_keys, $hw;
46 }
47 }
48
49 # shorten chains and analyze for errors
50 for $hw (@ordered_keys) {
51 if ( $headword{$hw} > 1 ) {
52 push @duplicate, $hw unless $duplicate{$hw};
53 ++$duplicate{$hw};
54 next;
55 }
56 $target = $xref{$hw};
57 if (! $headword{$target}) {
58 push @missing, $hw;
59 next;
60 }
61 %chain = undef;
62 ++$chain{$target};
63 while (defined $xref{$target} and $headword{$xref{$target}}
64 and $chain{$target} == 1) {
65 $target = $xref{$target};
66 ++$chain{$target};
67 }
68 if ($chain{$target} > 1) {
69 push @loop, $hw;
70 next;
71 }
72 ++$remove{$hw};
73 push @ordered_remove, $hw;
74 $headword_expansion{$target} .= $separator . $hw;
75 }
76
77 # report
78 if ($verbose > 0) {
79 warn scalar @ordered_remove, " condensable cross-references\n";
80 }
81 if ($verbose > 2 ) {
82 for(sort @ordered_remove) { warn" $_\n"; }
83 }
84 if ($verbose > 0) {
85 warn scalar @missing, " entries with dangling cross-reference\n";
86 }
87 if ($verbose > 1 ) {
88 for(sort @missing) { warn" $_\n"; }
89 }
90 if ($verbose > 0) {
91 warn scalar @duplicate, " duplicate entries with cross-reference\n";
92 }
93 if ($verbose > 1 ) {
94 for(sort @duplicate) { warn" $_\n"; }
95 }
96 if ($verbose > 0) {
97 warn scalar @loop, " looped chains of cross-references\n";
98 }
99 if ($verbose > 1 ) {
100 for(sort @loop) { warn" $_\n"; }
101 }
102
103 # starting over, prime $new_headword
104 seek DATA, 0, 0;
105 while($_=<DATA> and ! /^\S/) {print}
106 chomp($new_headword = $_);
107
108 # and write the condensed dictionary
109 while (defined $new_headword) {
110 $headword = $new_headword;
111 undef $new_headword;
112 if ($remove{$headword}) {
113 while($_=<DATA> and ! /^\S/) { }
114 chomp($new_headword = $_);
115 } else {
116 print $headword, $headword_expansion{$headword}, "\n";
117 while($_=<DATA> and ! /^\S/) { print; }
118 chomp($new_headword = $_);
119 }
120 }