File: Spelling.pm

package info (click to toggle)
otrs2 2.0.4p01-17
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 7,892 kB
  • ctags: 4,437
  • sloc: perl: 81,607; xml: 8,135; sql: 8,013; sh: 1,113; makefile: 22; php: 16
file content (175 lines) | stat: -rw-r--r-- 5,277 bytes parent folder | download | duplicates (2)
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
# --
# Kernel/System/Spelling.pm - the global spelling module
# Copyright (C) 2001-2005 Martin Edenhofer <martin+code@otrs.org>
# --
# $Id: Spelling.pm,v 1.12 2005/06/15 03:51:33 martin Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --

package Kernel::System::Spelling;

use strict;
use Kernel::System::FileTemp;

use vars qw($VERSION);
$VERSION = '$Revision: 1.12 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;

# --
sub new {
    my $Type = shift;
    my %Param = @_;

    # allocate new hash for object
    my $Self = {};
    bless ($Self, $Type);

    $Self->{Debug} = 0;

    # get needed objects
    foreach (qw(ConfigObject LogObject)) {
        $Self->{$_} = $Param{$_} || die "Got no $_!";
    }

    # create file template object
    $Self->{FileTempObject} = Kernel::System::FileTemp->new(%Param);

    # spell checker config options
    $Self->{SpellChecker} = $Self->{ConfigObject}->Get('SpellCheckerBin') || 'ispell';
    $Self->{SpellChecker} .= ' -a ';

    return $Self;
}
# --
sub Check {
    my $Self = shift;
    my %Param = @_;
    # check needed stuff
    foreach (qw(Text)) {
      if (!$Param{$_}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need $_!");
        return;
      }
    }
    # get needed dict
    if ($Param{SpellLanguage}) {
        $Self->{SpellChecker} .= " -d $Param{SpellLanguage}";
    }
    # default ignored words
    my @Ignore = qw(com org de net Cc www Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Sun Mon Tue Wed Thu Fri Sat Fwd Re DNS Date To Cc Bcc ca tm COM Co op netscape bcc jpg gif email Tel ie eg otrs suse redhat debian caldera php perl java html unsubscribe queue event day month year ticket);
    # add configured ignored words
    if (ref($Self->{ConfigObject}->Get('SpellCheckerIgnore')) eq 'ARRAY') {
        foreach (@{$Self->{ConfigObject}->Get('SpellCheckerIgnore')}) {
            push (@Ignore, $_);
        }
    }
    # don't correct emails
    $Param{Text} =~ s/<.+?\@.+?>//g;
    $Param{Text} =~ s/\s.+?\@.+?\s/ /g;
    # don't correct quoted text
    $Param{Text} =~ s/^>.*$//gm;
    # ? (just do encoding for ispell)
    if ($Self->{SpellChecker} =~ /ispell/) {
        $Param{Text} =~ s//a"/g;
        $Param{Text} =~ s//o"/g;
        $Param{Text} =~ s//u"/g;
        $Param{Text} =~ s//A"/g;
        $Param{Text} =~ s//O"/g;
        $Param{Text} =~ s//U"/g;
        $Param{Text} =~ s//sS/g;
    }
    # --
    # get spell output
    # --
    # write text to file and cat it throuh (i|a)spell
    # - can't use IPC::Open* because it's not working with mod_perl* :-/
    my ($FH, $TmpFile) = $Self->{FileTempObject}->TempFile();
    if ($FH) {
        print $FH $Param{Text};
    }
    else {
        $Self->{Error} = 1;
        $Self->{LogObject}->Log(
            Priority => 'error',
            Message => "Can't write spell tmp text to $TmpFile: $!",
        );
        return;
    }
    if (open (SPELL, "cat $TmpFile | $Self->{SpellChecker} |")) {
        my $Output = '';
        my %Data = ();
        my $Lines = 1;
        my $CurrentLine = 0;
        while (my $Line = <SPELL>) {
            $CurrentLine++;
            # ? (just do encoding for ispell)
            if ($Self->{SpellChecker} =~ /ispell/) {
                $Line =~ s/a"//g;
                $Line =~ s/o"//g;
                $Line =~ s/u"//g;
                $Line =~ s/A"//g;
                $Line =~ s/O"//g;
                $Line =~ s/U"//g;
                $Line =~ s/sS//g;
            }
            if ($Line =~ /^# (.+?) .+?$/) {
                $Data{$CurrentLine} = {
                    Word => $1,
                    Replace => '',
                    Line => $Lines,
                };
            }
            elsif ($Line =~ /^& (.+?) .+?: (.*)$/) {
                my @Replace = split(/, /, $2);
                $Data{$CurrentLine} = {
                    Word => $1,
                    Replace => \@Replace,
                    Line => $Lines,
                };
            }
            elsif ($Line =~ /^\n$/) {
                $Lines++;
            }
        }
        # drop double words and add line of double word
        my %DoubleWords;
        foreach (sort {$a <=> $b} keys %Data) {
            if ($DoubleWords{$Data{$_}->{Word}}) {
                $DoubleWords{$Data{$_}->{Word}}->{Line} .= "/".$Data{$_}->{Line};
                delete $Data{$_};
            }
            else {
                $DoubleWords{$Data{$_}->{Word}} = $Data{$_};
            }
        }
        # remove ignored words
        foreach (sort keys %Data) {
            foreach my $IgnoreWord (@Ignore) {
                if ($Data{$_}->{Word} && $Data{$_}->{Word} =~ /^$IgnoreWord$/i) {
                    delete $Data{$_};
                }
            }
        }
        close (SPELL);
        return %Data;
    }
    else {
        $Self->{Error} = 1;
        $Self->{LogObject}->Log(
            Priority => 'error',
            Message => "Can't open spell: $!",
        );
        return;
    }
}
# --
sub Error {
    my $Self = shift;
    my %Param = @_;
    return $Self->{Error};
}
# --
1;