Lingua/DetectCharset.pm

filter: Statementstmt 88.4% 46 / 52 Branchbran 65.0% 13 / 20 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 66.9% 71 / 106 CC 20 SCAR 29.5
CC
20
Cov
72%
Detect
32.6
GetCodeScore
9.0
BEGIN
1.0
CRAP 19.0
« Convert/Cyrillic.pm ↑ Summary Lingua/DetectCharset/StatKoi8u.pm »
1 # Package Lingua::DetectCharset
2 # Version 1.02
3 # Part of "WWW Cyrillic Encoding Suite"
4 # Get docs and newest version from
5 # http://www.neystadt.org/cyrillic/
6 #
7 # Copyright (c) 1997-98, John Neystadt <http://www.neystadt.org/john/>
8 # You may install this script on your web site for free
9 # To obtain permision for redistribution or any other usage
10 # contact john@neystadt.org.
11 #
12 # Portions copyright by
13 #
14 # Drop me a line if you deploy this script on tyour site.
15  
16 package Lingua::DetectCharset;
17  
18 1 $VERSION = "1.02";
19  
20 1 ▶use Convert::Cyrillic;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Lingua::DetectCharset::StatKoi;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Lingua::DetectCharset::StatWin;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Lingua::DetectCharset::StatUtf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 $PairSize = 2;
26 1 $MinRatio = 1.5; # Mark must be in $MinRatio times larger of
27 # one encoding than another to decide upon, or ENG.
28 1 $DoubtRatio = 1;
29 1 $DoubtLog = 'DetectCharsetDoubt.txt';
30  
31 1 ▶sub Detect {
Subroutine Detect: called CC=12 SCAR=34.8
Pod undocumented
32 1 my (@Data) = @_;
33 1 my ($KoiMark) = GetCodeScore ('Koi', @Data);
34 1 my ($WinMark) = GetCodeScore ('Win', @Data);
35 1 my ($Utf8Mark) = GetCodeScore ('Utf8', @Data);
36  
37 # print STDERR "GetEncoding: Koi8 - $KoiMark, Win - $WinMark, Utf8 - $Utf8Mark\n";
38  
39 1 $KoiRatio = $KoiMark/($WinMark+$Utf8Mark+1);
40 1 $WinRatio = $WinMark/($KoiMark+$Utf8Mark+1);
41 1 $Utf8Ratio = $Utf8Mark/($KoiMark+$WinMark+1);
42  
43 1 ▶ if ($DoubtLog) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DoubtLog) 1 0 1
44 1 ▶ if (($KoiRatio < $MinRatio && $KoiRatio > $DoubtRatio) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($KoiRatio < $MinRatio and $KoiRatio > $DoubtRatio or $WinRatio < $MinRatio and $WinRatio > $DoubtRatio or $Utf8Ratio < $MinRatio and $Utf8Ratio > $DoubtRatio) 0 1 1
Condition33%5/15
$KoiRatio < $MinRatio and $KoiRatio > $DoubtRatio or $WinRatio < $MinRatio and $WinRatio > $DoubtRatio or $Utf8Ratio < $MinRatio and $Utf8Ratio > $DoubtRatio
l!l&&r!l&&!r
001
$KoiRatio < $MinRatio and $KoiRatio > $DoubtRatio or $WinRatio < $MinRatio and $WinRatio > $DoubtRatio
l!l&&r!l&&!r
001
$KoiRatio < $MinRatio and $KoiRatio > $DoubtRatio
!ll&&!rl&&r
010
$WinRatio < $MinRatio and $WinRatio > $DoubtRatio
!ll&&!rl&&r
100
$Utf8Ratio < $MinRatio and $Utf8Ratio > $DoubtRatio
!ll&&!rl&&r
010
MC/DC0%0/6
$KoiRatio < $MinRatio and $KoiRatio > $DoubtRatio or $WinRatio < $MinRatio and $WinRatio > $DoubtRatio or $Utf8Ratio < $MinRatio and $Utf8Ratio > $DoubtRatio
$KoiRatio < $MinRatio $KoiRatio > $DoubtRatio $WinRatio < $MinRatio $WinRatio > $DoubtRatio $Utf8Ratio < $MinRatio $Utf8Ratio > $DoubtRatio
Truth table6%1/15
A and B or C and D or E and F
A= $KoiRatio < $MinRatioB= $KoiRatio > $DoubtRatioC= $WinRatio < $MinRatioD= $WinRatio > $DoubtRatioE= $Utf8Ratio < $MinRatioF= $Utf8Ratio > $DoubtRatio
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
45 ($WinRatio < $MinRatio && $WinRatio > $DoubtRatio) ||
46 ($Utf8Ratio < $MinRatio && $Utf8Ratio > $DoubtRatio)) {
47 0 open Log, ">>$DoubtLog";
48 0 print Log " Koi8 - $KoiMark, Win - $WinMark, Utf8 - $Utf8Mark\n",
49 join ("\n", @Data), "\n\n";
50 0 close Log;
51 }
52 }
53  
54 1 ▶ return 'KOI8' if $KoiRatio > $WinRatio && $KoiRatio > $Utf8Ratio; # $MinRatio;
Branch: 1 branch
BranchTrueFalseTotal
if $KoiRatio > $WinRatio and $KoiRatio > $Utf8Ratio 0 1 1
Condition33%1/3
$KoiRatio > $WinRatio and $KoiRatio > $Utf8Ratio
!ll&&!rl&&r
100
MC/DC0%0/2
$KoiRatio > $WinRatio and $KoiRatio > $Utf8Ratio
$KoiRatio > $WinRatio $KoiRatio > $Utf8Ratio
Truth table33%1/3
A and B
A= $KoiRatio > $WinRatioB= $KoiRatio > $Utf8Ratio
ABresult
0X0
100
111
55 1 ▶ return 'WIN' if $WinRatio > $Utf8Ratio;
Branch: 1 branch
BranchTrueFalseTotal
if $WinRatio > $Utf8Ratio 1 0 1
56  
57 # We do english, only if no single cyrillic character were detected
58 0 ▶ return 'UTF8' if $WinRatio + $KoiRatio + $Utf8Ratio > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $WinRatio + $KoiRatio + $Utf8Ratio > 0 0 0 0
59 0 return 'ENG';
60 }
61  
62 3 ▶sub GetCodeScore {
Subroutine GetCodeScore: called CC=9 SCAR=22.0
Pod undocumented
63 3 my ($Code, @Data) = @_;
64 3 my ($Table);
65  
66 3 ▶ if ($Code eq 'Koi') {
Branch: 3 branches
BranchTrueFalseTotal
if ($Code eq 'Koi') { } 1 2 3
elsif ($Code eq 'Win') { } 1 1 2
elsif ($Code eq 'Utf8') { } 1 0 1
67 1 $Table = \%Lingua::DetectCharset::StatKoi::StatsTableKoi;
68 } elsif ($Code eq 'Win') {
69 1 $Table = \%Lingua::DetectCharset::StatWin::StatsTableWin;
70 } elsif ($Code eq 'Utf8') {
71 1 $Table = \%Lingua::DetectCharset::StatUtf8::StatsTableUtf8Long;
72 } else {
73 0 die "Don't know $Code!\n";
74 }
75  
76 3 ▶ $PairSize = 4 if $Code eq 'Utf8';
Branch: 1 branch
BranchTrueFalseTotal
if $Code eq "Utf8" 1 2 3
77 3 ▶ $PairSize = 2 if $Code ne 'Utf8';
Branch: 1 branch
BranchTrueFalseTotal
if $Code ne "Utf8" 2 1 3
78  
79 3 my ($Mark, $i);
80 3 for (@Data) {
81 3 s/[\n\r]//go;
82 3 $_ = Convert::Cyrillic::toLower ($_, $Code);
83 3 for (split (/[\.\,\-\s\:\;\?\!\'\"\(\)\d<>]+/o)) {
84 12 for $i (0..length ()-$PairSize) {
85 19 $Mark += ${$Table} {substr ($_, $i, $PairSize)};
86 }
87 }
88 }
89  
90 3 $Mark;
91 }
92 1 1;
93  
94 __END__
« Convert/Cyrillic.pm ↑ Summary Lingua/DetectCharset/StatKoi8u.pm »