| 1 | #!/usr/bin/perl -X | ||
| 2 | package AI::MicroStructure::Relations; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use utf8; # for translator credits | ||
| 6 | use Data::Dumper; | ||
| 7 | use AI::MicroStructure::Cache; | ||
| 8 | use WWW::Wikipedia; | ||
| 9 | use JSON::XS; | ||
| 10 | use Statistics::Basic qw(:all); | ||
| 11 | use Digest::MD5 qw(md5_hex); | ||
| 12 | #use AI::MicroStructure::Memd; | ||
| 13 | use Cache::Memcached::Fast; | ||
| 14 | |||
| 15 | our $memd = new Cache::Memcached::Fast({ | ||
| 16 | servers => [ { address => 'localhost:11211', weight => 2.5 }], | ||
| 17 | namespace => 'my:', | ||
| 18 | connect_timeout => 0.2, | ||
| 19 | io_timeout => 0.1, | ||
| 20 | close_on_error => 1, | ||
| 21 | compress_threshold => 100_000, | ||
| 22 | compress_ratio => 0.9, | ||
| 23 | max_failures => 1, | ||
| 24 | max_size => 512 * 1024, | ||
| 25 | }); | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | our $supports = {}; | ||
| 30 | our $scale = 1; | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | my $class = shift; | ||
| 34 | |||
| 35 | no strict 'refs'; | ||
| 36 | my $self = bless { @_,storage=> AI::MicroStructure::Cache->new, | ||
| 37 | # memd=>AI::MicroStructure::Memd->new(), | ||
| 38 | cache=>{}, | ||
| 39 | dominant => [] }, $class; | ||
| 40 | |||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub gofor { | ||
| 45 | my $self = shift; | ||
| 46 | my $next = shift; | ||
| 47 | my $opt = shift; | ||
| 48 | |||
| 49 | my $store={}; | ||
| 50 | $store = $memd->get(sprintf("micro-relation%s",md5_hex($next))); | ||
| 51 | if(!$store){ | ||
| 52 | |||
| 53 | my $wiki = WWW::Wikipedia->new(); | ||
| 54 | my $result = $wiki->search("$next"); | ||
| 55 | |||
| 56 | |||
| 57 | if(defined($result) and $result){ | ||
| 58 | |||
| 59 | |||
| 60 | my $micosense = `micro-sense $next`; | ||
| 61 | |||
| 62 | if($micosense =~ /[{]/){ | ||
| 63 | my $sense = JSON::XS->new->pretty(1)->decode($micosense); | ||
| 64 | |||
| 65 | push @{$self->{storage}->{category}},{$next=>$sense} unless(!@{$sense->{senses}}); | ||
| 66 | } | ||
| 67 | $self->{storage}->{all}->{$_} = $result->{$_} for qw(categories | ||
| 68 | headings | ||
| 69 | currentlang); | ||
| 70 | |||
| 71 | $self->{storage}->{related}->{$next} = [grep{!/\(/}map{$_=~ s/ /_/g; $_=ucfirst $_;} $result->related()]; | ||
| 72 | |||
| 73 | |||
| 74 | foreach my $elem ($result->related()){ | ||
| 75 | |||
| 76 | $elem =~ s/ /_/g; | ||
| 77 | |||
| 78 | $self->{storage}->{data}->{$elem} = defined($self->{storage}->{data}->{$elem}) ? | ||
| 79 | $self->{storage}->{data}->{$elem}+1:1; | ||
| 80 | |||
| 81 | |||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | $memd->set(sprintf("micro-relation%s",md5_hex($next)),$self->{storage}); | ||
| 86 | return $self->{storage}; | ||
| 87 | } | ||
| 88 | return $store; | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | sub inspect { | ||
| 93 | |||
| 94 | |||
| 95 | my $self = shift; | ||
| 96 | my $times = shift; | ||
| 97 | |||
| 98 | |||
| 99 | foreach(sort {$a cmp $b} keys %{$self->{storage}->{data}} ){ | ||
| 100 | |||
| 101 | |||
| 102 | $self->{storage}->{dominant}->{$self->{storage}->{data}->{$_}} = $_; | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | $self->{storage}->insert($self->{storage}); | ||
| 107 | return $self->{storage}; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | 1; | ||
| 112 | # ABSTRACT: this is part of AI-MicroStructure the package handels relations between concepts | ||
| 113 | |||
| 114 | |||
| 115 | =head1 NAME | ||
| 116 | |||
| 117 | AI::MicroStructure::Relations | ||
| 118 | |||
| 119 | =head1 DESCRIPTION | ||
| 120 | |||
| 121 | Gets Relations for Concepts based on words | ||
| 122 | |||
| 123 | =head1 SYNOPSIS | ||
| 124 | |||
| 125 | |||
| 126 | ~$ micro new world | ||
| 127 | |||
| 128 | ~$ micro structures | ||
| 129 | |||
| 130 | ~$ micro any 2 | ||
| 131 | |||
| 132 | ~$ micro drop world | ||
| 133 | |||
| 134 | ~$ micro | ||
| 135 | |||
| 136 | |||
| 137 | =head1 AUTHOR | ||
| 138 | |||
| 139 | Hagen Geissler <santex@cpan.org> | ||
| 140 | |||
| 141 | =head1 COPYRIGHT AND LICENCE | ||
| 142 | |||
| 143 | Hagen Geissler <santex@cpan.org> | ||
| 144 | |||
| 145 | =head1 SUPPORT AND DOCUMENTATION | ||
| 146 | |||
| 147 | ☞ [sample using concepts](http://quantup.com) | ||
| 148 | |||
| 149 | ☞ [PDF info on my works](https://github.com/santex) | ||
| 150 | |||
| 151 | |||
| 152 | =head1 SEE ALSO | ||
| 153 | |||
| 154 | AI-MicroStructure | ||
| 155 | AI-MicroStructure-Cache | ||
| 156 | AI-MicroStructure-Deamon | ||
| 157 | AI-MicroStructure-Relations | ||
| 158 | AI-MicroStructure-Concept | ||
| 159 | AI-MicroStructure-Data | ||
| 160 | AI-MicroStructure-Driver | ||
| 161 | AI-MicroStructure-Plugin-Pdf | ||
| 162 | AI-MicroStructure-Plugin-Twitter | ||
| 163 | AI-MicroStructure-Plugin-Wiki | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | __DATA__ | ||
| 168 |