. , . . , , , .
. - , wordpair. :
> word5
[1] "hotter the doghou se would be bec ause the col or was diffe rent"
:
> abc1_pairs
word1 word2
1 hotter the
2 the doghou
3 doghou se
4 se would
5 would be
6 be bec
7 bec ause
8 ause the
9 the col
10 col or
11 or was
12 was diffe
13 diffe rent
wordpairs , , , ( , ):
merge_wordpairs = function(wordpairs) {
require(plyr)
merged_pairs = as.character(mlply(wordpairs, merge_word))
correct_words_idxs = which(sapply(merged_pairs, word_correct))
if(length(correct_words_idxs) == 0) {
return(wordpairs)
} else {
message(sprintf("Number of words about to be merged in this pass: %s", length(correct_words_idxs)))
for(idx in correct_words_idxs) {
wordpairs = merge_specific_pair(wordpairs, idx, delete_pair = FALSE)
}
return(merge_wordpairs(wordpairs[-correct_words_idxs,]))
}
}
:
> word5 <- "hotter the doghou se would be bec ause the col or was diffe rent"
> abc1 = strsplit(word5, split = " ")[[1]]
> abc1_pairs = wordlist2wordpairs(abc1)
> abc1_pairs
word1 word2
1 hotter the
2 the doghou
3 doghou se
4 se would
5 would be
6 be bec
7 bec ause
8 ause the
9 the col
10 col or
11 or was
12 was diffe
13 diffe rent
> abc1_merged_pairs = merge_wordpairs(abc1_pairs)
Number of words about to be merged in this pass: 4
> merged_sentence = paste(wordpairs2wordlist(abc1_merged_pairs), collapse = " ")
> c(word5, merged_sentence)
[1] "hotter the doghou se would be bec ause the col or was diffe rent"
[2] "hotter the doghouse would be because the color was different"
:
wordlist2wordpairs = function(word_list) {
require(plyr)
wordpairs = ldply(seq_len(length(word_list) - 1),
function(idx)
return(c(word_list[idx],
word_list[idx+1])))
names(wordpairs) = c("word1", "word2")
return(wordpairs)
}
wordpairs2wordlist = function(wordpairs) {
return(c(wordpairs[[1]], wordpairs[[2]][nrow(wordpairs)]))
}
word_correct = function(word) return(nrow(aspell(factor(word))) == 0)
# Merge two words
merge_word = function(word1, word2) return(paste(word1, word2, sep = ""))
# Merge a specific pair, option to postpone deletion of pair
merge_specific_pair = function(wordpairs, idx, delete_pair = TRUE) {
merged_word = do.call("merge_word", wordpairs[idx,])
if(!(idx == 1)) wordpairs[idx - 1, "word2"] = merged_word
if(!(idx == nrow(wordpairs))) wordpairs[idx + 1, "word1"] = merged_word
if(delete_pair) wordpairs = wordpairs[-idx,]
return(wordpairs)
}