Acme-Lingua-ZH-Remix
view release on metacpan or search on metacpan
lib/Acme/Lingua/ZH/Remix.pm view on Meta::CPAN
s/\s+$//;
s/^(.+?) //;
$_;
} grep { $_ } @xc;
return @phrases;
}
=head2 feed($corpus_text)
Instance method. Takes a scalar, return the topic object.
Merge C<$corpus_text> into the internal phrases corpus of the object.
=cut
sub feed {
my $self = shift;
my $corpus = shift;
my %phrase;
my @phrases = $self->split_corpus($corpus);
for (@phrases) {
my $p = substr($_, -1);
push @{$phrase{$p} ||=[]}, \$_;
}
$self->phrases(merge($self->phrases, \%phrase));
return $self;
}
sub phrase_ratio {
my $self = shift;
my $type = shift;
my $phrases = $self->phrases->{$type}||=[];
my $count = $self->phrase_count;
return 0 if $count == 0;
return @{$phrases} / $count;
}
sub random_phrase {
my $self = shift;
my $type = shift;
return ${ random(@{ $self->phrases->{$type}||=[] }) || \'' };
}
=head2 random_sentence( min => $min, max => $max )
Instance method. Optionally takes "min" or "max" parameter as the constraint of
sentence length (number of characters).
Both min and max values are required to be integers greater or equal to
zero. The value of max should be greater then the value of min. If any of these
values are invalidate, it is treated as if they are not passed.
The default values of min, max are 0 and 140, respectively.
The implementation random algorthm based, thus it needs indefinite time to
generate the result. If it takes more then 1000 iterations, it aborts and return
the results anyway, regardless the length constraint. This can happen when the
lengths of phrases from corpus do no adds up to a value within the given range.
The returned scalar is the generate sentence string of wide characters. (Which
makes Encode::is_utf8 return true.)
=cut
sub random_sentence {
my ($self, %options) = @_;
for my $p (qw(min max)) {
my $x = $options{$p};
unless (defined($x) && int($x) eq $x && $x >= 0) {
delete $options{$p}
}
}
if (defined($options{max}) && defined($options{min}) && $options{max} < $options{min}) {
delete $options{max};
delete $options{min};
}
$options{min} ||= 0;
$options{max} ||= 140;
my $str = "";
my @phrases;
my $ending = $self->random_phrase(random(qw/ã ï¼ ï¼/)) || "â¦";
while ( length($ending) > $options{max} ) {
$ending = $self->random_phrase(random(qw/ã ï¼ ï¼/)) || "â¦";
}
unshift @phrases, $ending;
my $l = length($ending);
my $iterations = 0;
my $max_iterations = 1000;
my $average = ($options{min} + $options{max}) / 2;
my $desired = int(rand($options{max} - $options{min}) + $options{min}) || $average || $options{max};
while ($iterations++ < $max_iterations) {
my $x;
do {
$x = random('ï¼', 'ã', 'ï¼', '/')
} while ($self->phrase_ratio($x) == 0);
my $p = $self->random_phrase($x);
if ($l + length($p) < $options{max}) {
unshift @phrases, $p;
$l += length($p);
}
my $r = abs(1 - $l/$desired);
last if $r < 0.1;
last if $r < 0.2 && $iterations >= $max_iterations/2;
}
$str = join "", @phrases;
$str =~ s/ï¼$//;
$str =~ s/^ã(.+)ã$/$1/;
if (rand > 0.5) {
$str =~ s/(ï¼)â¦â¦/$1/gs;
} else {
$str =~ s/ï¼(â¦â¦)/$1/gs;
}
return $str;
}
1;
=head1 AUTHOR
Kang-min Liu <gugod@gugod.org>
=head1 COPYRIGHT
Copyright 2010- by Kang-min Liu, <gugod@gugod.org>
This program is free software; you can redistribute it a nd/or modify
it under the same terms as Perl itself.
See L<http://www.perl.com/perl/misc/Artistic.html>
=cut
# Data coming from Wikisource
# http://zh.wikisource.org/zh-hant/%E5%BF%98%E4%B8%8D%E4%BA%86%E7%9A%84%E9%81%8E%E5%B9%B4
__DATA__
#c9s
éä¸è³´ï¼éä¸è³´ãéä¸è³´ï¼éä¸è³´ï¼
# åé²
å¨ä¸åæä¸ï¼æ¯é»æçæä¸ï¼æé»çæ°£æ°ï¼æ¿æ¿å¯å¯æç©ºéå
å¡èï¼ä¸è®ææçå
æï¼æ¼å°å°å°ä¸ãé£é»æéå¨å¹¾ç¾å±¤çå°åºï¼ä¹æ¯ç¶é©ä¸å°ï¼æ¯æªæ¾æéé§äººç黿ã
ããå¨éè¢«é»ææå
å¡çå°ä¸ï¼æåå被æä»£æ¯è¦ªæéºæ£çå©ç«¥ãä»åç便·æäºä¸æï¼ä¸æå¾æ¯è¿½æ
ä¸è¿æ¯è¦ªçæ
æï¼èªå·±èµ°åºå®¶ä¾ï¼ä¹æ¯ä¸å徿¯æè¨ï¼è¢«éçå人ä¹åã
ããä»åä¸ç¥ç«çä»éº¼å°æ¹ï¼ä¹ä¸ç¥ä»éº¼æ¯æ¹åï¼ä¸ç¥ç«çå°é¢æ¯å¦ç©©åºï¼ä¹ä¸ç¥ç«çå卿¯å¦å±éªï¼å çºä¸çæé»ï¼ç¼ç已失äºä½ç¨ã
ããä»åå·²ç¶å¿å»äºä¸åï¼å¿è£¡ä¸æ·æ±é©æï¼ä¹ä¸å¸æ±æ
°å®ï¼åªæä¸ç¨®çç´è¦ºæ¯é
èä»åï¼ââåé²ï¼
ããä»åæå°æä¸ç¨®ï¼ä¸è¨±ä»åæ°¸ä¹
ç«ååä¸ä½ç½®çå¢åãä»åä¾¿ä¹æèæï¼å
åºå°ä¿¡è³´ãäºç¸ææï¼ç±æ¬è½çè¡åï¼åé¢çæåï¼é£ä¸ç¥å»èçåéï¼ç§»åèªå·±çè
³æ¥ãåé²ï¼ç²ç®å°åé²ï...
ç¡ç®çå°åé²ï¼èªç¶å¿è¨ä»åè¡ç¨çé è¿ï¼åªæ¯åé²ï¼äºç¸ä¿¡è³´ï¼äºç¸ææï¼çºèåé²èåé²ã
ããä»åæ²æå°æ±å
æä¹è·¯çæèï¼ä¹æ²æèµ°å°èªç±ä¹è·¯çæ
¾æï¼åªæ¯æé¢çæåèè¡ãç¤æ¥çç³é ï¼åºè
³çèæ£ï¼é·äººçæ³¥æ¾¤ï¼æººäººçæ°´çªªï¼ææä¸ååé²çé»ç¤åå±éªï¼å¨éé»æçµ±æ²»ä¹ä...
ããå¨ä»åèªå§å°±ç¡æè¦éµèãäººé¡æ¾ç¶è¡éä¹è·¡ãç念é ãå¨é黿ä¹ä¸ï¼ç«ä¹æ²æè¡ä¸åé²çäºï¼ééæäºé¡è¹¶ï¼ä¹ä¸è½ææ¢ä»åçåé²ãåé²ï¼å¿äºä¸åå±éªèåé²ã
ããå¨éæ¨£é»æä¹ä¸ï¼ææä¸åï¼ç¡æä¼å¨æ»ä¸è¬ç坿»
裡ï¼åªæé¢¨å
ççæ
æï¼é¨å¤ªå¤ªç好æï¼ç¹å¥çºä»ååå¥èé²è¡æ²ï¼åªæéæ¨è²å¨é黿䏿å±èï¼è¦ä»¥æ
°å®ä»åéä¸çå¯å¯ï¼æ
°åä...
( run in 3.578 seconds using v1.01-cache-2.11-cpan-b301d465b3d )