line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Tweet; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
3
|
|
|
3
|
|
60111
|
$Text::Tweet::AUTHORITY = 'cpan:GETTY'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
BEGIN { |
6
|
3
|
|
|
3
|
|
62
|
$Text::Tweet::VERSION = '0.004'; |
7
|
|
|
|
|
|
|
} |
8
|
|
|
|
|
|
|
# ABSTRACT: Optimize a tweet based on given keywords |
9
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
4017
|
use Moo; |
|
3
|
|
|
|
|
63435
|
|
|
3
|
|
|
|
|
18
|
|
11
|
3
|
|
|
3
|
|
7692
|
use Text::Trim; |
|
3
|
|
|
|
|
1954
|
|
|
3
|
|
|
|
|
3136
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has maxlen => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
default => sub { 140 }, |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has marker => ( |
20
|
|
|
|
|
|
|
is => 'ro', |
21
|
|
|
|
|
|
|
default => sub { '#' }, |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# TODO |
25
|
|
|
|
|
|
|
has marker_re => ( |
26
|
|
|
|
|
|
|
is => 'ro', |
27
|
|
|
|
|
|
|
default => sub { '\#' }, |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has hashtags_at_end => ( |
31
|
|
|
|
|
|
|
is => 'ro', |
32
|
|
|
|
|
|
|
default => sub { 0 }, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has keywords => ( |
36
|
|
|
|
|
|
|
is => 'ro', |
37
|
|
|
|
|
|
|
default => sub {[]}, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# TODO - mappings |
41
|
|
|
|
|
|
|
has mappings => ( |
42
|
|
|
|
|
|
|
is => 'ro', |
43
|
|
|
|
|
|
|
default => sub {{}}, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub make_tweet { |
47
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $text, $url, $keywords ) = @_; |
48
|
0
|
|
|
|
|
0
|
warn '['.__PACKAGE__.'] This function is DEPRECATED, use ->make($text,\$url,\@keywords)'."\n"; |
49
|
0
|
|
|
|
|
0
|
return $self->make($text,\$url,$keywords); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub make { |
53
|
13
|
|
|
13
|
0
|
5948
|
my $self = shift; |
54
|
13
|
|
|
|
|
56
|
return $self->_generate_tweet( $self->keywords, @_ ); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
0
|
0
|
0
|
sub make_without_keywords { shift->_generate_tweet(@_) } |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub parts_length { |
60
|
35
|
|
|
35
|
0
|
36
|
my @parts; |
61
|
35
|
|
|
|
|
53
|
for (@_) { |
62
|
80
|
100
|
|
|
|
142
|
if (ref $_ eq 'SCALAR') { |
63
|
28
|
|
|
|
|
29
|
push @parts, ${$_}; |
|
28
|
|
|
|
|
58
|
|
64
|
|
|
|
|
|
|
} else { |
65
|
52
|
|
|
|
|
88
|
push @parts, $_; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
35
|
|
|
|
|
113
|
length(join(' ',@parts)); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _generate_tweet { |
72
|
13
|
|
|
13
|
|
18
|
my $self = shift; |
73
|
|
|
|
|
|
|
|
74
|
13
|
|
|
|
|
14
|
my @keywords; |
75
|
|
|
|
|
|
|
my @parts; |
76
|
|
|
|
|
|
|
|
77
|
13
|
|
|
|
|
25
|
for my $part (@_) { |
78
|
51
|
|
|
|
|
54
|
my $newpart; |
79
|
51
|
100
|
|
|
|
134
|
if (ref $part eq 'ARRAY') { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
80
|
26
|
|
|
|
|
35
|
push @keywords, @{$part}; |
|
26
|
|
|
|
|
49
|
|
81
|
|
|
|
|
|
|
} elsif (ref $part eq 'HASH') { |
82
|
|
|
|
|
|
|
# TODO - mappings |
83
|
|
|
|
|
|
|
} elsif (ref $part eq 'SCALAR') { |
84
|
11
|
|
|
|
|
16
|
my $scalar_newpart = \trim(join(' ',split(/[\n\r\t ]+/,${$part}))); |
|
11
|
|
|
|
|
50
|
|
85
|
11
|
50
|
|
|
|
136
|
$newpart = $scalar_newpart if ${$scalar_newpart}; |
|
11
|
|
|
|
|
32
|
|
86
|
|
|
|
|
|
|
} else { |
87
|
14
|
|
|
|
|
168
|
$newpart = trim(join(' ',split(/[\n\r\t ]+/,scalar $part))); |
88
|
|
|
|
|
|
|
} |
89
|
51
|
100
|
|
|
|
417
|
push @parts, $newpart if $newpart; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
13
|
|
|
|
|
17
|
my @newparts; |
93
|
|
|
|
|
|
|
my @used_keywords; |
94
|
13
|
|
|
|
|
29
|
my $marker = $self->marker; |
95
|
13
|
|
|
|
|
25
|
my $marker_re = $self->marker_re; |
96
|
13
|
|
|
|
|
18
|
for my $keyword (@keywords) { |
97
|
|
|
|
|
|
|
|
98
|
38
|
100
|
|
|
|
63
|
if (!grep { lc($_) eq lc($keyword) } @used_keywords) { |
|
49
|
|
|
|
|
138
|
|
99
|
35
|
|
|
|
|
49
|
push @used_keywords, $keyword; |
100
|
|
|
|
|
|
|
|
101
|
35
|
|
|
|
|
72
|
my $count = parts_length(@parts,@newparts); |
102
|
35
|
50
|
|
|
|
148
|
last if $count + 1 + length($marker) > $self->maxlen; |
103
|
|
|
|
|
|
|
|
104
|
35
|
|
|
1
|
|
605
|
my $hkeyword = lc($keyword); |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
13
|
|
105
|
35
|
|
|
|
|
26224
|
$hkeyword =~ s/[^\w]|_//ig; |
106
|
35
|
|
|
|
|
52
|
$hkeyword = $marker.$hkeyword; |
107
|
|
|
|
|
|
|
|
108
|
35
|
|
|
|
|
48
|
my $found_in_parts = 0; |
109
|
|
|
|
|
|
|
|
110
|
35
|
100
|
|
|
|
92
|
if (!$self->hashtags_at_end) { |
111
|
23
|
|
|
|
|
50
|
for (@parts) { |
112
|
29
|
100
|
|
|
|
67
|
next if ref $_ eq 'SCALAR'; |
113
|
23
|
|
|
|
|
26
|
my $original_part = $_; |
114
|
23
|
|
|
|
|
776
|
$_ =~ s/($keyword)/$marker$1/i; |
115
|
23
|
100
|
|
|
|
6694
|
if ($_ ne $original_part) { |
116
|
17
|
|
|
|
|
21
|
$found_in_parts = 1; |
117
|
17
|
|
|
|
|
33
|
last; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
35
|
100
|
66
|
|
|
225
|
if ($self->hashtags_at_end || ( !$self->hashtags_at_end && !$found_in_parts ) ) { |
|
|
|
100
|
|
|
|
|
123
|
18
|
100
|
|
|
|
86
|
if ($count + 1 + length($hkeyword) <= $self->maxlen) { |
124
|
16
|
|
|
|
|
41
|
push @newparts, $hkeyword; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
} |
131
|
13
|
100
|
|
|
|
24
|
for (@parts) { $_ = ${$_} if (ref $_ eq 'SCALAR') }; |
|
25
|
|
|
|
|
58
|
|
|
11
|
|
|
|
|
31
|
|
132
|
13
|
|
|
|
|
24
|
push @parts, @newparts; |
133
|
|
|
|
|
|
|
|
134
|
13
|
|
|
|
|
112
|
return join(" ",@parts); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
1; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
__END__ |