source: app/src/grp/grp @ a7b254c

help
Last change on this file since a7b254c was f5d3b20, checked in by obrebski <obrebski@…>, 18 years ago

ser i grp obsluguja tags

git-svn-id: svn://atos.wmid.amu.edu.pl/utt@38 e293616e-ec6a-49c2-aa92-f4a8b91c5d16

  • Property mode set to 100755
File size: 4.1 KB
Line 
1#!/usr/bin/perl
2
3#package:        UAM Text Tools
4#component name: grp
5#version:        1.0
6#author:         Tomasz Obrebski
7
8use strict;
9use Getopt::Long;
10use File::HomeDir;
11
12# katalog zawierajacy terms.m4
13my $LIB_DIR="/usr/local/lib/utt";
14
15my $systemconfigfile="/usr/local/etc/utt/grp.conf";
16#my $userconfigfile="$ENV{'HOME'}/.utt/grp.conf";
17my $userconfigfile=home()."/.utt/grp.conf";
18
19Getopt::Long::Configure('no_ignore_case_always');
20
21my $help=0;
22my $pattern=0;
23my $matches_only=0;
24my $macrofile=0;
25my $define=0;
26my $show_command=0;
27my $action="pgP";
28my $eos="seg(EOS)";
29my $morfield='lem';
30my $tags=0;
31
32#read configuration files###########################
33my $file;
34foreach $file ($systemconfigfile, $userconfigfile){
35  if(open(CONFIG, $file)){
36        while (<CONFIG>) {
37                chomp;                 
38                s/#.*//;               
39                s/^\s+//;               
40                s/\s+$//;               
41                next unless length;     
42                my ($name, $value) = split(/\s*=\s*/, $_, 2);
43                if(($name eq "pattern")or($name eq "e")){
44                        $pattern=$value;
45                }
46                elsif(($name eq "eos")or($name eq "E")){
47                        $eos=$value;
48                }
49                elsif($name eq "morph"){
50                        $morfield=$value;
51                }
52                elsif($name eq "macros"){
53                        $macrofile=$value;
54                }
55                elsif($name eq "define"){
56                        $define=$value;
57                }
58                elsif($name eq "command"){
59                        $show_command=1;
60                }
61                elsif($name eq "action"){
62                        $action;
63                }
64                elsif($name eq "tags"){
65                        $tags=$value;
66                }
67                elsif(($name eq "help")or($name eq "h")){
68                        $help=1;
69                }
70       
71        } 
72        close CONFIG;
73  }
74}
75#########################################################
76
77GetOptions("pattern|e=s" => \$pattern,
78           "eos|E=s" => \$eos,
79           "morph=s" => \$morfield,
80           "macros=s" => \$macrofile,
81           "define=s" => \$macrofile,
82           "command" => \$show_command,
83           "action=s" => \$action,
84           "tags=s" => \$tags,
85           "help|h" => \$help);
86
87if($help)
88{
89    print <<'END'
90Usage: gre [OPTIONS] [file ..]
91
92Options:
93   --pattern -e PATTERN         Pattern.
94   --eos -E PATTERN             Segment serving as sentence delimiter.
95   --morph=STRING               Field containing morphological information (default 'lem').
96   --macros=FILE                Read macrodefinitions from FILE.
97   --define=FILE                Add macrodefinitions from FILE.
98   --action -a [u][p][g][P]     Perform only indicated actions.
99                                    u - uncompress with 'lzop -cd'
100                                    p - preprocess
101                                    g - grep
102                                    P - postprocess
103                                (default pgP)
104   --tags=STRING                Morphosyntactic tag format.
105   --command                    Print the shell command to be executed and exit.
106   --help -h                    Help.
107END
108;
109    exit 0;
110}
111
112die("$0: no pattern given.\n") unless $pattern || $action !~ /g/;
113
114die("$0: macro file not found") unless
115    $macrofile or
116    -e "$LIB_DIR/terms.m4" and $macrofile="$LIB_DIR/terms.m4";
117
118die("$0: undefined tagset format (tags option missing)") unless
119    $tags;
120
121die("$0: $tags.tag2re program not found") unless
122    1; #JAK NAPISAC WARUNEK???
123
124
125my $uncompress = ($action =~ /u/) ? ' lzop -cd | '  : '';
126my $preproc    = ($action =~ /p/) ? ' fla  | '  : '';
127
128my $postproc   = ($action =~ /P/) ? ' | unfla '  : '';
129
130
131# discarding spaces
132$pattern =~ s/\s+/\\`'/g; #`
133# quoting escaped commas
134$pattern =~ s/\\,/\\`\\`\\,''/g;
135# quoting commas in {m,n} r.e. operator
136$pattern =~ s/(\{\d*),(\d*\})/\1\\`\\`,''\2/g;
137
138my $grepre = `echo \"$pattern\" | m4 --define=ENDOFSEGMENT='[[:cntrl:]]' --define=MORFIELD=$morfield $macrofile - 2>/dev/null`;
139
140die("Incorrect pattern (m4).") if $? >> 8;
141
142
143chomp $grepre;
144
145# <> expansion
146
147$grepre =~ s/<([^>]+)>/`echo $1 | $tags.tag2re`/ge;
148
149$grepre =~ s/\./[^ [:cntrl:]]/g;
150
151$grepre =~ s/\\s/[ ]/g;
152$grepre =~ s/\\S/[^ [:cntrl:]]/g;
153$grepre =~ s/\\d/[0-9]/g;
154$grepre =~ s/\\D/[^0-9 [:cntrl:]]/g;
155$grepre =~ s/\\w/[a-z±æê³ñ󶌿A-Z¡ÆÊ£ÑÓŠ¬¯0-9_]/g;
156$grepre =~ s/\\W/[^a-z±æê³ñ󶌿A-Z¡ÆÊ£ÑÓŠ¬¯0-9_ [:cntrl:]]/g;
157# extensions
158$grepre =~ s/\\l/[a-z±æê³ñ󶌿]/g; #lowercase letter
159$grepre =~ s/\\L/[A-Z¡ÆÊ£ÑÓŠ¬¯]/g; #upercase letter
160
161my $grep_command = ($action =~ /g/) ? "egrep '$grepre'" : " cat ";
162
163if($show_command)
164{
165    print $grep_command."\n";
166    exit 0;
167}
168
169#print $preproc.$grep_command.$postproc."\n";
170
171exec $preproc.$grep_command.$postproc;
Note: See TracBrowser for help on using the repository browser.