File: Filter.pm

package info (click to toggle)
libstar-parser-perl 0.59-5
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 220 kB
  • sloc: perl: 1,360; makefile: 2
file content (231 lines) | stat: -rw-r--r-- 6,508 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package STAR::Filter;

use STAR::DataBlock;
use STAR::Dictionary;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);

$VERSION = '0.01';

# $Id: Filter.pm,v 1.2 2000/12/19 22:54:56 helgew Exp $  RCS Identification


####################
# Constructor: new #
####################

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = {};
    bless ($self,$class);
    return $self;
}


#############################
# Class method: filter_dict #
#############################

# a simple interactive method which goes through
# the dictionary category by category and prompts
# the user whether to keep/include it

# This method is preliminary and should be considered 
# subject to change

# this filters/reduces the in-memory data representation (.cob) 
# of the dictionary, not the dictionary file (.cif)

sub filter_dict {

    my ($self, @parameters) = @_;
    my ($dict, $dict_filtered, $options);

    while ($_ = shift @parameters) {
        $dict = shift @parameters if /-dict/;
        $options = shift @parameters if /-options/;
    }

    my ($d, $s, $c, $i); #data, save, category, item
    my (@saves, $save);
    my ( %keep_cat_lookup, $incl );

    $dict_filtered = STAR::Dictionary->new;

    $dict_filtered->{TITLE} = ($dict->{TITLE})."_filtered";
    $dict_filtered->{TYPE} = $dict->{TYPE};
    $dict_filtered->{FILE} = $dict->{FILE};
    $dict_filtered->{STARTLN} = $dict->{STARTLN};
    $dict_filtered->{ENDLN} = $dict->{ENDLN};

    print $dict->get_attributes;

    #build up keep_cat_lookup
    foreach $d ( keys %{$dict->{DATA}} ) {
        foreach $s ( sort keys %{$dict->{DATA}{$d}} ) {
            if ( $s eq "-" ) {  #dictionary itself (no save block)
                $keep_cat_lookup{lc($s)} = $s;
            }
            elsif ( $s !~ /\./ ) {  #it's a category, not an item
                print "Category $s -- include? (y/n)";
                $incl = <STDIN>;
                chomp $incl;
                if ( $incl =~ /y/ ) {
                    $keep_cat_lookup{lc($s)} = $s;   #hash lookup 
                                                     #lower case => original
                }  
            }
        }
    }

    #filter dictionary according to keep_cat_lookup hash
    foreach $d ( keys %{$dict->{DATA}} ) {  
        foreach $s ( keys %{$dict->{DATA}{$d}} ) {

            if ( $s !~ /\./ && $keep_cat_lookup{lc($s)} ) {
            #save block that's a category to be included

                foreach $c ( keys %{$dict->{DATA}{$d}{$s}} ) {
                    foreach $i ( keys %{$dict->{DATA}{$d}{$s}{$c}} ) {
                            $dict_filtered->{DATA}{$d}{$s}{$c}{$i} =
                            $dict->{DATA}{$d}{$s}{$c}{$i};
                    }
                }
            }

            if ( $s =~ /^_(\S+)\./ && $keep_cat_lookup{lc($1)} ) {
            #save block that's an item in a category to be included

                foreach $c ( keys %{$dict->{DATA}{$d}{$s}} ) {
                    foreach $i ( keys %{$dict->{DATA}{$d}{$s}{$c}} ) {
                            $dict_filtered->{DATA}{$d}{$s}{$c}{$i} =
                            $dict->{DATA}{$d}{$s}{$c}{$i};
                    }
                }
            }
        }
        $dict_filtered->{DATA}{$d}{"-"}{"_dictionary"}
                        {"_dictionary.version"}[0]
          .= "_filtered";
    }

    return $dict_filtered;
}


#####################################
# Class method: filter_through_dict #
#####################################

sub filter_through_dict {

    my ($self,@parameters) = @_;
    my ($data, $out, $dict, $options);

    while ($_ = shift @parameters) {
       $data = shift @parameters if /-data/;
       $dict = shift @parameters if /-dict/;
       $options = shift @parameters if /-options/;
    }

    my ($d,$s,$c,$i);  # data, save, category, item
    my (@items);
    my ($dict_item, @dict_items, %dict_lookup);

    @items = $data ->get_items;
    @dict_items = $dict->get_save_blocks;

    foreach $dict_item (@dict_items) {
        $dict_lookup{lc($dict_item)} = $dict_item;
    }

    $out = STAR::DataBlock->new;

    $out->{TITLE} = $data->{TITLE};
    $out->{TYPE} = $data->{TYPE};
    $out->{FILE} = $data->{FILE};
    $out->{STARTLN} = $data->{STARTLN};
    $out->{ENDLN} = $data->{ENDLN};

    foreach $d ( keys %{$data->{DATA}} ) {  
        foreach $s ( keys %{$data->{DATA}{$d}} ) {
            foreach $c ( keys %{$data->{DATA}{$d}{$s}} ) {
                foreach $i ( keys %{$data->{DATA}{$d}{$s}{$c}} ) {
                    if ( $dict_lookup{lc($i)} ) {
                        $out ->{DATA}{$d}{$s}{$c}{$i} =
                        $data->{DATA}{$d}{$s}{$c}{$i};
                    }
                }
            }
        }
    }
    return $out;
}

1;
__END__

=head1 NAME

STAR::Filter - Perl extension for filtering DataBlock objects

=head2 Version

This documentation refers to version 0.01 of this module.

=head1 SYNOPSIS

  use STAR::Filter;

=head1 DESCRIPTION

Contains the filter object for filtering DataBlock objects.
DataBlock objects are created by Parser and modified by DataBlock.

=head1 CLASS METHODS

=head2 filter_dict

  Usage:  $filtered_dict = STAR::Filter->filter_dict(
                             -dict=>$dict,
                             -options=>$options);

A (very simplistic) interactive method for filtering a STAR::Dictionary 
object (.cob file). The user is prompted for each category whether 
to include (retain) it in the filtered object. The method returns a 
reference to the filtered (reduced) STAR::Dictionary object.

Note: This method is preliminary and subject to change.

=head2 filter_through_dict

  Usage:  $filtered_data = STAR::Filter->filter_through_dict(
                             -data=>$data,
                             -dict=>$dict,
                             -options=>$options);

Filters an STAR::DataBlock object through a STAR::Dictionary object. 
Returns a reference to a new STAR::DataBlock object in which only 
those items are included which were defined in the specified dictionary.

=head1 AUTHOR

Wolfgang Bluhm, mail@wbluhm.com

=head2 Acknowledgments

Thanks to Phil Bourne, Helge Weissig, Anne Kuller, Doug Greer, 
Michele Bluhm, and others for support, help, and comments.

=head1 COPYRIGHT

A full copyright statement is provided with the distribution
Copyright (c) 2000 University of California, San Diego

=head1 SEE ALSO

STAR::Parser, STAR::DataBlock, STAR::Dictionary.

=cut