0525. [Perl] (hash array) txt 파일에서 특정단어 다음 나오는 단어 뽑아내기
#!/usr/bin/perl
use strict;
sub build_bigram {
my ($word, $pre);
my @words;
my %bigram;
while(<>){
@words=split(/\s+/,$_);
foreach $word (@words) {
push(@{$bigram{$pre}}, $word);
$pre=$word;
}
}
#return @{$bigram{$pre}};
return %bigram;
}
MAIN: {
my %bigram;
my @candidates;
my ($pre, $choice, $i);
%bigram=build_bigram();
$pre="device";
print "device ";
for ($i=1; $i<100; $i++){
@candidates=@{$bigram{$pre}};
$choice=$candidates[int(rand($#candidates))];
print $choice . " ";
$pre=$choice;
}
print "\n";
}
[root@linuxtest kim]# ./ranvi.pl < messages
device strings: Mfr=1, Product=2, SerialNumber=0 Oct 7 10:32:41 linuxtest kernel: usb 7-1: new low speed USB OPTICAL MOUSE Oct 11 05:19:47 linuxtest kernel: usb 7-1: Manufacturer: PIXART Oct 6 22:43:33 linuxtest kernel: usb 7-1: New USB OPTICAL MOUSE as /devices/pci0000:00/0000:00:1d.1/usb7/7-1/7-1:1.0/input/input2999 Oct 6 07:34:48 linuxtest kernel: generic-usb 0003:093A:2510.06CE: input,hidraw2: USB device found, idVendor=093a, idProduct=2510 Oct 11 08:53:51 linuxtest kernel: generic-usb 0003:093A:2510.413B: input,hidraw2: USB device number 12 using uhci_hcd Oct 9 05:23:16 linuxtest kernel: usb 7-1: USB OPTICAL MOUSE] on usb-0000:00:1d.1-1/input0 Oct 6 15:17:57 linuxtest kernel: usb 7-1: configuration #1 chosen from 1 choice Oct 8 02:16:09 linuxtest kernel: usb 7-1: New
[root@linuxtest kim]#
'IT' 카테고리의 다른 글
0527. [Perl] (CPAN) install module 확인하기 (0) | 2016.03.04 |
---|---|
0526. [Perl] (CPAN) CPAN 설치하기 (0) | 2016.03.04 |
0524. [Perl] (hash array) hash 의 한 요소를 무명배열 array 로 사용하기 (0) | 2016.03.04 |
0523. [Perl] (무명배열) 무명배열 array (0) | 2016.03.04 |
0522. [Perl] (reference) array reference dereference 배열 레퍼런스 디레퍼런스 (0) | 2016.03.04 |