| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use File::Find::Rule; | ||
| 4 | use File::Temp qw(tempdir); | ||
| 5 | use IO::File; | ||
| 6 | use HTML::Entities; | ||
| 7 | use Data::Printer; | ||
| 8 | use Data::Dumper; | ||
| 9 | use Parallel::ForkManager; | ||
| 10 | use AI::MicroStructure; | ||
| 11 | use Getopt::Long; | ||
| 12 | use JSON; | ||
| 13 | |||
| 14 | use JSON::XS; | ||
| 15 | use Storable qw(lock_store lock_retrieve); | ||
| 16 | my $pm = Parallel::ForkManager->new(8); | ||
| 17 | sub nicefy { return reverse sort {length($a) <=> length($b)}@_ } | ||
| 18 | our $cache = {}; | ||
| 19 | our $kkey = sprintf "%s",join("_",@ARGV); | ||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | BEGIN{ | ||
| 25 | |||
| 26 | use Try::Tiny; | ||
| 27 | eval { | ||
| 28 | local $^W = 0; # because otherwhise doesn't pass errors | ||
| 29 | |||
| 30 | |||
| 31 | use vars qw($cache $micro $result $curSysDate %opts $results); | ||
| 32 | use Cache::Memcached::Fast; | ||
| 33 | our $memd = new Cache::Memcached::Fast({ | ||
| 34 | servers => [ { address => 'localhost:11211', weight => 2.5 }], | ||
| 35 | namespace => 'my:', | ||
| 36 | connect_timeout => 0.2, | ||
| 37 | io_timeout => 0.1, | ||
| 38 | close_on_error => 1, | ||
| 39 | compress_threshold => 100_000, | ||
| 40 | compress_ratio => 0.9, | ||
| 41 | max_failures => 1, | ||
| 42 | max_size => 512 * 1024, | ||
| 43 | }); | ||
| 44 | |||
| 45 | my $ping = `ping -c 1 google.com | grep -c trans`; | ||
| 46 | if(!$ping) | ||
| 47 | { | ||
| 48 | warn($ping); | ||
| 49 | } | ||
| 50 | |||
| 51 | $curSysDate = `date +"%Y%m%d"`; | ||
| 52 | $curSysDate=~ s/\n//g; | ||
| 53 | %opts = ( | ||
| 54 | match => `micro | cut -f 1`, | ||
| 55 | max_cache_age => 18000, | ||
| 56 | cache_file => sprintf("%s/realtime_%s.cache", | ||
| 57 | "/tmp", | ||
| 58 | $curSysDate), | ||
| 59 | cache => 1, | ||
| 60 | ); | ||
| 61 | GetOptions (\%opts, "match=s", | ||
| 62 | "query=s", | ||
| 63 | "max_cache_age=i", | ||
| 64 | "cache_file=s", | ||
| 65 | "cache!", | ||
| 66 | ); | ||
| 67 | if(!`ls $opts{cache_file}`){ | ||
| 68 | lock_store({},$opts{cache_file}); | ||
| 69 | } | ||
| 70 | $cache = lock_retrieve($opts{cache_file}); | ||
| 71 | $cache = {} unless $cache; | ||
| 72 | }; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | #micro-dict share/apps/kcookiejar/cookies | data-freq | sort -n | egrep -v ": $" | ||
| 77 | my @structures = map{encode_entities($_) } AI::MicroStructure->new()->structures(); | ||
| 78 | |||
| 79 | printf("%s",join("\n",nicefy @structures)); | ||
| 80 | @structures = grep{m/$opts{match}/i}@structures; | ||
| 81 | push @structures,$opts{match}; | ||
| 82 | p @structures; | ||
| 83 | |||
| 84 | for ( 0 ..$#structures) { | ||
| 85 | |||
| 86 | my $micro = $structures[$_]; | ||
| 87 | |||
| 88 | $pm->start and next; | ||
| 89 | |||
| 90 | alarm 60; # <--- | ||
| 91 | |||
| 92 | |||
| 93 | $cache->{$micro}->{structure} = $micro; | ||
| 94 | $cache->{$micro}->{names} = [nicefy split "\n",`micro "$micro" all`]; | ||
| 95 | $cache->{$micro}->{names} = [ @{$cache->{$micro}->{names}}]; | ||
| 96 | $cache->{$micro}->{relation} = [nicefy split "\n",`micro-relation $micro `]; | ||
| 97 | |||
| 98 | my @lower = split(/\W/,$micro); | ||
| 99 | p @lower; | ||
| 100 | push @{$cache->{$micro}->{bilangual}}, [nicefy split "\n",`bash /home/santex/bin/sex.sh "$_"`] for @{$cache->{$micro}->{names}}; | ||
| 101 | my $file1; | ||
| 102 | $file1 .= sprintf join(" ",@{$cache->{$micro}->{$_}}) for qw(bilangual relation names); | ||
| 103 | |||
| 104 | |||
| 105 | $cache->{$micro}->{category} = [split "\n",`getcat $micro`]; | ||
| 106 | my $sear = $micro ; | ||
| 107 | $sear=~ s/_/ /g; | ||
| 108 | $cache->{$micro}->{search} = [find( file => | ||
| 109 | name =>["*".$sear."*.*" ], | ||
| 110 | size => '>0k', | ||
| 111 | in => [@INC])]; | ||
| 112 | |||
| 113 | my $encoder = JSON->new->allow_blessed->pretty(1); | ||
| 114 | my $json = $encoder->encode($cache); | ||
| 115 | #print $fh $json; | ||
| 116 | #close $fh; | ||
| 117 | open defined(my $fh), ">", sprintf("%s/cache.json",$cache->{ "tempdir"}); | ||
| 118 | print $fh $json; | ||
| 119 | close $fh; | ||
| 120 | |||
| 121 | my $cmd = sprintf("'micro-dict %s/cache.json 1 | data-freq | sort -n",$cache->{ "tempdir"}) ; | ||
| 122 | print `$cmd`; | ||
| 123 | |||
| 124 | |||
| 125 | my %hash; | ||
| 126 | foreach my $line (@{$cache->{$micro}->{names}}){ | ||
| 127 | while ($file1 =~ m/$line/g){ | ||
| 128 | $cache->{freq}->{$line}++; | ||
| 129 | }} | ||
| 130 | $cache->{$micro}->freq = \%hash; | ||
| 131 | |||
| 132 | $pm->finish; } | ||
| 133 | $pm->wait_all_children(); | ||
| 134 | |||
| 135 | END{ | ||
| 136 | lock_store($cache,$opts{cache_file}); | ||
| 137 | p $cache; } |