| 1 | # | ||
| 2 | # Graph::Reader::HTK - perl module for reading an HTK lattice into a Graph | ||
| 3 | # | ||
| 4 | package Graph::Reader::HTK; | ||
| 5 | $Graph::Reader::HTK::VERSION = '2.10'; | ||
| 6 | use 5.006; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use parent 'Graph::Reader'; | ||
| 11 | use Carp; | ||
| 12 | |||
| 13 | my %node_attributes = | ||
| 14 | ( | ||
| 15 | 'WORD' => [ 'WORD', 'label' ], | ||
| 16 | 'W' => [ 'WORD', 'label' ], | ||
| 17 | 'time' => [ 'time' ], | ||
| 18 | 't' => [ 'time' ], | ||
| 19 | 'var' => [ 'var' ], | ||
| 20 | 'v' => [ 'var' ], | ||
| 21 | 'L' => [ 'L' ], | ||
| 22 | ); | ||
| 23 | |||
| 24 | my %edge_attributes = | ||
| 25 | ( | ||
| 26 | 'WORD' => [ 'WORD', 'label' ], | ||
| 27 | 'W' => [ 'WORD', 'label' ], | ||
| 28 | 'START' => [ 'START' ], | ||
| 29 | 'S' => [ 'START' ], | ||
| 30 | 'END' => [ 'END' ], | ||
| 31 | 'E' => [ 'END' ], | ||
| 32 | 'var' => [ 'var' ], | ||
| 33 | 'v' => [ 'var' ], | ||
| 34 | 'div' => [ 'div' ], | ||
| 35 | 'd' => [ 'div' ], | ||
| 36 | 'acoustic' => [ 'acoustic' ], | ||
| 37 | 'a' => [ 'acoustic' ], | ||
| 38 | 'ngram' => [ 'ngram' ], | ||
| 39 | 'n' => [ 'ngram' ], | ||
| 40 | 'language' => [ 'language', 'weight' ], | ||
| 41 | 'l' => [ 'language', 'weight' ], | ||
| 42 | ); | ||
| 43 | |||
| 44 | #======================================================================= | ||
| 45 | # | ||
| 46 | # _read_graph | ||
| 47 | # | ||
| 48 | # The private method which implements the actual reading of the file. | ||
| 49 | # | ||
| 50 | #======================================================================= | ||
| 51 | sub _read_graph | ||
| 52 | { | ||
| 53 | my $self = shift; | ||
| 54 | my $graph = shift; | ||
| 55 | my $FILE = shift; | ||
| 56 | |||
| 57 | my $nvertices; | ||
| 58 | my $nedges; | ||
| 59 | my $v; | ||
| 60 | my $from; | ||
| 61 | my $to; | ||
| 62 | my $weight; | ||
| 63 | my ($an, $av); | ||
| 64 | my $node_num; | ||
| 65 | my $link; | ||
| 66 | |||
| 67 | |||
| 68 | while (<$FILE>) | ||
| 69 | { | ||
| 70 | chop; | ||
| 71 | |||
| 72 | #--------------------------------------------------------------- | ||
| 73 | # ignore version line | ||
| 74 | #--------------------------------------------------------------- | ||
| 75 | if (/^\s*(V|VERSION)\s*=\s*(\S+)/) | ||
| 76 | { | ||
| 77 | $graph->set_graph_attribute('HTK_VERSION', $2); | ||
| 78 | next; | ||
| 79 | } | ||
| 80 | |||
| 81 | if (/^\s*(base|lmname|lmscale|wdpenalty)\s*=\s*(\S+)/) | ||
| 82 | { | ||
| 83 | # print STDERR "Setting graph attribute $1 to $2\n"; | ||
| 84 | $graph->set_graph_attribute($1, ''.$2); | ||
| 85 | } | ||
| 86 | |||
| 87 | #--------------------------------------------------------------- | ||
| 88 | # line which says how many nodes & links we have | ||
| 89 | #--------------------------------------------------------------- | ||
| 90 | if (/N\s*=\s*(\d+)\s*L\s*=\s*(\d+)/) | ||
| 91 | { | ||
| 92 | # $NUM_NODES = $1; | ||
| 93 | # $NUM_LINKS = $2; | ||
| 94 | next; | ||
| 95 | } | ||
| 96 | |||
| 97 | #--------------------------------------------------------------- | ||
| 98 | # node definition | ||
| 99 | #--------------------------------------------------------------- | ||
| 100 | if (/^I\s*=\s*(\d+)/mg) | ||
| 101 | { | ||
| 102 | #--------------------------------------------------------------- | ||
| 103 | # strip off the fields from the rest of the line | ||
| 104 | # and set appropriate attributes | ||
| 105 | #--------------------------------------------------------------- | ||
| 106 | $node_num = "n$1"; | ||
| 107 | $graph->add_vertex($node_num); | ||
| 108 | # print STDERR "Node $node_num:\n"; | ||
| 109 | while (/\s*(\S+)\s*=\s*(\S+)/mg) | ||
| 110 | { | ||
| 111 | $an = $1; | ||
| 112 | $av = $2; | ||
| 113 | if (exists $node_attributes{$an}) | ||
| 114 | { | ||
| 115 | foreach my $a (@{ $node_attributes{$an} }) | ||
| 116 | { | ||
| 117 | # print STDERR " attr $a = $av\n"; | ||
| 118 | $graph->set_vertex_attribute($node_num, $a, $av); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | else | ||
| 122 | { | ||
| 123 | carp "unknown node field \"$an\" - ignoring\n"; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | next; | ||
| 127 | } | ||
| 128 | elsif (/I\s*=\s*/) | ||
| 129 | { | ||
| 130 | carp "unexpected format for node line \"$_\" - ignoring\n"; | ||
| 131 | next; | ||
| 132 | } | ||
| 133 | |||
| 134 | #--------------------------------------------------------------- | ||
| 135 | # edge definition | ||
| 136 | #--------------------------------------------------------------- | ||
| 137 | if (/^J\s*=\s*(\d+)/mg) | ||
| 138 | { | ||
| 139 | my %attr; | ||
| 140 | |||
| 141 | $link = $1; | ||
| 142 | # print STDERR "Edge $link:\n"; | ||
| 143 | while (/\s*(\S+)\s*=\s*(\S+)/mg) | ||
| 144 | { | ||
| 145 | $an = $1; | ||
| 146 | $av = $2; | ||
| 147 | # print STDERR " field $an = $av\n"; | ||
| 148 | if (exists $edge_attributes{$an}) | ||
| 149 | { | ||
| 150 | foreach my $a (@{ $edge_attributes{$an} }) | ||
| 151 | { | ||
| 152 | $attr{$a} = $av; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | else | ||
| 156 | { | ||
| 157 | carp "unknown link field \"$an\" - ignoring\n"; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | if (exists $attr{START} && exists $attr{END}) | ||
| 162 | { | ||
| 163 | $from = 'n'.$attr{START}; | ||
| 164 | $to = 'n'.$attr{END}; | ||
| 165 | delete $attr{START}; | ||
| 166 | delete $attr{END}; | ||
| 167 | } | ||
| 168 | else | ||
| 169 | { | ||
| 170 | carp "link on line $. doesn't have START and END - ignoring\n"; | ||
| 171 | next; | ||
| 172 | } | ||
| 173 | $graph->add_edge($from, $to); | ||
| 174 | foreach $a (keys %attr) | ||
| 175 | { | ||
| 176 | # print STDERR " attr $a = ", $attr{$a}, "\n"; | ||
| 177 | $graph->set_edge_attribute($from, $to, $a, $attr{$a}); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | elsif (/^J/) | ||
| 181 | { | ||
| 182 | carp "unexpected format for link line \"$_\" - ignoring\n"; | ||
| 183 | next; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | return 1; | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | __END__ |