source: src/grp/grp @ 3b02b04

Last change on this file since 3b02b04 was 3b02b04, checked in by Tomasz Obrebski <to@…>, 11 years ago

prawie ca�kiem nowe dgc, du�e zmiany w dgp, pomniejsze poprawki

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