How to extract content keywords from python _python

Source: Internet
Author: User

This article illustrates how Python extracts content keywords. Share to everyone for your reference. The specific analysis is as follows:

A very efficient extraction of content keyword Python code, this code can only be used in English article content, Chinese because to participle, this piece of code can do nothing, but to add participle function, the effect and English is the same.

Copy Code code 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 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 we 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 are 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 ()

I hope this article will help you with your Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.