| 1 | #!/usr/bin/perl | ||
| 2 | package AI::MicroStructure::Context; | ||
| 3 | use strict; | ||
| 4 | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); | ||
| 5 | |||
| 6 | require Carp; | ||
| 7 | require Symbol; | ||
| 8 | require Exporter; | ||
| 9 | require DynaLoader; | ||
| 10 | use Data::Printer; | ||
| 11 | use Data::Dumper; | ||
| 12 | use Search::ContextGraph; | ||
| 13 | use Statistics::Basic qw(:all); | ||
| 14 | |||
| 15 | @ISA = qw(Exporter DynaLoader); | ||
| 16 | |||
| 17 | @EXPORT = (); | ||
| 18 | |||
| 19 | @EXPORT_OK = (); | ||
| 20 | |||
| 21 | %EXPORT_TAGS = (all => [@EXPORT_OK]); | ||
| 22 | |||
| 23 | $VERSION = "0.1"; | ||
| 24 | |||
| 25 | |||
| 26 | ## bootstrap AI::MicroStructure::Context $VERSION; | ||
| 27 | |||
| 28 | my $Class = __PACKAGE__; ## This class's name | ||
| 29 | my $Table = $Class . '::'; ## This class's symbol table | ||
| 30 | |||
| 31 | my $Count = 0; ## Counter for generating unique names for all locations | ||
| 32 | my $Alive = 1; ## Flag for disabling auto-dump during global destruction | ||
| 33 | |||
| 34 | sub _usage_ | ||
| 35 | { | ||
| 36 | my($text) = @_; | ||
| 37 | |||
| 38 | Carp::croak("Usage: $text"); | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub new { | ||
| 43 | my ( $class, @tools ) = ( @_ ); | ||
| 44 | |||
| 45 | |||
| 46 | my $self = bless {tools => @_ , | ||
| 47 | micro => AI::MicroStructure->new(), | ||
| 48 | graph => {content=>Search::ContextGraph->new()}}, $class; | ||
| 49 | |||
| 50 | $self->{structures} = [$self->{micro}->structures]; | ||
| 51 | |||
| 52 | return $self; | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | sub retrieveIndex { | ||
| 59 | |||
| 60 | my $self = shift; | ||
| 61 | my $in = shift; | ||
| 62 | |||
| 63 | if(!$in) { | ||
| 64 | |||
| 65 | Carp::croak("Usage: provide path") unless($self->{micro}->{state}->{path}->{"cwd/structures"}); | ||
| 66 | |||
| 67 | $in = $self->{micro}->{state}->{path}->{"cwd/structures"} unless($in); | ||
| 68 | } | ||
| 69 | |||
| 70 | $self->{graph}->{content} =$self->{graph}->{content}->load_from_dir($in); | ||
| 71 | |||
| 72 | # $self->{graph}->{content} = $self->retrieve($in); | ||
| 73 | #$g->retrieve($in); | ||
| 74 | |||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | sub storeIndex { | ||
| 79 | |||
| 80 | my $self = shift; | ||
| 81 | my $in = shift; | ||
| 82 | $in = "stored.cng" unless($in); | ||
| 83 | |||
| 84 | $self->{graph}->{content}->store( $in ); | ||
| 85 | |||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | sub training_docs { | ||
| 92 | |||
| 93 | |||
| 94 | my $self = shift; | ||
| 95 | |||
| 96 | my ($i,$prop) = (); | ||
| 97 | |||
| 98 | my $cat={}; | ||
| 99 | foreach my $th (@{$self->{structures}}) | ||
| 100 | { | ||
| 101 | |||
| 102 | $prop = sprintf(`micro $th all`); | ||
| 103 | |||
| 104 | $cat->{sprintf("%s",$th)}={categories => [$th],content =>$prop}; | ||
| 105 | |||
| 106 | $i++; | ||
| 107 | } | ||
| 108 | |||
| 109 | return $cat; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | sub play { | ||
| 115 | |||
| 116 | my $self = shift; | ||
| 117 | |||
| 118 | my ($style,$in) = @_; | ||
| 119 | |||
| 120 | $style->{$in}->{structs} = [$self->{graph}->{content}->search($self->metaArg($in,$style))]; | ||
| 121 | |||
| 122 | return $style; | ||
| 123 | |||
| 124 | }; | ||
| 125 | |||
| 126 | sub metaArg { | ||
| 127 | |||
| 128 | my $self = shift; | ||
| 129 | my $in = shift; | ||
| 130 | my $style = shift; | ||
| 131 | |||
| 132 | my @metaArg = (); | ||
| 133 | |||
| 134 | $style->{explicit} = 0 unless($style->{explicit}); | ||
| 135 | |||
| 136 | return split(" ",$style->{explicit} ? $in : `micro all $in`); | ||
| 137 | |||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | sub getOverAvg { | ||
| 143 | |||
| 144 | my $self = shift; | ||
| 145 | |||
| 146 | my $style = shift; | ||
| 147 | |||
| 148 | my ($payload) = @_; | ||
| 149 | |||
| 150 | my @files = keys %$payload; | ||
| 151 | my @scores = values %$payload; | ||
| 152 | my $avgscrore= mean(@scores); | ||
| 153 | |||
| 154 | |||
| 155 | foreach my $file ( sort { $a cmp $b } @files ) { | ||
| 156 | printf("%s=%s\n",$file, | ||
| 157 | $payload->{$file}) | ||
| 158 | unless(($payload->{$file}*$payload->{modifier})<=$avgscrore); | ||
| 159 | |||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | sub intersect { | ||
| 164 | |||
| 165 | my $self = shift; | ||
| 166 | |||
| 167 | my ($style,$in) = @_; | ||
| 168 | |||
| 169 | my @in = $self->metaArg($in,$style); | ||
| 170 | |||
| 171 | return [$self->{graph}->{content}->intersection( terms => [@in] )]; | ||
| 172 | |||
| 173 | |||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | sub similar { | ||
| 178 | |||
| 179 | |||
| 180 | my $self = shift; | ||
| 181 | |||
| 182 | my ($style,$in) = @_; | ||
| 183 | |||
| 184 | |||
| 185 | my @in = $self->metaArg($in,$style); | ||
| 186 | |||
| 187 | my @ranked_docs = $self->{graph}->{content}->simple_search(@in); | ||
| 188 | |||
| 189 | return [$self->{graph}->{content}->find_similar(@ranked_docs)]; | ||
| 190 | |||
| 191 | # return @in; | ||
| 192 | |||
| 193 | } | ||
| 194 | |||
| 195 | sub simpleMixedSearch { | ||
| 196 | |||
| 197 | |||
| 198 | |||
| 199 | my $self = shift; | ||
| 200 | |||
| 201 | my ($style,$in) = @_; | ||
| 202 | |||
| 203 | my @in = $self->metaArg($in,$style); | ||
| 204 | |||
| 205 | my @ranked_docs = $self->{graph}->{content}->simple_search(@in); | ||
| 206 | |||
| 207 | |||
| 208 | p @ranked_docs; | ||
| 209 | |||
| 210 | my( $docs, $words ) =$self->{graph}->{content}->mixed_search( {documents => [@ranked_docs], | ||
| 211 | terms => [@in] | ||
| 212 | } ); | ||
| 213 | |||
| 214 | |||
| 215 | p $docs; | ||
| 216 | p $words; | ||
| 217 | |||
| 218 | |||
| 219 | $words->{modifier} = 0.1; | ||
| 220 | $docs->{modifier} = 0.1; | ||
| 221 | |||
| 222 | $self->getOverAvg($words); | ||
| 223 | $self->getOverAvg($docs); | ||
| 224 | |||
| 225 | return 1; | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | 1; | ||
| 230 | |||
| 231 | |||
| 232 | =head1 NAME | ||
| 233 | |||
| 234 | AI::MicroStructure::Context | ||
| 235 | |||
| 236 | =head1 DESCRIPTION | ||
| 237 | |||
| 238 | Context driven scoreing | ||
| 239 | |||
| 240 | =head1 SYNOPSIS | ||
| 241 | |||
| 242 | ~$ micro new world | ||
| 243 | |||
| 244 | ~$ micro structures | ||
| 245 | |||
| 246 | ~$ micro any 2 | ||
| 247 | |||
| 248 | ~$ micro drop world | ||
| 249 | |||
| 250 | ~$ micro | ||
| 251 | |||
| 252 | =head1 AUTHOR | ||
| 253 | |||
| 254 | Hagen Geissler <santex@cpan.org> | ||
| 255 | |||
| 256 | =head1 COPYRIGHT AND LICENCE | ||
| 257 | |||
| 258 | Hagen Geissler <santex@cpan.org> | ||
| 259 | |||
| 260 | =head1 SUPPORT AND DOCUMENTATION | ||
| 261 | |||
| 262 | [sample using concepts](http://quantup.com) | ||
| 263 | |||
| 264 | [PDF info on my works](https://github.com/santex) | ||
| 265 | |||
| 266 | |||
| 267 | =head1 SEE ALSO | ||
| 268 | |||
| 269 | AI-MicroStructure | ||
| 270 | |||
| 271 | =cut |