| 1 | package Clusterize; | ||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use Clusterize::Pattern; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | our $VERSION = '0.02'; | |||||||||||
| 6 | |||||||||||||
| 7 | 0 | ▶ | sub pair { | ||||||||||
|
Subroutine
pair: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 8 | 0 | my ($self, $key, $val) = @_; | |||||||||||
| 9 | 0 | ▶ | $self->{pairs}{$key} = $val if defined $val; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 10 | 0 | $self->{pairs}{$key}; | |||||||||||
| 11 | } | ||||||||||||
| 12 | |||||||||||||
| 13 | 0 | ▶ | sub delete_pair { | ||||||||||
|
Subroutine
delete_pair: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 14 | 0 | my ($self, $key) = @_; | |||||||||||
| 15 | 0 | delete $self->{pairs}{$key}; | |||||||||||
| 16 | } | ||||||||||||
| 17 | |||||||||||||
| 18 | 0 | ▶ | sub cluster_list { keys %{shift->{clusters}} } | ||||||||||
|
Subroutine
cluster_list: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 19 | 0 | ▶ | sub remove_cluster_pair { | ||||||||||
|
Subroutine
remove_cluster_pair: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 20 | 0 | my ($self, $digest, $key) = @_; | |||||||||||
| 21 | 0 | delete $self->{clusters}{$digest}{$key}; | |||||||||||
| 22 | delete $self->{clusters}{$digest} | ||||||||||||
| 23 | 0 | ▶ | unless %{$self->{clusters}{$digest}}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 24 | } | ||||||||||||
| 25 | |||||||||||||
| 26 | 0 | ▶ | sub add_cluster_pair { | ||||||||||
|
Subroutine
add_cluster_pair: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 27 | 0 | my ($self, $digest, $pair) = @_; | |||||||||||
| 28 | 0 | $self->{clusters}{$digest}{$pair->{key}} = $pair->{val}; | |||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | 0 | ▶ | sub cluster_pairs { | ||||||||||
|
Subroutine
cluster_pairs: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 32 | 0 | my ($self, $digest) = @_; | |||||||||||
| 33 | 0 | return $self->{clusters}{$digest}; | |||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | 1 | ▶ | sub new { return bless {}, shift } | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 37 | 0 | ▶ | sub add_pair { | ||||||||||
|
Subroutine
add_pair: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 38 | 0 | my ($self, $key, $digest) = @_; | |||||||||||
| 39 | 0 | ▶ | return if $self->pair($key); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 0 | ▶ | $digest = Clusterize::Pattern->text2digest($digest) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | if ref $digest eq 'ARRAY'; | ||||||||||||
| 42 | 0 | $self->pair($key, $digest); | |||||||||||
| 43 | 0 | for (keys %{$digest}) { | |||||||||||
| 44 | 0 | $self->add_cluster_pair($_, {key => $key, val => $digest->{$_}}); | |||||||||||
| 45 | } | ||||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 0 | ▶ | sub remove_pair { | ||||||||||
|
Subroutine
remove_pair: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 49 | 0 | my ($self, $key) = @_; | |||||||||||
| 50 | 0 | ▶ | my $cluster_pair = $self->pair($key) || return; | ||||||||||
|
Condition0%0/2
$self->pair($key) || (return)
MC/DC0%0/1
$self->pair($key) || (return)
$self->pair($key)
Truth table0%0/2
A
A= $self->pair($key)
| |||||||||||||
| 51 | 0 | for (keys %{$cluster_pair}) {$self->remove_cluster_pair($_, $key)} | |||||||||||
| 52 | 0 | $self->delete_pair($key); | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | 0 | ▶ | sub list { | ||||||||||
|
Subroutine
list: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 56 | 0 | my ($self, $opt) = @_; | |||||||||||
| 57 | 0 | my (%md5, @clusters); | |||||||||||
| 58 | 0 | for (map { Clusterize::Pattern->new($self->cluster_pairs($_)) } | |||||||||||
| 59 | $self->cluster_list($opt)) { | ||||||||||||
| 60 | 0 | ▶ | next if $md5{$_->digest}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 0 | $md5{$_->digest} = 1; | |||||||||||
| 62 | 0 | push @clusters, $_ | |||||||||||
| 63 | } | ||||||||||||
| 64 | 0 | @clusters; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 3 | 1; | |||||||||||
| 68 | |||||||||||||
| 69 | =head1 NAME | ||||||||||||
| 70 | |||||||||||||
| 71 | Clusterize - clustering text documents. | ||||||||||||
| 72 | |||||||||||||
| 73 | =head1 VERSION | ||||||||||||
| 74 | |||||||||||||
| 75 | Version 0.02 | ||||||||||||
| 76 | |||||||||||||
| 77 | =head1 SYNOPSIS | ||||||||||||
| 78 | |||||||||||||
| 79 | use Clusterize; | ||||||||||||
| 80 | |||||||||||||
| 81 | my %pairs = ( | ||||||||||||
| 82 | key1 => [ string1, string2, ...stringN ], | ||||||||||||
| 83 | key2 => [ string5, string6, ...stringM ], | ||||||||||||
| 84 | ... | ||||||||||||
| 85 | keyN => [ ... ], | ||||||||||||
| 86 | ); | ||||||||||||
| 87 | |||||||||||||
| 88 | my $clusterize = Clusterize->new(); | ||||||||||||
| 89 | while (my @pair = each %files) { $clusterize->add_pair(@pair) } | ||||||||||||
| 90 | |||||||||||||
| 91 | foreach my $c ( $clusterize->list ) { | ||||||||||||
| 92 | printf "# /%s/ (digest=%s) (accuracy=%.3f) (size=%d)", | ||||||||||||
| 93 | $c->pattern, $digest, $c->accuracy, $c->size; | ||||||||||||
| 94 | my $pairs = $c->pairs; | ||||||||||||
| 95 | for ( keys %{$pairs} ) { print $_." ".$pairs->{$_} } | ||||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | |||||||||||||
| 99 | =head1 DESCRIPTION | ||||||||||||
| 100 | |||||||||||||
| 101 | B<Clusterize> module implements specific algorithm for clustering text documents. | ||||||||||||
| 102 | |||||||||||||
| 103 | =head1 PUBLIC METHODS | ||||||||||||
| 104 | |||||||||||||
| 105 | =head2 new | ||||||||||||
| 106 | |||||||||||||
| 107 | This is the constructor. No parameter is required. | ||||||||||||
| 108 | |||||||||||||
| 109 | =head2 add_pair | ||||||||||||
| 110 | |||||||||||||
| 111 | This method is used to add new document into cluster set: | ||||||||||||
| 112 | |||||||||||||
| 113 | $clusterize->add_pair($key, [$string1, $string2, ...]); | ||||||||||||
| 114 | |||||||||||||
| 115 | $key - is uniq name of the document (e.g. filename), | ||||||||||||
| 116 | [$string1, $string2, ...] - text of the document. | ||||||||||||
| 117 | |||||||||||||
| 118 | =head2 remove_pair | ||||||||||||
| 119 | |||||||||||||
| 120 | This method is used to remove document from cluster set: | ||||||||||||
| 121 | |||||||||||||
| 122 | $clusterize->remove_pair($key); | ||||||||||||
| 123 | |||||||||||||
| 124 | $key - is name of the document (e.g. filename). | ||||||||||||
| 125 | |||||||||||||
| 126 | =head2 list | ||||||||||||
| 127 | |||||||||||||
| 128 | This method is used to get list of built clusters: | ||||||||||||
| 129 | |||||||||||||
| 130 | my @clusters = $clusterize->list(); | ||||||||||||
| 131 | |||||||||||||
| 132 | Returns list of B<Clusterize::Pattern> objects with the following attributes: | ||||||||||||
| 133 | |||||||||||||
| 134 | $c->pattern - regexp that matches all strings in the given cluster; | ||||||||||||
| 135 | |||||||||||||
| 136 | $c->accuracy - this value reflects how similar strings in the cluster (value from 0 to 1); | ||||||||||||
| 137 | |||||||||||||
| 138 | $c->size - how many documents in the cluster; | ||||||||||||
| 139 | |||||||||||||
| 140 | $c->digest - MD5 digest of the cluster to identify duplicate clusters; | ||||||||||||
| 141 | |||||||||||||
| 142 | $c->pairs - list of { key => $key1, val => $val1 } hash pairs, where: | ||||||||||||
| 143 | key - is name of document, val - is string from 'key' document; | ||||||||||||
| 144 | |||||||||||||
| 145 | =head1 AUTHOR | ||||||||||||
| 146 | |||||||||||||
| 147 | Slava Moiseev, <slava.moiseev@yahoo.com> | ||||||||||||
| 148 | |||||||||||||