2013-04-18 57 views
0

設定筆者在語料庫中每個文檔我有一個TM語料庫對象是這樣的:如何通過解析文檔ID

> summary(corp.eng) 
A corpus with 154 text documents 

The metadata consists of 2 tag-value pairs and a data frame 
Available tags are: 
    create_date creator 
Available variables in the data frame are: 
    MetaID 

在語料庫每個文件的元數據中查找此:

> meta(corp.eng[[1]]) 
Available meta data pairs are: 
    Author  : 
    DateTimeStamp: 2013-04-18 14:37:24 
    Description : 
    Heading  : 
    ID   : Smith-John_e.txt 
    Language  : en_CA 
    Origin  : 

我知道我可以在這個時間設置一個文件的作者:

meta(corp.eng[[1]],tag="Author") <- 
    paste(
    rev(
     unlist(
     strsplit(meta(corp.eng[[1]],tag="ID"), c("[-_]")) 
    )[1:2] 
    ), collapse=' ') 

,給了我這樣的結果:

> meta(corp.eng[[1]],tag="Author") 
[1] "John Smith" 

如何對作業進行批處理?

回答

2

注意:這仍然可能應該是一個評論,但有一定的工作部分,所以這裏去一個例子:

data(crude) 
extracted.values <- meta(crude,tag="Places",type="local") 
for (i in seq_along(extracted.values)) { 
    meta(crude[[i]],tag="Places") <- substr(extracted.values[[i]],1,3) 
} 

每個人都應該能夠使用lapply以及做到這一點,但我不熟悉tm的內部工作,我會堅持循環。將substr函數替換爲您需要的函數,當然也可以使用左側的數據。希望這可以幫助。

+0

meta {tm} description http://www.inside-r.org/packages/cran/tm/docs/meta。 'tm'有一個被稱爲「原油」的數據集。儘管我對ID的特定解析不適用,但如果要將其用於測試,則應用任何函數都可以。 – dnagirl

+0

'lapply'返回一個列表。我需要編輯語料庫。 – dnagirl