簡體   English   中英

從 SEC Edgar 文件中清除 readtext 和 quanteda 中的標簽

[英]Clean tags from SEC Edgar filings in readtext and quanteda

我正在嘗試使用 readtext 和 quanteda 將 .txt 文件讀取到 R 中,這些文件是我從 SEC Edgar 公開上市公司文件數據庫中解析出來的。 .txt 文件的一個例子在這里,一個更用戶友好的版本在這里進行比較(加利福尼亞野火期間的 PG​​&E)。

我的代碼如下,對於 1996 年的文件夾,包含許多 .txt 文件:

directory<-("D:")
text <- readtext(paste0(directory,"/1996/*.txt"))
corpus<-corpus(text)
dfm<-dfm(corpus,tolower=TRUE,stem=TRUE,remove=stopwords("english"),remove_punct=TRUE)

我注意到 dfm 仍然包含很多“無用”標記,例如“字體樣式”、“斜體”,最后還有許多無用標記,例如“3eyn”和“kq”,我認為它們是其中的一部分.txt 文件底部的 .jpg 部分。

當我在使用 readtext 時對文檔進行編碼時,問題仍然存在,例如在執行以下操作時:

text<-readtext(paste0(directory,"/*.txt"),encoding="UTF-8")
text<-readtext(paste0(directory,"/*.txt"),encoding="ASCII")

非常感謝有關如何清理這些文件以使它們看起來更像上面的用戶友好版本(即僅包含正文)的任何幫助。

這里的關鍵是在文本中找到指示您想要的文本開始的標記,以及指示文本結束位置的標記。 這可以是在正則表達式中使用|分隔的一組條件| .

在第一個標記之前不保留任何內容(默認情況下),您可以通過使用corpus_subset()從語料庫中刪除結尾標記后面的文本。 在您發現實際數據中的各種模式后,實際模式無疑需要進行調整。

以下是我為您的示例文檔所做的操作:

library("quanteda")
## Package version: 2.0.0

corp <- readtext::readtext("https://www.sec.gov/Archives/edgar/data/75488/000114036117038612/0001140361-17-038612.txt") %>%
  corpus()

# clean up text
corp <- gsub("<.*?>|&#\\d+;", "", corp)
corp <- gsub("&amp;", "&", corp)

corp <- corpus_segment(corp,
  pattern = "Item 8\\.01 Other Events\\.|SIGNATURES",
  valuetype = "regex"
) %>%
  corpus_subset(pattern != "SIGNATURES")

print(corp, max_nchar = -1)
## Corpus consisting of 1 document and 1 docvar.
## 0001140361-17-038612.txt.1 :
## "Investigation of Northern California Fires   Since October 8, 2017, several catastrophic wildfires have started and remain active in Northern California. The causes of these fires are being investigated by the California Department of Forestry and Fire Protection (Cal Fire), including the possible role of power lines and other facilities of Pacific Gas and Electric Companys (the Utility), a subsidiary of PG&E Corporation.   It currently is unknown whether the Utility would have any liability associated with these fires. The Utility has approximately $800 million in liability insurance for potential losses that may result from these fires. If the amount of insurance is insufficient to cover the Utility's liability or if insurance is otherwise unavailable, PG&E Corporations and the Utilitys financial condition or results of operations could be materially affected."

暫無
暫無

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

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