如何以XML-LMF格式导入词典以在R中进行情感分析

时间:2019-01-22 10:45:07

标签: r xml-parsing text-mining sentiment-analysis quanteda

我正在尝试在R中导入以下词典,以与诸如quanteda之类的文本挖掘程序包一起使用,或将其导出为列表或数据框:

https://github.com/opener-project/VU-sentiment-lexicon/tree/master/VUSentimentLexicon/IT-lexicon

格式为XML-LMF。我找不到用R解析这种格式的任何方法。

(请参阅https://en.wikipedia.org/wiki/Lexical_Markup_Framework

作为一种解决方法,我尝试使用XML包,但是其结构与常规XML有所不同,并且我没有设法解析所有节点。

1 个答案:

答案 0 :(得分:0)

我设法使用xml2包使其工作。这是我的代码:

library(xml2)
library(quanteda)

# Read file and find the nodes
opeNER_xml <- read_xml("it-sentiment_lexicon.lmf.xml")
entries <- xml_find_all(opeNER_xml, ".//LexicalEntry")
lemmas <- xml_find_all(opeNER_xml, ".//Lemma")
confidence <- xml_find_all(opeNER_xml, ".//Confidence")
sentiment <- xml_find_all(opeNER_xml, ".//Sentiment")

# Parse and put in a data frame
opeNER_df <- data.frame(
  id = xml_attr(entries, "id"),
  lemma = xml_attr(lemmas, "writtenForm"),
  partOfSpeech = xml_attr(entries, "partOfSpeech"),
  confidenceScore = as.numeric(xml_attr(confidence, "score")),
  method = xml_attr(confidence, "method"),
  polarity = as.character(xml_attr(sentiment, "polarity")),
  stringsAsFactors = F
)
# Fix a mistake
opeNER_df$polarity <- ifelse(opeNER_df$polarity == "nneutral", 
                             "neutral", opeNER_df$polarity)

# Make quanteda dictionary
opeNER_dict <- quanteda::dictionary(with(opeNER_df, split(lemma, polarity)))