RAKE: Rapid Automatic Keyword Extraction Algorithm 快速自動提取關鍵詞算法
自然語言處理分析的最基本和初始步驟是關鍵詞提取,因為沒有關鍵詞提取,就不可能再進一步。正如我們所知,在NLP中,我們有許多算法可以幫助我們提取文本數據的關鍵字,但是我們知道最常用的算法嗎?
大多數數據科學家和機器學習開發人員使用的一些主要文本處理算法是:
TF-IDF
TextRank
RAKE
What is the RAKE Algorithm?什么是RAKE算法?
Rake也稱為快速自動關鍵字提取,是一種非常高效的關鍵字提取算法,可對單個文檔進行操作,以實現對動態集合的應用,也可非常輕松地應用于新域,并且在處理多種類型的文檔時也非常有效,尤其是遵循特定語法慣例的文本類型。
Rake是基于這樣的觀察:關鍵詞經常包含多個帶有標準標點符號的單詞或停用詞,或者我們可以說像“and”、“of”、“the”等具有最低詞匯意義的功能詞,停用詞通常會在所有信息系統中刪除,也不會包含在各種文本分析中,因為它們被認為是無意義的。被認為具有與文本相關的含義的詞被描述為內容承載詞,稱為內容詞。
RAKE算法的輸入參數包括一個停用詞列表,以及一組短語分隔符和單詞分隔符。它使用停用詞和短語分隔符將文檔劃分為候選關鍵字,這些候選關鍵字主要是幫助開發人員提取從文檔中獲取信息所需的確切關鍵字。
- 使用以下命令安裝Rake:
$ git clone [https://github.com/zelandiya/*RAKE*-tutorial](https://github.com/zelandiya/RAKE-tutorial)
# 要在python代碼中導入rake:
import rake
import operator
# 加載文本并對其應用rake:
filepath = "keyword_extraction.txt"
rake_object = rake.Rake(filepath)
text = "Compatibility of systems of linear constraints over the set of natural numbers. Criteria of compatibility of a system of linear Diophantine equations, strict inequations, and nonstrict inequations are considered.Upper bounds for components of a minimal set of solutions and algorithms of construction of minimal generatingsets of solutions for all types of systems are given. These criteria and the corresponding algorithms for constructing a minimal supporting set of solutions can be used in solving all the considered types of systems and systems of mixed types."
sample_file = open(“data/docs/fao_test/w2167e.txt”, ‘r’)
text = sample_file.read()
keywords = rake_object.run(text) print “Keywords:”, keywords
- 候選關鍵字:
如上所述,我們知道RAKE通過使用停用詞和短語分隔符解析文檔,將包含主要內容的單詞分類為候選關鍵字。這基本上是通過以下一些步驟來完成的,首先,文檔文本被特定的單詞分隔符分割成一個單詞數組,其次,該數組再次被分割成一個在短語分隔符和停用單詞位置的連續單詞序列。最后,位于相同序列中的單詞被分配到文本中的相同位置,并一起被視為候選關鍵字。
stopwordpattern = rake.build_stop_word_regex(filepath)
phraseList = rake.generate_candidate_keywords(sentenceList, stopwordpattern)
- 關鍵詞得分:
從文本數據中識別出所有候選關鍵字后,將生成單詞共現圖,該圖計算每個候選關鍵字的分數,并定義為成員單詞分數。借助該圖,我們根據圖中頂點的程度和頻率評估了計算單詞分數的幾個指標。
keywordcandidates = rake.generate_candidate_keyword_scores(phraseList, wordscores)
-
主要指標如下:
Word Frequency (freq(w))
Word Degree (deg(w))
Ratio of degree to frequency (deg(w)/freq(w))
(Deg (w)) favors words that occur often and in longer candidates.
(Freq (w)) favors words that occur frequently regardless of the number of words with which they co-occurred.
(Deg (w)/ Freq (w) favors the words that predominately occur in longer candidate keywords.
每個候選關鍵字的最終分數計算為其成員單詞分數之和。
- 相鄰關鍵字:
正如我們所知,Rake通過停用詞分割候選關鍵字,因此提取的關鍵字不包含內部停用詞,因此,將包含內部停用詞的關鍵字識別為邪惡軸是一種興趣。查找在同一文檔中以相同順序彼此至少相鄰兩次的關鍵字。為此,將創建一個新的候選關鍵字,作為這些關鍵字和內部停用詞的組合。在這一部分中,我們應該理解,只有很少的鏈接詞被提取出來,從而增加了意義。
-
提取關鍵詞:
After the candidate keyword score is calculated, the top T candidate keywords are selected from the document. The T value is one-third the number of words in the graph.
計算候選關鍵字得分后,將從文檔中選擇前T個候選關鍵字。T值是圖中字數的三分之一。
totalKeywords = len(sortedKeywords)
for keyword in sortedKeywords[0:(totalKeywords / 3)]:
print “Keyword: “, keyword[0], “, score: “, keyword[1]
- 期望輸出:
Keywords: Keywords: [(‘household food security’, 7.711414565826329), (‘indigenous groups living’, 7.4), (‘national forest programmes’, 7.249539170506913), (‘wood forest products’, 6.844777265745007)…
Keyword: minimal generating sets , score: 8.66666666667Keyword: linear diophantine equations , score: 8.5
Keyword: minimal supporting set , score: 7.66666666667
Keyword: minimal set , score: 4.66666666667
Keyword: linear constraints , score: 4.5
Keyword: upper bounds , score: 4.0
Keyword: natural numbers , score: 4.0
Keyword: nonstrict inequations , score: 4.
- 使用以下命令評估Rake算法:
Precision 4.44 Recall 5.17 F-Measure 4.78
原文需翻墻:
https://medium.datadriveninvestor.com/rake-rapid-automatic-keyword-extraction-algorithm-f4ec17b2886c