【发布时间】:2018-06-03 23:46:57
【问题描述】:
我写了如下函数来判断一个文档的tf-idf:
确定 tf
tf <- function(specific_word, text){
count = 0
list = unlist(strsplit(text, " "))
for(word in (list)){
if(word == specific_word){
count = count + 1
}
}
hit_rate <- count/length(list)
return(hit_rate)
}
确定 idf 值
idf <- function(specific_word, texts){
times_a_word_appears <- 0
total_number_of_documents <- length(texts)
for(document in texts){
list = strsplit(document, " ")
list = unlist(list)
for(word in list){
if(word == specific_word){
times_a_word_appears = times_a_word_appears + 1
break
}
}
}
times_a_word_appears = times_a_word_appears + 1
idf = log(total_number_of_documents/ times_a_word_appears)
return(idf)
}
最后——确定 tf-idf
tfidf <- function(specific_word, text, texts){
x = tf(specific_word, text)
y = idf(specific_word, texts)
z = x * y
print(paste0("The tf-idf value is: ", z))
}
我现在可以使用它来确定这些文档的 fe 的 tf-idf 值:
document1 = c("films is a 2000 made-for-TV horror movie directed by Richard Clabaugh. The film features several cult favorite actors, including William Zabka of The Karate Kid fame, Wil Wheaton, Casper Van Dien, Jenny McCarthy, Keith Coogan, Robert Englund (best known for his role as Freddy Krueger in the
A Nightmare on Elm Street series of films), Dana Barron, David Bowe, and Sean Whalen. The film concerns a genetically engineered snake, a python, that escapes and unleashes itself on a small town. It includes the classic final girl scenario evident in films like Friday the 13th. It was filmed in Los Angeles,
California and Malibu, California. Python was followed by two sequels: Python II (2002) and Boa vs. Python (2004), both also made-for-TV films")
document2 = c("Python, from the Greek word, is a genus of nonvenomous pythons[2] found in Africa and Asia. Currently, 7 species are recognised.[2] A member of this genus, P. reticulatus, is among the longest snakes known.")
document3 = c("The Colt Python is a .357 Magnum caliber revolver formerly manufactured by Colt's Manufacturing Company of Hartford, Connecticut. It is sometimes referred to as a Combat Magnum It was first introduced in 1955, the same year as Smith & Wesson's M29 .44 Magnum. The now discontinued
Colt Python targeted the premium revolver market segment. Some firearm collectors and writers such as Jeff Cooper, Ian V. Hogg, Chuck Hawks, Leroy Thompson, Renee Smeets and Martin Dougherty have described the Python as the finest production revolver ever made")
texts = c(document1, document2, document3)
并在document1中找到“films”的tf-idf值
word = "films"
relevant_text = document1
tfidf(word, relevant_text, texts)
但是我现在想要的是遍历所有文档中的所有单词,以确定文档中评分最高的单词。
所以对于文档 1 有点像:
words = unlist(unique(strsplit(document1, " ")))
for(word in words){
tfidf(word, document1, texts)
}
但是这些值应该存储在一个数组中并进行排序。在python中有点像这样:
scores = {word: tfidf(word, document1, texts) for word in document1.words}
sorted_words = sorted(scores.items(), key=lambda x: x[1], reverse=True)
关于如何在 R 中最有效地完成此操作有什么想法吗?
【问题讨论】:
-
您打算自己做这件事吗?是锻炼吗?否则,您可以使用许多现有实现之一:
tidytext::bind_tf_idf、cleanNLP::get_tfidf、quanteda::tfidf或tm::weightTfIdf...