source: app/src/tok/tok @ 2fe08ac

help
Last change on this file since 2fe08ac was 20b4e44, checked in by pawelk <pawelk@…>, 16 years ago

Wersja niestabilna, zawiera wstepne poprawki.

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

  • Property mode set to 100755
File size: 1.9 KB
Line 
1#!/usr/bin/perl
2
3#package:       UAM Text Tools
4#component:     tok (tokenizer)
5#version:       1.0
6#author:        Tomasz Obrebski
7
8use strict;
9use locale;
10use Getopt::Long;
11use File::HomeDir;
12
13my $interactive=0;
14my $help;
15
16my $systemconfigfile='/usr/local/etc/utt/tok.conf';
17#my $userconfigfile="$ENV{'HOME'}/.utt/tok.conf";
18my $userconfigfile=home()."/.utt/tok.conf";
19
20#read configuration files###########################
21my $file;
22foreach $file ($systemconfigfile, $userconfigfile){
23        if(open(CONFIG, $configfile1)){
24                while (<CONFIG>) {
25                        chomp;
26                        s/#.*//;
27                        s/^\s+//;
28                        s/\s+$//;
29                        next unless length;
30                        my ($name, $value) = split(/\s*=\s*/, $_, 2);
31                        if(($name eq "interactive")or($name eq "i")){
32                                $interactive=1;
33                        }
34                        elsif(($name eq "help")or($name eq "h")){
35                                $help=1;
36                        }
37                }
38                close CONFIG;
39        }
40}
41#########################################################s
42
43GetOptions("interactive|i" => \$interactive,
44           "help|h" => \$help);
45
46if($help)
47{
48    print <<'END'
49Usage: tok [OPTIONS]
50
51Options:
52   --interactive                Interactive (no output buffering).
53   --help -h                    Help.
54END
55;
56    exit 0;
57}
58
59
60$| = $interactive;
61
62my $offset = 0;
63
64while(<>)
65{
66    1 while
67        / [[:alpha:]]+   (?{seg('W',$&)})
68        | \d+            (?{seg('N',$&)})
69        | \s+            (?{seg('S',$&)})
70        | [[:punct:]]    (?{seg('P',$&)})
71        | .              (?{seg('B',$&)})
72        /gxo;
73}
74
75#       | [^[:print:]]   (?{seg("B",$&)})
76
77
78sub seg
79{
80    my ($tag,$match) = @_;
81    my $len=length $match;
82    printf "%04d %02d %s ", $offset, $len, $tag;
83    if($tag eq 'S')
84    {
85        for(my $i=0; $i<$len; ++$i)
86        {
87            my $c = substr $match, $i, 1;
88            print '_' if $c eq ' ';
89            print '\n' if $c eq "\n";
90            print '\t' if $c eq "\t";
91            print '\r' if $c eq "\r";
92            print '\f' if $c eq "\f";
93        }
94    }
95    elsif($tag eq 'B')
96    {
97        printf "\\x%02X", ord($match);
98    }
99    else
100    {
101        print $match;
102    }
103    print "\n";
104    $offset += $len;
105}
Note: See TracBrowser for help on using the repository browser.