2016-11-29 6 views
0

複数のtxt.filesに対してkoRpusパッケージを使用し、結果をExcelまたはsqllite3またはtxtに保存するR-3.3.2(R-Studio 3.4 for Win)の可読性スコアを計算したい。 これで、1つのファイルの可読性スコアのみを計算し、コンソールに出力できます。私はディレクトリ上のループを使用してコードを改善しようとしましたが、正しく動作しません。複数ファイルの可読性スコアを計算するR

library(koRpus) 
library(tm) 

#Loop through files 
path = "D://Reports" 
out.file<-"" 
file.names <- dir(path, pattern =".txt") 
for(i in 1:length(file.names)){ 
    file <- read.table(file.names[i],header=TRUE, sep=";", stringsAsFactors=FALSE) 
    out.file <- rbind(out.file, file) 
} 

#Only one file 
report <- tokenize(txt =file , format = "file", lang = "en") 

#SMOG-Index 
results_smog <- SMOG(report) 
summary(results_smog) 

#Flesch/Kincaid-Index 
results_fleshkin <- flesch.kincaid(report) 
summary(results_fleshkin) 

#FOG-Index 
results_fog<- FOG(report) 
summary(results_fog) 
+0

あなたは明確にできます:彼らはあなたが読んしようとしているだけで、プレーンテキスト文書これらのレポートは、見出しの最初の行には本当にセミコロン区切りテーブル(あなた 'read.table'呼び出しが暗示するように)している、またはされています。 –

+0

また、一緒に連結されたすべてのファイルに対して 'koRpus'呼び出しを実行するつもりです(あたかも1つの大きなファイルであるかのように)(koRpusの結果を1つだけ得るように)、あるいは別々のセットを生成したいか'koRpus'の結果は、各ファイルに1つずつありますか? @K。 –

+0

A. Buhr私のディレクトリにはシンプルなプレーンテキストの文書が入っています。それぞれのファイルの結果を別々に取得したいので、結果を後で1つのExcelテーブルに結合することができます。 – In777

答えて

1

同じ問題が発生しました。私は解決策のためにstackoverflowを見ていて、あなたの投稿を見ました。試行錯誤の末、私は次のコードを思いついた。私のためにうまくいった。私はすべての余分な情報を取り出した。私が探していたスコアのインデックス値を見つけるために、私は最初にそれを1つのファイルに対して実行し、可読性ラッパーの概要を取り出しました。それはあなたにさまざまな価値の束を与えるでしょう。列と列を一致させると、特定の番号が検索されます。さまざまなオプションがあります。

パスディレクトリでは、ファイルは独立したテキストファイルである必要があります。

#Path 
path="C:\\Users\\Philipp\\SkyDrive\\Documents\\Thesiswork\\ReadStats\\" 

#list text files 
ll.files <- list.files(path = path, pattern = "txt", full.names = TRUE);length(ll.files) 

#set vectors 
SMOG.score.vec=rep(0.,length(ll.files)) 
FleshKincaid.score.vec=rep(0.,length(ll.files)) 
FOG.score.vec=rep(0.,length(ll.files)) 

#loop through each file 
for (i in 1:length(ll.files)){ 
    #tokenize 
    tagged.text <- koRpus::tokenize(ll.files[i], lang="en") 
    #hyphen the word for some of the packages that require it 
    hyph.txt.en <- koRpus::hyphen(tagged.text) 
    #Readability wrapper 
    readbl.txt <- koRpus::readability(tagged.text, hyphen=hyph.txt.en, index="all") 
    #Pull scores, convert to numeric, and update the vectors 
    SMOG.score.vec[i]=as.numeric(summary(readbl.txt)$raw[36]) #SMOG Score 
    FleshKincaid.score.vec[i]=as.numeric(summary(readbl.txt)$raw[11]) #Flesch Reading Ease Score 
    FOG.score.vec[i]=as.numeric(summary(readbl.txt)$raw[22]) #FOG score 
    if (i%%10==0) 
    cat("finished",i,"\n")} 

#if you wanted to do just one 
df=cbind(FOG.score.vec,FleshKincaid.score.vec,SMOG.score.vec) 
colnames(df)=c("FOG", "Flesch Kincaid", "SMOG") 
write.csv(df,file=paste0(path,"Combo.csv"),row.names=FALSE,col.names=TRUE) 

# if you wanted to write seperate csvs 
write.csv(SMOG.score.vec,file=paste0(path,"SMOG.csv"),row.names=FALSE,col.names = "SMOG") 
write.csv(FOG.score.vec,file=paste0(path,"FOG.csv"),row.names=FALSE,col.names = "FOG") 
write.csv(FleshKincaid.score.vec,file=paste0(path,"FK.csv"),row.names=FALSE,col.names = "Flesch Kincaid") 
関連する問題