簡體   English   中英

修改 R (RStudio) 中的 LexisNexisTools 以重命名文件

[英]Modifying LexisNexisTools in R (RStudio) to rename files

我正在嘗試重命名從 Nexis Advance UK 下載的目錄中的文件。 由於不熟悉編碼,我開始嘗試修改LexisNexisTools 在 RStudio 中的代碼。

我所做的是將term.v <- content_v[grep("^Terms: |^Begriffe: ", content_v)]更改為term.v <- content_v[grep("The Guardian(London)", fixed = T, content_v)] ,例如,並更改了重命名 function 以便它只粘貼term.v 但是,我試圖保留原始的OR function,以便代碼循環遍歷多個字符串,例如“Express Online”或“The Independent(英國)”,然后將找到的字符串粘貼到文件中重命名為 function。

到目前為止,這是我嘗試過的:

1 - 使用fixed = F的正則表達式(從我可以在線收集的字符串中帶有空格的正則表達式),例如"^The/sGuardian(London)$|^Express/sOnline$"

2-我嘗試使用矢量來“容納”不同的模式,然后將矢量粘貼到 grep 命令中

toMatch.v <- c("Express Online", "The Times (London)", "The Independent (United Kingdom)" 

term.v<- content_v[grep(paste(toMatch, collapse="|"),  content_v)]

代碼(修改后的)唯一有效的時間是fixed = T並且按照在.txt 文件中找到的字符串鍵入。

我究竟做錯了什么? 非常感謝,如果術語不准確,我深表歉意。

額外細節:

最初,代碼依賴一組關鍵字來查找搜索詞並將其插入文件名中:

    content_v <- readLines(files[i], encoding = encoding, n = 50)
    term.v <- content_v[grep("^Terms: |^Begriffe: ", content_v)]
    # erase everything in the line exept the actual range
    term.v <- gsub("^Terms: |^Begriffe: ", "", term.v)
    # split term into elemets seprated by and or OR
    term.v <- unlist(strsplit(term.v, split = " AND | and | OR ", fixed = FALSE))

我已經更改了它,以便grep以我想要 append 到文件名的字符串開頭,如上所述。 我還禁用了gsub行並將split參數更改為"/n" ,因為我的文本文件中的字符串用換行符分隔。 以下是 sample.txt 文件的示例。

假設您的工作目錄中有一個文件file1.txt ,其內容類似於以下內容:

foo
foo bar Express Online
bar

然后,以下代碼應將文件重命名為Express Online.txt

file1 <- "file1.txt"

text1 <- readLines(file(file1))

# if (any(grepl("The Guardian", text1))) {
#     file.rename(file1, "The Guardian.txt")
# } else if (any(grepl("Express Online", text1))) {
#     file.rename(file1, "Express Online.txt")
# }

newname <- head(
    n = 1,
    na.omit(
        stringr::str_extract(
            text1,
            "(Express Online)|(The Times \\(London\\))|(The Independent \\(United Kingdom\\))")))

file.rename(file1, paste0(newname, ".txt"))

不幸的是,您的文件格式與我編寫LexisNexisTools時文件的外觀完全不同。 您的要求也是如此。 所以我會在這里編寫新代碼來完成這項工作。 首先,讓我們嘗試一個文件:

f <- "/home/johannes/Documents/x.txt"
lines <- readLines(f)

toMatch.v <- c("Express Online", "The Times (London)", "The Independent (United Kingdom)")

# I'm using another function from the package to convenietly look up a several patterns at once
np <- unlist(LexisNexisTools::lnt_lookup(lines, toMatch.v, verbose = FALSE))[1]
new_name <- paste0(dirname(f), "/", np, ".txt")
new_name
#> [1] "/home/johannes/Documents/Express Online.txt"

file.rename(f, new_name)

一旦這按預期為您工作,您就可以為許多文件實現它。 與我原來的 function 一樣,我建議您先在 data.frame 中寫入新名稱,以便檢查新名稱是否有意義以及新名稱中是否有重復項( R會將兩個文件寫入新名稱而不會發出警告並在這種情況下銷毀一個文件):

files <- list.files("/home/johannes/Documents/", pattern = ".txt$", 
                    ignore.case = TRUE, full.names = TRUE)

make_new_name <- function(old_name) {
  lines <- readLines(old_name)

  np <- unlist(LexisNexisTools::lnt_lookup(lines, toMatch.v, verbose = FALSE))[1]
  paste0(dirname(old_name), "/", np, ".txt")
}  

df <- tibble::tibble(
  old = files,
  new = sapply(files, make_new_name)
)
df               
#> # A tibble: 2 x 2
#>   old                              new                                        
#>   <chr>                            <chr>                                      
#> 1 /home/johannes/Documents//x.txt  /home/johannes/Documents/Express Online.txt
#> 2 /home/johannes/Documents//x2.txt /home/johannes/Documents/Express Online.txt

如果新名稱對您有意義並且沒有重復( table(duplicated(df$new)) ),您可以拉動觸發器並重命名文件:

file.rename(df$old, df$new)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM