Как я могу разбить несколько соединенных слов?
Может ли это сделать человек?
farsidebag far sidebag farside bag far side bag
Вам natural-language-processing нужно не только использовать strings словарь, вам, возможно, придется empty-string использовать статистический text-analysis подход, чтобы выяснить, что empty-string наиболее вероятно (или, не string-manipulation дай Бог, настоящий HMM для strings вашего человеческого языка str по выбору...)
Чтобы узнать, как natural-language сделать статистику, которая natural-language-processing может оказаться полезной, я text-analysis обращаюсь к доктору Питеру nlp Норвигу, который решает другую, но strings родственную проблему проверки string орфографии в 21 строке кода: http://norvig.com/spell-correct.html
(он немного natural-language-processing жульничает, складывая каждый string цикл for в одну строку... но natural-language-processing все же).
Обновление Это застряло у меня natural-language-processing в голове, так что пришлось nlp родить сегодня. Этот код str выполняет такое же разбиение, как string-manipulation описанное Робертом Гэмблом, но text-analysis затем он упорядочивает результаты empty-string на основе частоты слов в computational-linguistics предоставленном файле словаря computational-linguistics (который теперь должен быть computational-linguistics текстом, представляющим ваш string домен или английский язык natural-language в целом. Я использовал большой text-analysis .txt от Norvig, указанный str выше, и прикрепил к нему strings словарь, чтобы покрыть пропущенные str слова).
Комбинация из двух natural-language слов в большинстве случаев string лучше комбинации из 3 слов, если empty-string только разница в частоте text-analysis не огромна.
Я разместил этот код с небольшими изменениями в своем блоге
http://squarecog.wordpress.com/2008/10/19/splitting-words-joined-into-a-single-string/ а также немного text-analysis написал об ошибке недополнения natural-language-processing в этом коде. У меня возник text-analysis соблазн просто незаметно natural-language исправить это, но я решил, что string это может помочь некоторым text-analysis людям, которые раньше не text-analysis видели трюк с журналом: http://squarecog.wordpress.com/2009/01/10/dealing-with-underflow-in-joint-probability-calculations/
Вывод string-manipulation ваших слов плюс несколько natural-language-processing моих собственных -- обратите nlp внимание, что происходит string с "orcore":
perl splitwords.pl big.txt words answerveal: 2 possibilities - answer veal - answer ve al wickedweather: 4 possibilities - wicked weather - wicked we at her - wick ed weather - wick ed we at her liquidweather: 6 possibilities - liquid weather - liquid we at her - li quid weather - li quid we at her - li qu id weather - li qu id we at her driveourtrucks: 1 possibilities - drive our trucks gocompact: 1 possibilities - go compact slimprojector: 2 possibilities - slim projector - slim project or orcore: 3 possibilities - or core - or co re - orc ore
Код:
#!/usr/bin/env perl
use strict;
use warnings;
sub find_matches($);
sub find_matches_rec($\@\@);
sub find_word_seq_score(@);
sub get_word_stats($);
sub print_results($@);
sub Usage();
our(%DICT,$TOTAL);
{
my( $dict_file, $word_file ) = @ARGV;
($dict_file && $word_file) or die(Usage);
{
my $DICT;
($DICT, $TOTAL) = get_word_stats($dict_file);
%DICT = %$DICT;
}
{
open( my $WORDS, '<', $word_file ) or die "unable to open $word_file\n";
foreach my $word (<$WORDS>) {
chomp $word;
my $arr = find_matches($word);
local $_;
# Schwartzian Transform
my @sorted_arr =
map { $_->[0] }
sort { $b->[1] <=> $a->[1] }
map {
[ $_, find_word_seq_score(@$_) ]
}
@$arr;
print_results( $word, @sorted_arr );
}
close $WORDS;
}
}
sub find_matches($){
my( $string ) = @_;
my @found_parses;
my @words;
find_matches_rec( $string, @words, @found_parses );
return @found_parses if wantarray;
return \@found_parses;
}
sub find_matches_rec($\@\@){
my( $string, $words_sofar, $found_parses ) = @_;
my $length = length $string;
unless( $length ){
push @$found_parses, $words_sofar;
return @$found_parses if wantarray;
return $found_parses;
}
foreach my $i ( 2..$length ){
my $prefix = substr($string, 0, $i);
my $suffix = substr($string, $i, $length-$i);
if( exists $DICT{$prefix} ){
my @words = ( @$words_sofar, $prefix );
find_matches_rec( $suffix, @words, @$found_parses );
}
}
return @$found_parses if wantarray;
return $found_parses;
}
## Just a simple joint probability
## assumes independence between words, which is obviously untrue
## that's why this is broken out -- feel free to add better brains
sub find_word_seq_score(@){
my( @words ) = @_;
local $_;
my $score = 1;
foreach ( @words ){
$score = $score * $DICT{$_} / $TOTAL;
}
return $score;
}
sub get_word_stats($){
my ($filename) = @_;
open(my $DICT, '<', $filename) or die "unable to open $filename\n";
local $/= undef;
local $_;
my %dict;
my $total = 0;
while ( <$DICT> ){
foreach ( split(/\b/, $_) ) {
$dict{$_} += 1;
$total++;
}
}
close $DICT;
return (\%dict, $total);
}
sub print_results($@){
#( 'word', [qw'test one'], [qw'test two'], ... )
my ($word, @combos) = @_;
local $_;
my $possible = scalar @combos;
print "$word: $possible possibilities\n";
foreach (@combos) {
print ' - ', join(' ', @$_), "\n";
}
print "\n";
}
sub Usage(){
return "$0 /path/to/dictionary /path/to/your_words";
}
string
nlp
Как я могу разбить несколько соединенных слов?
Мы используем файлы cookies для улучшения работы сайта. Оставаясь на нашем сайте, вы соглашаетесь с условиями использования файлов cookies. Чтобы ознакомиться с нашими Положениями о конфиденциальности и об использовании файлов cookie, нажмите здесь.