欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python提取內(nèi)容關(guān)鍵詞的方法

 更新時間:2015年03月16日 10:29:09   作者:上大王  
這篇文章主要介紹了python提取內(nèi)容關(guān)鍵詞的方法,適用于英文關(guān)鍵詞的提取,非常具有實用價值,需要的朋友可以參考下

本文實例講述了python提取內(nèi)容關(guān)鍵詞的方法。分享給大家供大家參考。具體分析如下:

一個非常高效的提取內(nèi)容關(guān)鍵詞的python代碼,這段代碼只能用于英文文章內(nèi)容,中文因為要分詞,這段代碼就無能為力了,不過要加上分詞功能,效果和英文是一樣的。

復(fù)制代碼 代碼如下:

# coding=UTF-8
import nltk
from nltk.corpus import brown
# This is a fast and simple noun phrase extractor (based on NLTK)
# Feel free to use it, just keep a link back to this post
# http://thetokenizer.com/2013/05/09/efficient-way-to-extract-the-main-topics-of-a-sentence/
# Create by Shlomi Babluki
# May, 2013
 
# This is our fast Part of Speech tagger
#############################################################################
brown_train = brown.tagged_sents(categories='news')
regexp_tagger = nltk.RegexpTagger(
    [(r'^-?[0-9]+(.[0-9]+)?$', 'CD'),
     (r'(-|:|;)$', ':'),
     (r'\'*$', 'MD'),
     (r'(The|the|A|a|An|an)$', 'AT'),
     (r'.*able$', 'JJ'),
     (r'^[A-Z].*$', 'NNP'),
     (r'.*ness$', 'NN'),
     (r'.*ly$', 'RB'),
     (r'.*s$', 'NNS'),
     (r'.*ing$', 'VBG'),
     (r'.*ed$', 'VBD'),
     (r'.*', 'NN')
])
unigram_tagger = nltk.UnigramTagger(brown_train, backoff=regexp_tagger)
bigram_tagger = nltk.BigramTagger(brown_train, backoff=unigram_tagger)
#############################################################################
# This is our semi-CFG; Extend it according to your own needs
#############################################################################
cfg = {}
cfg["NNP+NNP"] = "NNP"
cfg["NN+NN"] = "NNI"
cfg["NNI+NN"] = "NNI"
cfg["JJ+JJ"] = "JJ"
cfg["JJ+NN"] = "NNI"
#############################################################################
class NPExtractor(object):
    def __init__(self, sentence):
        self.sentence = sentence
    # Split the sentence into singlw words/tokens
    def tokenize_sentence(self, sentence):
        tokens = nltk.word_tokenize(sentence)
        return tokens
    # Normalize brown corpus' tags ("NN", "NN-PL", "NNS" > "NN")
    def normalize_tags(self, tagged):
        n_tagged = []
        for t in tagged:
            if t[1] == "NP-TL" or t[1] == "NP":
                n_tagged.append((t[0], "NNP"))
                continue
            if t[1].endswith("-TL"):
                n_tagged.append((t[0], t[1][:-3]))
                continue
            if t[1].endswith("S"):
                n_tagged.append((t[0], t[1][:-1]))
                continue
            n_tagged.append((t[0], t[1]))
        return n_tagged
    # Extract the main topics from the sentence
    def extract(self):
        tokens = self.tokenize_sentence(self.sentence)
        tags = self.normalize_tags(bigram_tagger.tag(tokens))
        merge = True
        while merge:
            merge = False
            for x in range(0, len(tags) - 1):
                t1 = tags[x]
                t2 = tags[x + 1]
                key = "%s+%s" % (t1[1], t2[1])
                value = cfg.get(key, '')
                if value:
                    merge = True
                    tags.pop(x)
                    tags.pop(x)
                    match = "%s %s" % (t1[0], t2[0])
                    pos = value
                    tags.insert(x, (match, pos))
                    break
        matches = []
        for t in tags:
            if t[1] == "NNP" or t[1] == "NNI":
            #if t[1] == "NNP" or t[1] == "NNI" or t[1] == "NN":
                matches.append(t[0])
        return matches
# Main method, just run "python np_extractor.py"
def main():
    sentence = "Swayy is a beautiful new dashboard for discovering and curating online content."
    np_extractor = NPExtractor(sentence)
    result = np_extractor.extract()
    print "This sentence is about: %s" % ", ".join(result)
if __name__ == '__main__':
    main()

希望本文所述對大家的Python程序設(shè)計有所幫助。

相關(guān)文章

  • selenium?UI自動化實戰(zhàn)過程記錄

    selenium?UI自動化實戰(zhàn)過程記錄

    如果大家有做過web的自動化測試,相信對于selenium一定不陌生,測試人員經(jīng)常使用它來進行自動化測試,下面這篇文章主要給大家介紹了關(guān)于selenium?UI自動化實戰(zhàn)的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • python裝飾器實例大詳解

    python裝飾器實例大詳解

    這篇文章主要介紹了python裝飾器實例大詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-10-10
  • Python中l(wèi)ist列表的一些進階使用方法介紹

    Python中l(wèi)ist列表的一些進階使用方法介紹

    這篇文章主要介紹了Python中l(wèi)ist列表的一些進階使用方法介紹,是Python入門學習中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-08-08
  • 詳解pyqt5的UI中嵌入matplotlib圖形并實時刷新(挖坑和填坑)

    詳解pyqt5的UI中嵌入matplotlib圖形并實時刷新(挖坑和填坑)

    這篇文章主要介紹了詳解pyqt5的UI中嵌入matplotlib圖形并實時刷新(挖坑和填坑),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • python 獲取指定文件夾下所有文件名稱并寫入列表的實例

    python 獲取指定文件夾下所有文件名稱并寫入列表的實例

    下面小編就為大家分享一篇python 獲取指定文件夾下所有文件名稱并寫入列表的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python實現(xiàn)讀取excel表格詳解方法

    python實現(xiàn)讀取excel表格詳解方法

    python操作excel主要用到xlrd和xlwt兩個庫,xlrd讀取表格數(shù)據(jù),支持xlsx和xls格式的excel表格;xlwt寫入excel表格數(shù)據(jù)
    2022-07-07
  • 深入理解Pytorch中的torch. matmul()

    深入理解Pytorch中的torch. matmul()

    這篇文章主要介紹了Pytorch中的torch. matmul()的相關(guān)資料,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • 用Python實現(xiàn)隨機森林算法的示例

    用Python實現(xiàn)隨機森林算法的示例

    這篇文章主要介紹了用Python實現(xiàn)隨機森林算法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Flask中Cookie和Session理解與作用介紹

    Flask中Cookie和Session理解與作用介紹

    Flask是一個使用 Python 編寫的輕量級 Web 應(yīng)用框架。其 WSGI 工具箱采用 Werkzeug ,模板引擎則使用 Jinja2 。Flask使用 BSD 授權(quán)。Flask也被稱為 “microframework” ,因為它使用簡單的核心,用 extension 增加其他功能,F(xiàn)lask中Cookie和Session有什么區(qū)別呢
    2022-10-10
  • python GUI庫圖形界面開發(fā)之PyQt5布局控件QVBoxLayout詳細使用方法與實例

    python GUI庫圖形界面開發(fā)之PyQt5布局控件QVBoxLayout詳細使用方法與實例

    這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5布局控件QVBoxLayout詳細使用方法與實例,需要的朋友可以參考下
    2020-03-03

最新評論