RAKE: Rapid Automatic Keyword Extraction Algorithm 快速自動提取關(guān)鍵詞算法
自然語言處理分析的最基本和初始步驟是關(guān)鍵詞提取,因為沒有關(guān)鍵詞提取,就不可能再進一步。正如我們所知,在NLP中,我們有許多算法可以幫助我們提取文本數(shù)據(jù)的關(guān)鍵字,但是我們知道最常用的算法嗎?
大多數(shù)數(shù)據(jù)科學(xué)家和機器學(xué)習(xí)開發(fā)人員使用的一些主要文本處理算法是:

TF-IDF
TextRank
RAKE
What is the RAKE Algorithm?什么是RAKE算法?

Rake也稱為快速自動關(guān)鍵字提取,是一種非常高效的關(guān)鍵字提取算法,可對單個文檔進行操作,以實現(xiàn)對動態(tài)集合的應(yīng)用,也可非常輕松地應(yīng)用于新域,并且在處理多種類型的文檔時也非常有效,尤其是遵循特定語法慣例的文本類型。
Rake是基于這樣的觀察:關(guān)鍵詞經(jīng)常包含多個帶有標(biāo)準(zhǔn)標(biāo)點符號的單詞或停用詞,或者我們可以說像“and”、“of”、“the”等具有最低詞匯意義的功能詞,停用詞通常會在所有信息系統(tǒng)中刪除,也不會包含在各種文本分析中,因為它們被認(rèn)為是無意義的。被認(rèn)為具有與文本相關(guān)的含義的詞被描述為內(nèi)容承載詞,稱為內(nèi)容詞。

RAKE算法的輸入?yún)?shù)包括一個停用詞列表,以及一組短語分隔符和單詞分隔符。它使用停用詞和短語分隔符將文檔劃分為候選關(guān)鍵字,這些候選關(guān)鍵字主要是幫助開發(fā)人員提取從文檔中獲取信息所需的確切關(guān)鍵字。
- 使用以下命令安裝Rake:
$ git clone [https://github.com/zelandiya/*RAKE*-tutorial](https://github.com/zelandiya/RAKE-tutorial)
# 要在python代碼中導(dǎo)入rake:
import rake
import operator
# 加載文本并對其應(yīng)用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
- 候選關(guān)鍵字:
如上所述,我們知道RAKE通過使用停用詞和短語分隔符解析文檔,將包含主要內(nèi)容的單詞分類為候選關(guān)鍵字。這基本上是通過以下一些步驟來完成的,首先,文檔文本被特定的單詞分隔符分割成一個單詞數(shù)組,其次,該數(shù)組再次被分割成一個在短語分隔符和停用單詞位置的連續(xù)單詞序列。最后,位于相同序列中的單詞被分配到文本中的相同位置,并一起被視為候選關(guān)鍵字。
stopwordpattern = rake.build_stop_word_regex(filepath)
phraseList = rake.generate_candidate_keywords(sentenceList, stopwordpattern)
- 關(guān)鍵詞得分:
從文本數(shù)據(jù)中識別出所有候選關(guān)鍵字后,將生成單詞共現(xiàn)圖,該圖計算每個候選關(guān)鍵字的分?jǐn)?shù),并定義為成員單詞分?jǐn)?shù)。借助該圖,我們根據(jù)圖中頂點的程度和頻率評估了計算單詞分?jǐn)?shù)的幾個指標(biāo)。
keywordcandidates = rake.generate_candidate_keyword_scores(phraseList, wordscores)
-
主要指標(biāo)如下:
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.
每個候選關(guān)鍵字的最終分?jǐn)?shù)計算為其成員單詞分?jǐn)?shù)之和。
- 相鄰關(guān)鍵字:
正如我們所知,Rake通過停用詞分割候選關(guān)鍵字,因此提取的關(guān)鍵字不包含內(nèi)部停用詞,因此,將包含內(nèi)部停用詞的關(guān)鍵字識別為邪惡軸是一種興趣。查找在同一文檔中以相同順序彼此至少相鄰兩次的關(guān)鍵字。為此,將創(chuàng)建一個新的候選關(guān)鍵字,作為這些關(guān)鍵字和內(nèi)部停用詞的組合。在這一部分中,我們應(yīng)該理解,只有很少的鏈接詞被提取出來,從而增加了意義。
-
提取關(guān)鍵詞:
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.
計算候選關(guān)鍵字得分后,將從文檔中選擇前T個候選關(guān)鍵字。T值是圖中字?jǐn)?shù)的三分之一。
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