pip install multi-rake如果安裝因cld錯誤narrowing conversions失敗,則可以安裝
CFLAGS= " -Wno-narrowing " pip install multi-rake英語文本,我們沒有明確指定語言或止版列表(使用內置列表)。
from multi_rake import Rake
text_en = (
'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 generating sets '
'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.'
)
rake = Rake ()
keywords = rake . apply ( text_en )
print ( keywords [: 10 ])
# ('minimal generating sets', 8.666666666666666),
# ('linear diophantine equations', 8.5),
# ('minimal supporting set', 7.666666666666666),
# ('minimal set', 4.666666666666666),
# ('linear constraints', 4.5),
# ('natural numbers', 4.0),
# ('strict inequations', 4.0),
# ('nonstrict inequations', 4.0),
# ('upper bounds', 4.0),
# ('mixed types', 3.666666666666667),用愛斯佩蘭托(Esperanto)撰寫的文字(有關自由主義的文章)。沒有此語言的停止詞列表,它們將從提供的文本中生成。
text由三個介紹的第一段組成。 text_for_stopwords所有其他文本。
text = (
'Liberalismo estas politika filozofio aŭ mondrigardo konstruita en '
'ideoj de libereco kaj egaleco. Liberaluloj apogas larĝan aron de '
'vidpunktoj depende de sia kompreno de tiuj principoj, sed ĝenerale '
'ili apogas ideojn kiel ekzemple liberaj kaj justaj elektoj, '
'civitanrajtoj, gazetara libereco, religia libereco, libera komerco, '
'kaj privata posedrajto. Liberalismo unue iĝis klara politika movado '
'dum la Klerismo, kiam ĝi iĝis populara inter filozofoj kaj '
'ekonomikistoj en la okcidenta mondo. Liberalismo malaprobis heredajn '
'privilegiojn, ŝtatan religion, absolutan monarkion kaj la Didevena '
'Rajto de Reĝoj. La filozofo John Locke de la 17-a jarcento ofte '
'estas meritigita pro fondado de liberalismo kiel klara filozofia '
'tradicio. Locke argumentis ke ĉiu homo havas naturon rekte al vivo, '
'libereco kaj posedrajto kaj laŭ la socia '
'kontrakto, registaroj ne rajtas malobservi tiujn rajtojn. '
'Liberaluloj kontraŭbatalis tradician konservativismon kaj serĉis '
'anstataŭigi absolutismon en registaroj per reprezenta demokratio kaj '
'la jura hegemonio.'
)
rake = Rake ( max_words_unknown_lang = 3 )
keywords = rake . apply ( text , text_for_stopwords = other_text )
print ( keywords )
# ('serĉis anstataŭigi absolutismon', 9.0) # sought to replace absolutism
# ('filozofo john locke', 8.5), # philosopher John Locke
# ('locke argumentis', 4.5) # Locke argues
# ('justaj elektoj', 4.0), # fair elections
# ('libera komerco', 4.0), # free trade
# ('okcidenta mondo', 4.0), # western world
# ('ŝtatan religion', 4.0), # state religion
# ('absolutan monarkion', 4.0), # absolute monarchy
# ('didevena rajto', 4.0), # Dominican Rights
# ('socia kontrakto', 4.0), # social contract
# ('jura hegemonio', 4.0), # legal hegemony
# ('mondrigardo konstruita', 4.0) # worldview built
# ('vidpunktoj depende', 4.0), # views based
# ('sia kompreno', 4.0), # their understanding
# ('tiuj principoj', 4.0), # these principles
# ('gazetara libereco', 3.5), # freedom of press
# ('religia libereco', 3.5), # religious freedom
# ('privata posedrajto', 3.5), # private property
# ('libereco', 1.5), # liberty
# ('posedrajto', 1.5)] # property因此,我們能夠在沒有明確的停止詞的情況下獲得體面的結果。
初始化耙子對象
from multi_rake import Rake
rake = Rake (
min_chars = 3 ,
max_words = 3 ,
min_freq = 1 ,
language_code = None , # 'en'
stopwords = None , # {'and', 'of'}
lang_detect_threshold = 50 ,
max_words_unknown_lang = 2 ,
generated_stopwords_percentile = 80 ,
generated_stopwords_max_len = 3 ,
generated_stopwords_min_freq = 2 ,
)MIN_CHARS-如果其長度為> = min_chars,則選擇Word作為關鍵字的一部分。默認3
max_words-被認為是關鍵字的短語中的最大單詞數。默認3
MIN_FREQ-將視為關鍵字的短語發生的最小數量。默認1
Lanaging_code-提供語言代碼作為字符串,以使用內置的停止字樣。請參閱可用語言列表。如果未指定語言,算法將嘗試使用CLD2確定語言,並使用相應的內置停止字樣。默認沒有
停止詞- 提供自己的停止詞的集合(最好是集合,下降)。覆蓋language_code (如果指定)。默認沒有
將language_code和stopwords保留為None ,並且將從提供的文本中生成停止字。
lang_detect_threshold- cld2中檢測到的語言概率的閾值(0-100)。默認50
max_words_unknown_lang-與max_words相同,但如果語言未知並且從提供的文本生成停止字詞,則將使用。通常,當使用專門精心製作的停止字樣時,獲得最佳結果,如果它不存在並且使用產生的止版詞的使用情況可能不那麼漂亮,例如,可能是一個好主意,例如,為未知語言製作2個字的關鍵字,以及使用PEDEDEDEDENED SEETSETSETSSETS SETSSETS的語言進行2字關鍵字。默認2
生成的_stopwords_percentile-要生成stopwords,我們按頻率創建每個單詞的分佈。高於此百分位數(0-100)的單詞將被視為候選人成為停止詞。默認80
生成的_STOPWORDS_MAX_LEN-生成的停止詞的最大字符長度。默認3
生成的_STOPWORDS_MIN_FREQ-發行中生成的停止詞的最小頻率。默認2
將耙子對象應用於文本。
keywords = rake . apply (
text ,
text_for_stopwords = None ,
)文本- 包含應生成關鍵字的文本的字符串。
text_for_stopwords-包含文本的字符串將用於與text一起生成stopwords。例如,您的文章帶有介紹和幾個小節。您知道,出於您的目的,介紹的關鍵字就足夠了,您不知道文本語言,也不知道停止文字的列表。因此,可以從文本本身以及您擁有的文本越多,就可以生成停止詞。比您可能指定的text=introduction, text_for_stopwords=rest_of_your_text 。
Rake算法如Rose S.,Engel,D.,Cramer,N。和Cowley,W。 (2010)所述的工作。從各個文檔中提取自動關鍵字。在MW Berry&J。 Kogan(編輯)中,文本挖掘:理論與應用:John Wiley&Sons
通過其多語言支持,這種實現與其他實現不同。基本上,您可以在不知道其語言的情況下提供文本(應該用西里爾語或拉丁字母編寫),而無需明確的停止詞列表並獲得體面的結果。儘管最佳的結果是通過徹底構建的止詞列表來實現的。
引擎蓋下發生了什麼:
text和text_for_stopwords生成的我們通過在文本中創建單詞的頻率分佈並使用參數generated_stopwords_percentile , generated_stopwords_max_len , generated_stopwords_min_freq來生成停止字詞。我們將無法完美地生成它們,但是很容易找到文章和介詞,因為它們通常由3-4個字符組成,並且經常出現。這些停止詞,再加上標點符號的定界符,使我們能夠為我們不了解的語言獲得體面的結果。
在耙初始化期間,僅應使用語言代碼。
存儲庫已經配置了襯裡,測試和覆蓋範圍。
在Multi_rake文件夾中創建新的虛擬環境以使用它。
python3 -m venv env
source env/bin/activate
make install-dev # install dependencies
make lint # run linter
make test # run tests and coverage Rake算法:Rose,S.,Engel,D.,Cramer,N。 ,&Cowley,W。 (2010)。從各個文檔中提取自動關鍵字。在MW Berry&J。 Kogan(編輯)中,文本挖掘:理論與應用:John Wiley&Sons
作為Fabianvf實施的基礎耙子。
停止詞:TREC-KBA,排名NL