2016-08-16 4 views
0

여러 개의 txt 파일이 있습니다. 각 파일은 다른 연도 (수년간)를 나타냅니다. 그래서, 어떻게 내가이 파일들 (텍스트 마이닝)을 각각의 코퍼스 (또는 비슷한 것)와 별도로 분석 할 수 있었는지, 월간 참조를 추적하여 감사합니다.여러 txt 파일의 R 텍스트 마이닝 문서

+0

분석 결과와 함께 파일 이름을 가져 와서 데이터 프레임/목록/...을 만듭니다. 즉, 파일 이름에 해당하는 콜론 이름을 사용합니다. – maRtin

답변

1

다음은 Game of Thrones 자막 용으로 프로그래밍 된 예입니다. 자막은 60 개의 텍스트 파일 형태로되어 있습니다. S01E01 형식의 한 에피소드에 대해 하나의 파일로 에피소드 정보를 보관하고 싶었습니다.

다음 코드는 파일을 목록으로 읽어 들여 에피소드 정보와 텍스트가 포함 된 데이터 프레임으로 변환합니다. 당신은 당신 자신의 문제에 그것을 적응시켜야 할 것이다.

library(plyr) 
####### Read data ###### 

filenames <- list.files("Set7/Game of Thrones Subtitles", pattern="*", full.names=TRUE) 
filenames_short <- list.files("Set7/Game of Thrones Subtitles", pattern="*", full.names=FALSE) 

ldf <- alply(.data=filenames,.margins=1,.fun=scan,what = "character", quiet = T, quote = "") 
names(ldf) <- filenames_short 


# Loop over all filenames 
# Turns list into two columns of a dataframe, episode and word 
# create empty dataframe  
df_got_subs <- as.data.frame(NULL) 


    for (i in 1:60) { 

     # extract listname 
     # vector with list name 
     listenname <- filenames_short[i] 
     vec_listenname <- rep.int(listenname,length(ldf[[i]])) 

     # Doublecheck 
     cat("listenname: ",listenname,"\n") 

     # turn list element into vector 
     vec_subs <- as.vector(ldf[[i]]) 

     # create dataframe from vectors 
     df_subs <- cbind.data.frame(vec_listenname,vec_subs,stringsAsFactors=FALSE) 

     # attach to the "big" dataframe 
     df_got_subs <- rbind.data.frame(df_got_subs,df_subs) 

    } 

    # test datastructure 
    str(df_got_subs) 

    # change column names 
    colnames(df_got_subs) <- c("episode","subs") 

Julia Silge의 tidytext 패키지로 수행 한 전체 텍스트 마이닝. 나는이 게시물에서 훨씬 더 좋은 예를 제시했기 때문에 코드를 게시하지 않았다 :

나는 이것이 당신의 문제에 도움이되기를 바랍니다.

관련 문제