2017-02-04 11 views
1

テキストドキュメントをn個のチャンクに分割し、各チャンクをリストに格納します。各チャンクは単語のセットに変換され、次に、コサイン類似性関数が、チャンクの1つと、関数に送信される前にセットに変換される別の短いテキストとの間に適用されます。私はどうにかして各チャンクを関数に渡して2番目のセットと比較する必要がありますが、適用ファミリの関数の1つがループを使うのではなく仕事をすることができるかどうか疑問に思っていました。また、各結果をベクトルに格納する時間を節約することもできます。リスト内の各要素とR内の別の要素の間に適用ファミリを使用する

これは私がコードの一部がthisからである(使用しています何です?

library("data.table","qdap","sets", "lsa") 

s <- c("employees businesses san gwann admitted sales taken hit after traffic diversions implemented without notice vjal ir - rihan over weekend.", 
"also complained werent consulted diversion blocked vehicles driving centre  san gwann via roundabout forks san gwann industrial estate, church forced motorists take detour around block instead.", 
"barriers erected roundabout exit, after youtube video cars disregarding signage passing through roundabout regardless went viral.", 
"planned temporary diversion, brace san gwann influx cars set pass through during works kappara junction project.", 
"usually really busy weekend, our sales lower round, corner store worker maria abela admitted maltatoday.") 

c <- "tm dont break whats broken. only queues developing, pass here every morning never experienced such mess notwithstanding tm officials directing traffic. hope report congestion happening area. lc tm tried pro - active hope admit recent traffic changes working." 


calculateCosine <- function(setX, setY){ 
require(qdap) 
y <- c(unlist(as.character(tolower(setY)))) 
x <- c(unlist(strsplit(as.character(tolower(setX)), split = ", "))) 
diffLength <- length(y) - length(x) 
x <- bag_o_words(x) 
for(pad in 1 : diffLength){ 
    x <- c(x, "") 
    } 
    # write both sets to temp files and calculate cosine similarity 
    write(y, file=paste(td, "Dy", sep="/")) 
    write(x, file=paste(td, "Dx", sep="/")) 
    myMatrix = textmatrix(td, stopwords=stopwords_en, minWordLength = 3) 
    similCosine <- as.numeric(round(cosine(myMatrix[,1], myMatrix[,2]), 3)) 
    return(similCosine) 
} 

n <- 3 
max <- length(s)%/%n 
x <- seq_along(s) 
d1 <- split(s, ceiling(x/max)) 
res <- c() 
for(i in 1 : length(d1)){ 
    val <- calculateCosine(as.set(paste(d1[i], sep = " ", collapse = " ")), as.set(c)) 
    res <- c(res, val) 
} 

清楚のために、適用機能の一つにループを変更することが可能であろう任意のアイデアやコメントがういただければ感謝

+0

編集。それを指摘してくれてありがとう。 – salvu

答えて

4

repsapplyで2つのforのループを調整してみてください:。。

インサイドcalculateCosine calculateCosinelapplyへの変更あなたの代わりに、ベクトル/行列の返されたリストが必要な場合)社外

# ORIGINAL CODE 
x <- bag_o_words(x) 
for(pad in 1 : diffLength){ 
    x <- c(x, "") 
    } 

# ADJUSTED CODE 
x <- bag_o_words(x) 
x <- c(x, rep("", diffLength))  

# OR ONE LINE 
x <- c(bag_o_words(x), rep("", diffLength)) 

# ORIGINAL CODE 
res <- c() 
for(i in 1 : length(d1)){ 
    val <- calculateCosine(as.set(paste(d1[i], sep = " ", collapse = " ")), as.set(c)) 
    res <- c(res, val) 
} 

# ADJUSTED CODE 
res <- sapply(d1, function(i) { 
    calculateCosine(as.set(paste(i, sep = " ", collapse = " ")), as.set(c)) 
}) 
+0

ありがとうございます。 'diffLength'が短い' setY'で負になるまで完全に働きました。 'calculateCosine'関数を' setX'ではなく 'setY'を埋め込むように修正しました。再度、感謝します。 – salvu

関連する問題