#!/usr/bin/perl use strict; use warnings; use Text::Levenshtein qw/fastdistance/; use Data::Dumper; my $classes = []; my $index = {}; my $limit = 0.5; sub avg_distance { my ($new_word, $class) = @_; my $distance = 0; for my $word (@$class) { $distance += fastdistance($new_word, $word); } return $distance / @$class; } sub put_or_create { my ($w) = @_; $w =~ s/[^a-z]//g; my $found=0; return if exists $index->{$w}; foreach my $class ( @$classes ) { if((avg_distance($w,$class)/length($w)) < $limit) { push @$class, $w; $index->{$w} = $class; $found = 1; last; }; }; if(! $found) { my $class = [$w]; push @$classes, $class; $index->{$w} = $class; }; }; #watch for an error opening the file open( my $file,"<", "possible_locations.txt") or die "Failed to open file: $!"; my @locations = <$file>; #remove the line endings chomp(@locations); my $count = 0; for (@locations) { #last if ++$count/@locations > 0.2;#not all put_or_create($_) } for (@$classes) { print join(",", sort @$_), "\n"; }