Python nltk extract useful chunk

Source: Internet
Author: User
Tags nltk

There is a lot of text information. How do we extract useful information?

For example:

JSON is a good boy

The expected information is JSON and a good boy.

First, we need to split sentences and determine the attributes of words:

You can use the followingCode:

 
Def ie_preprocess (document ):... sentences = nltk. sent_tokenize (document )... sentences = [nltk. word_tokenize (sent) for sent in sentences]... sentences = [nltk. pos_tag (sent) for sent in sentences]

Then we need to specify the types of information to be extracted:

That is, the syntax format:

 
Grammar = "NP: {<DT>? <JJ> * <NN> }"

DT is an attribute, JJ is an adjective, and NN is a noun.

 
CP = nltk. regexpparser (grammar)

Later use

 
Result = CP. parse (sentence)

Analyze statements

We will get a nltk. Tree. tree structure.

Then we use:

 
For N in chunked: If isinstance (n, nltk. Tree. Tree): If n. node = 'np ': A = N

This code gets the snippet we need

In reality, this method cannot remove non-English words.

We can add:

 
D = enchant. dict ("en_us ")

Such interpretation is used to delete and select.

I hope to help you.

Related Article

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.