Code Monkey home page Code Monkey logo

ckip.py's Introduction

CKIP.py

CKIP.py is a simple interface for the services provided by CKIP.

Usage

CKIP.py provides two classes, CKIPSegmenter and CKIPParser, to access the Chinese segmenter and the Chinese parser, respectively.

To create an instance of these classes, you must import the CKIPSegmenter class and/or the CKIPParser class from the ckip module, and then pass your username and password to the constructor:

from ckip import CKIPSegmenter, CKIPParser

segmenter = CKIPSegmenter('YOUR USERNAME', 'YOUR PASSWORD')
parser = CKIPParser('YOUR USERNAME', 'YOUR PASSWORD')

Then, you can use the process() method to process the given string:

segmented_result = segmenter.process('這是一隻可愛的小花貓')

or

parsed_result = parser.process('這是一隻可愛的小花貓')

This method returns a dictionary of the processed result:

{
    'status': 'Success',
    'status_code': '0',
    'result':
        [
            [
                {'term': u'這', 'pos': u'DET'},
                {'term': u'是', 'pos': u'Vt'},
                {'term': u'一', 'pos': u'DET'},
                {'term': u'隻', 'pos': u'M'},
                {'term': u'可愛', 'pos': u'Vi'},
                {'term': u'的', 'pos': u'T'},
                {'term': u'小', 'pos': u'Vi'},
                {'term': u'花貓', 'pos': u'N'}
            ]
        ]
}

The status and the status_code indicate whether the process is success or not:

if segmented_result['status_code'] != '0':
    print('Process Failed: ' + segmented_result['status'])

And the result is a list of objects that represent each sentence.

Takes the result of the CKIPSegmenter.process() for example, the sentence is represented by a list of dictionary. Each dictionary contains the Chinese term and the corresponding part-of-speech:

for sentence in segmented_result['result']:
    for term in sentence:
        print(term['term'], term['pos'])

The sentence in the result of the CKIPParser.process(), on the other hand, is represented by a parsing tree:

{
    'punctuation': None,
    'tree':
        {
            'head': {'term': u'是', 'pos': u'Vt'},
            'pos': u'S',
            'child':
                [
                    {
                        'head': {'term': u'這', 'pos': u'DET'},
                        'pos': u'NP',
                        'child':
                            [
                                {'term': u'這', 'pos': u'DET'}
                            ]
                    },
                    {'term': u'是', 'pos': u'Vt'},
                    {
                        'head': {'term': u'花貓', 'pos': u'N'},
                        'pos': u'NP',
                        'child':
                             [
                                 {'term': u'一隻', 'pos': u'DM'},
                                 {
                                     'head': {'term': u'的', 'pos': u'T'},
                                     'pos': u'V‧的',
                                     'child':
                                         [
                                             {'term': u'可愛', 'pos': u'Vi'},
                                             {'term': u'的', 'pos': u'T'}
                                         ]
                                 },
                                 {'term': u'小', 'pos': u'Vi'},
                                 {'term': u'花貓', 'pos': u'N'}
                             ]
                    }
                ]
        }
}

The punctuation is a dictionary like {'term': u'。', 'pos': u'PERIODCATEGORY'}, which represents the symbol that used to separate from next sentence, or None if there was no punctuation in this sentence.

tree is a dictionary that represent the tree structure. Each node has its own part-of-speech, and its children nodes (if this node is an internal node) or term (if this node is a leaf node).

Here is a simple example for traversing all leaf nodes (each of these is a Chinese term) of the parsing tree:

def traverse(root):
    if 'child' in root:
        for child in root['child']:
            for leaf in traverse(child):
                yield leaf
    else:
        yield root

for sentence in parsed_result['result']:
    for term in traverse(sentence['tree']):
        print(term['term'], term['pos'])

License

Copyright (c) 2012-2014, Chi-En Wu.

Distributed under The BSD 3-Clause License.

ckip.py's People

Watchers

James Cloos avatar Bolin, Lin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.