| 1 | =head1 NAME | ||
| 2 | |||
| 3 | EBI::FGPT::FuzzyRecogniser::OntologyTerm::Annotation | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | EBI::FGPT::FuzzyRecogniser::OntologyTerm::Annotation contains two fields- value | ||
| 8 | and normalised_value | ||
| 9 | |||
| 10 | my $annotationToMatch = | ||
| 11 | EBI::FGPT::FuzzyRecogniser::OntologyTerm::Annotation->new( value => $string_to_match ); | ||
| 12 | |||
| 13 | |||
| 14 | =head2 METHODS | ||
| 15 | |||
| 16 | =over | ||
| 17 | |||
| 18 | =item normalise() | ||
| 19 | |||
| 20 | Normalises a string by changing it lowercase and | ||
| 21 | splitting into 4-grams. | ||
| 22 | |||
| 23 | =back | ||
| 24 | |||
| 25 | =over | ||
| 26 | |||
| 27 | =item compare() | ||
| 28 | |||
| 29 | Counts how many n-grmas are shared between annotations | ||
| 30 | |||
| 31 | =back | ||
| 32 | |||
| 33 | =head1 AUTHOR | ||
| 34 | |||
| 35 | Emma Hastings , <ehastings@cpan.org> | ||
| 36 | |||
| 37 | =head1 ACKNOWLEDGEMENTS | ||
| 38 | |||
| 39 | Tomasz Adamusiak <tomasz@cpan.org> | ||
| 40 | |||
| 41 | =head1 COPYRIGHT AND LICENSE | ||
| 42 | |||
| 43 | Copyright [2011] EMBL - European Bioinformatics Institute | ||
| 44 | |||
| 45 | Licensed under the Apache License, Version 2.0 (the | ||
| 46 | "License"); you may not use this file except in compliance | ||
| 47 | with the License. You may obtain a copy of the License at | ||
| 48 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 49 | Unless required by applicable law or agreed to in writing, | ||
| 50 | software distributed under the License is distributed on an | ||
| 51 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
| 52 | either express or implied. See the License for the specific | ||
| 53 | language governing permissions and limitations under the | ||
| 54 | License. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | package EBI::FGPT::FuzzyRecogniser::OntologyTerm::Annotation; | ||
| 59 | |||
| 60 | |||
| 61 | use Moose; | ||
| 62 | use List::Util qw{min max}; | ||
| 63 | |||
| 64 | our $VERSION = 0.09; | ||
| 65 | |||
| 66 | has 'value' => ( | ||
| 67 | is => 'rw', | ||
| 68 | isa => 'Str', | ||
| 69 | ); | ||
| 70 | |||
| 71 | has 'normalised_value' => ( | ||
| 72 | is => 'ro', | ||
| 73 | isa => 'HashRef', | ||
| 74 | lazy_build => 1 | ||
| 75 | ); | ||
| 76 | |||
| 77 | sub _build_normalised_value { | ||
| 78 | my $self = shift; | ||
| 79 | return normalise( $self->value ); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub normalise { | ||
| 83 | my $string = shift; | ||
| 84 | $string = lc($string); | ||
| 85 | my $q = 4; | ||
| 86 | |||
| 87 | my $ngram; | ||
| 88 | |||
| 89 | # pad the string | ||
| 90 | for ( 1 .. $q - 1 ) { | ||
| 91 | $string = '^' . $string; | ||
| 92 | $string = $string . '$'; | ||
| 93 | } | ||
| 94 | |||
| 95 | # split ito ngrams | ||
| 96 | for my $i ( 0 .. length($string) - $q ) { | ||
| 97 | $ngram->{ substr( $string, $i, $q ) }++; | ||
| 98 | } | ||
| 99 | |||
| 100 | return $ngram; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub compare($$) { | ||
| 104 | # passed string_to_match wrapped in an annotation object | ||
| 105 | my ( $self, $annotationToMatch ) = @_; | ||
| 106 | #retrieve normalised hashes for the string values of the annotations | ||
| 107 | my $template = $self->normalised_value(); | ||
| 108 | my $normalised_annotationToMatch = $annotationToMatch->normalised_value(); | ||
| 109 | |||
| 110 | my $ngrams_matched = 0; | ||
| 111 | #count ngrams shared between this annotation and the annotation to match | ||
| 112 | for my $template_ngram ( keys %{$template} ) { | ||
| 113 | $ngrams_matched++ if exists $normalised_annotationToMatch->{$template_ngram}; | ||
| 114 | } | ||
| 115 | |||
| 116 | # normalise | ||
| 117 | return | ||
| 118 | int( | ||
| 119 | $ngrams_matched / max( scalar keys %$template, scalar keys %$normalised_annotationToMatch ) | ||
| 120 | * 100 ); | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; # End of EBI::FGPT::FuzzyRecogniser::OntologyTerm::Annotation |