lib/Serge/Util/WordStats.pm

filter: Statementstmt 15.0% 9 / 60 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 2 / 6 Podpod 0.0% 0 / 4 totaltotal 14.4% 11 / 76 SCAR 33.7
CC
6
Cov
14%
find_placeholders
20.0
count_words
12.0
wordcount
2.0
CRAP 29.2
« lib/Serge/Sync/Util.pm ↑ Summary lib/Serge/Engine/Plugin/parse_rrc.pm »
1 package Serge::Util::WordStats;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our @ISA = qw(Exporter);
7  
8 1 our @EXPORT = qw(
9 wordcount
10 get_wordcount_chunks
11 count_words
12 );
13  
14 sub wordcount {
15 0 ▶ my $string = shift;
Subroutine wordcount: not called CC=1 SCAR=6.9
Pod undocumented
16  
17 0 my $chunks = get_wordcount_chunks($string);
18  
19 0 return count_words($chunks);
20 }
21  
22 sub get_wordcount_chunks {
23 0 ▶ my $string = shift;
Subroutine get_wordcount_chunks: not called CC=1 SCAR=6.9
Pod undocumented
24  
25 # As Trados seems to be buggy when it comes to translating
26 # multiline strings, replace newlines with special '{\n}' marker
27 # which we then will mark as non-translatable
28  
29  
30 0 $string =~ s/\n/{\\n}/g;
31  
32 0 my $chunks = [{
33 'translate' => 1,
34 'string' => $string
35 }
36 ];
37  
38 0 find_placeholders($chunks, '<\/?[\w]+.*?>'); # escaped XML tags (used in some strings)
39 0 find_placeholders($chunks, '<\/?[\w]+.*?>'); # XML tags
40 0 find_placeholders($chunks, '\\\{\d+\\\}|\{\d+\}'); # Java format and it's escaped version
41 0 find_placeholders($chunks, '\$\{[\w\.\:]+\}'); # template format
42 0 find_placeholders($chunks, '%\d\$\w'); # Android format
43 0 find_placeholders($chunks, '%[\d]*(?:.\d+)*(?:h|l|I|I32|I64)*[cdiouxefgns]'); # sprintf
44 0 find_placeholders($chunks, '%@'); # Objective C style placeholders
45 0 find_placeholders($chunks, '\$[\w\d]+?\$'); # dollar sign placeholders
46 0 find_placeholders($chunks, '\%[\w\d]+?\%'); # percent sign placeholders
47 0 find_placeholders($chunks, '\{\\\n\}'); # '{\n}' newline marker
48 0 find_placeholders($chunks, '\\\+[rnt]'); # escaping sequences (\n, \r, \t)
49 0 find_placeholders($chunks, '&#\d+;|&\w+;'); # XML entities
50 0 find_placeholders($chunks, 'Evernote International|Evernote Food|Evernote Hello|Evernote Clearly|Evernote Business|Skitch|Evernote®?|Food|^Hello$|Clearly'); # product names
51 0 find_placeholders($chunks, 'Ctrl\+\w$|Shift\+\w$|Alt\+\w$'); # Shortcuts
52 0 find_placeholders($chunks, 'Ctrl\+$|Shift\+$|Alt\+$'); # Shortcut modifiers
53 #find_placeholders($chunks, '^["\']+|["\']+$'); # surrounding quotes (including ones around placeholders)
54 #find_placeholders($chunks, '^\.$'); # end punctuation after (or between) placeholders
55  
56 # find patterns that are not counted as words in Trados
57 0 find_placeholders($chunks, '^[^\w\&]\s|\s[^\w\&]\s|\s[^\w\&]$|^[^\w\&]$', 'dont-count'); # hanging symbols (excluding a-z, _ and &)
58 #use Data::Dumper; print Dumper($aref);
59  
60 0 return $chunks;
61 }
62  
63  
64 sub count_words {
65 0 ▶ my ($chunks) = @_;
Subroutine count_words: not called CC=3 SCAR=24.8
Pod undocumented
66  
67 # These rules are based on observed Trados 2007 word calculation behavior
68 0 my $remove = qr{[\.]+}; # dots
69 0 my $delimiters = qr{\W+}; # anything except a-z, A-Z and _ ( \W doesn't work inside [...] )
70 0 my $english_date = qr{(^|\W)(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2},\s+(?:\d{2})?\d{2}(\W|$)};
71  
72 0 my $n = 0;
73  
74 0 foreach my $chunk (@$chunks) {
75 0 ▶ if ($chunk->{translate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk->{"translate"}) 0 0 0
76 0 my $s = $chunk->{string};
77 0 $s =~ s/$english_date/$1$2$3/g; # replace the date with just the month name (i.e. count as a single word)
78  
79 #print "::[$s]\n";
80  
81 0 $s =~ s/$remove//g;
82 0 $s =~ s/^$delimiters//;
83 0 $s =~ s/$delimiters$//;
84 0 my @a = split(/$delimiters/, $s);
85 #print "!!!\n".join("<\n>", @a)."\n!!!\n";
86 0 $n += scalar(@a);
87 }
88 }
89  
90 0 return $n;
91 }
92  
93  
94 sub find_placeholders {
95 0 ▶ my ($aref, $regex, $class) = @_;
Subroutine find_placeholders: not called CC=4 SCAR=30.0
Pod undocumented
96  
97 0 my $i = 0;
98 0 ▶ while ($i < scalar(@$aref)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$aref) 0 0 0
99 0 my $chunk = $aref->[$i];
100 0 ▶ if (!$chunk->{translate}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $chunk->{'translate'}) { } 0 0 0
101 0 $i++;
102 } else {
103 0 my @subchunks = split(/($regex)/, $chunk->{string});
104 0 my @a;
105 my $translate;
106 0 foreach my $subchunk (@subchunks) {
107 0 $translate = !$translate;
108 0 push @a, {'translate' => $translate, 'string' => $subchunk, 'class' => $class};
109 }
110 0 splice(@$aref, $i, 1, @a);
111 0 $i += scalar(@a);
112 }
113 }
114 }
115  
116 1 1;
« lib/Serge/Sync/Util.pm ↑ Summary lib/Serge/Engine/Plugin/parse_rrc.pm »