Python method for extracting content keywords, python Method for Extracting keywords
This example describes how to extract content keywords from python. Share it with you for your reference. The specific analysis is as follows:
A very efficient python code that extracts content keywords. This code can only be used in English articles. Chinese characters cannot be used because of word segmentation. However, the word segmentation function must be added, the effect is the same as that in English.
Copy codeThe Code is as follows:
# 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 $ ', 'ns '),
(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" % (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 ()
I hope this article will help you with Python programming.