Underthesea documentation

Vietnamese NLP Toolkit

Underthesea - Vietnamese NLP Toolkit

https://img.shields.io/pypi/v/underthesea.svg https://img.shields.io/pypi/pyversions/underthesea.svg https://img.shields.io/pypi/l/underthesea.svg https://img.shields.io/travis/magizbox/underthesea.svg Documentation Status Updates https://img.shields.io/badge/chat-on%20facebook-green.svg

[English] [Tiếng Việt]

https://raw.githubusercontent.com/magizbox/underthesea/master/logo.jpg

underthesea is a suite of open source Python modules, data sets and tutorials supporting research and development in Vietnamese Natural Language Processing.

Installation

To install underthesea, simply:

$ pip install underthesea==1.1.7a2
✨🍰✨

Satisfaction, guaranteed.

Usage

1. Word Segmentation

https://img.shields.io/badge/F1-94%25-red.svg https://img.shields.io/badge/✎-custom%20models-blue.svg https://img.shields.io/badge/★-api-green.svg

Usage

>>> # -*- coding: utf-8 -*-
>>> from underthesea import word_tokenize
>>> sentence = 'Chàng trai 9X Quảng Trị khởi nghiệp từ nấm sò'

>>> word_tokenize(sentence)
['Chàng trai', '9X', 'Quảng Trị', 'khởi nghiệp', 'từ', 'nấm', 'sò']

>>> word_tokenize(sentence, format="text")
'Chàng_trai 9X Quảng_Trị khởi_nghiệp từ nấm sò'

2. POS Tagging

https://img.shields.io/badge/accuracy-92.3%25-red.svg https://img.shields.io/badge/✎-custom%20models-blue.svg https://img.shields.io/badge/★-api-green.svg

Usage

>>> # -*- coding: utf-8 -*-
>>> from underthesea import pos_tag
>>> pos_tag('Chợ thịt chó nổi tiếng ở Sài Gòn bị truy quét')
[('Chợ', 'N'),
 ('thịt', 'N'),
 ('chó', 'N'),
 ('nổi tiếng', 'A'),
 ('ở', 'E'),
 ('Sài Gòn', 'Np'),
 ('bị', 'V'),
 ('truy quét', 'V')]

3. Chunking

https://img.shields.io/badge/F1-77%25-red.svg https://img.shields.io/badge/✎-custom%20models-blue.svg https://img.shields.io/badge/★-api-green.svg

Usage

>>> # -*- coding: utf-8 -*-
>>> from underthesea import chunk
>>> text = 'Bác sĩ bây giờ có thể thản nhiên báo tin bệnh nhân bị ung thư?'
>>> chunk(text)
[('Bác sĩ', 'N', 'B-NP'),
 ('bây giờ', 'P', 'I-NP'),
 ('có thể', 'R', 'B-VP'),
 ('thản nhiên', 'V', 'I-VP'),
 ('báo tin', 'N', 'B-NP'),
 ('bệnh nhân', 'N', 'I-NP'),
 ('bị', 'V', 'B-VP'),
 ('ung thư', 'N', 'I-VP'),
 ('?', 'CH', 'O')]

4. Named Entity Recognition

https://img.shields.io/badge/F1-86.6%25-red.svg https://img.shields.io/badge/✎-custom%20models-blue.svg https://img.shields.io/badge/★-api-green.svg

Usage

>>> # -*- coding: utf-8 -*-
>>> from underthesea import ner
>>> text = 'Chưa tiết lộ lịch trình tới Việt Nam của Tổng thống Mỹ Donald Trump'
>>> ner(text)
[('Chưa', 'R', 'O', 'O'),
 ('tiết lộ', 'V', 'B-VP', 'O'),
 ('lịch trình', 'V', 'B-VP', 'O'),
 ('tới', 'E', 'B-PP', 'O'),
 ('Việt Nam', 'Np', 'B-NP', 'B-LOC'),
 ('của', 'E', 'B-PP', 'O'),
 ('Tổng thống', 'N', 'B-NP', 'O'),
 ('Mỹ', 'Np', 'B-NP', 'B-LOC'),
 ('Donald', 'Np', 'B-NP', 'B-PER'),
 ('Trump', 'Np', 'B-NP', 'I-PER')]

5. Text Classification

https://img.shields.io/badge/accuracy-86.7%25-red.svg https://img.shields.io/badge/✎-custom%20models-blue.svg https://img.shields.io/badge/★-api-green.svg

Install dependencies and download default model

$ pip install Cython
$ pip install joblib future scipy numpy scikit-learn
$ pip install -U fasttext --no-cache-dir --no-deps --force-reinstall
$ underthesea data

Usage

>>> # -*- coding: utf-8 -*-
>>> from underthesea import classify
>>> classify('HLV đầu tiên ở Premier League bị sa thải sau 4 vòng đấu')
['The thao']
>>> classify('Hội đồng tư vấn kinh doanh Asean vinh danh giải thưởng quốc tế')
['Kinh doanh']
>>> classify('Đánh giá “rạp hát tại gia” Samsung Soundbar Sound+ MS750')
['Vi tinh']

6. Sentiment Analysis

https://img.shields.io/badge/F1-59.5%25-red.svg https://img.shields.io/badge/✎-custom%20models-blue.svg https://img.shields.io/badge/★-api-green.svg

Install dependencies

$ pip install future scipy numpy scikit-learn==0.19.0 joblib

Usage

>>> # -*- coding: utf-8 -*-
>>> from underthesea import sentiment
>>> sentiment('Gọi mấy lần mà lúc nào cũng là các chuyên viên đang bận hết ạ', domain='bank')
('CUSTOMER SUPPORT#NEGATIVE',)
>>> sentiment('bidv cho vay hay ko phu thuoc y thich cua thang tham dinh, ko co quy dinh ro rang', domain='bank')
('LOAN#NEGATIVE',)

Up Coming Features

  • Text to Speech
  • Automatic Speech Recognition
  • Machine Translation
  • Dependency Parsing

Contributing

Do you want to contribute with underthesea development? Great! Please read more details at CONTRIBUTING.rst.

Credits

Development Lead

Contributors

Developer Advocates

History

1.1.7.alpha (2018-04-12)

  • Rename word_sent function to word_tokenize
  • Refactor version control in setup.py file and __init__.py file
  • Update documentation badge url

1.1.6 (2017-12-26)

  • New feature: aspect sentiment analysis
  • Integrate with languageflow 1.1.6
  • Fix bug tokenize string with ‘=’ (#159)

1.1.5 (2017-10-12)

  • New feature: named entity recognition
  • Refactor and update model for word_sent, pos_tag, chunking

1.1.4 (2017-09-12)

  • New feature: text classification
  • [bug] Fix Text error
  • [doc] Add facebook link

1.1.3 (2017-08-30)

1.1.2 (2017-08-22)

  • Add dictionary

1.1.1 (2017-07-05)

  • Support Python 3
  • Refactor feature_engineering code

1.1.0 (2017-05-30)

  • Add chunking feature
  • Add pos_tag feature
  • Add word_sent feature, fix performance
  • Add Corpus class
  • Add Transformer classes
  • Integrated with dictionary of Ho Ngoc Duc
  • Add travis-CI, auto build with PyPI

1.0.0 (2017-03-01)

  • First release on PyPI.
  • First release on Readthedocs

word_tokenize

underthesea.word_tokenize.word_tokenize(sentence, format=None)[source]

Vietnamese word segmentation

Parameters:sentence ({unicode, str}) – raw sentence
Returns:tokens – tagged sentence
Return type:list of text

Examples

>>> # -*- coding: utf-8 -*-
>>> from underthesea import word_tokenize
>>> sentence = "Bác sĩ bây giờ có thể thản nhiên báo tin bệnh nhân bị ung thư"
>>> word_tokenize(sentence)
['Bác sĩ', 'bây giờ', 'có thể', 'thản nhiên', 'báo tin', 'bệnh nhân', 'bị', 'ung thư']
>>> word_tokenize(sentence, format="text")
'Bác_sĩ bây_giờ có_thể thản_nhiên báo_tin bệnh_nhân bị ung_thư'

pos_tag

underthesea.pos_tag.pos_tag(sentence, format=None)[source]

Vietnamese POS tagging

Parameters:sentence ({unicode, str}) – Raw sentence
Returns:tokens – tagged sentence
Return type:list of tuple with word, pos tag

Examples

>>> # -*- coding: utf-8 -*-
>>> from underthesea import pos_tag
>>> sentence = "Chợ thịt chó nổi tiếng ở TPHCM bị truy quét"
>>> pos_tag(sentence)
[('Chợ', 'N'),
('thịt', 'N'),
('chó', 'N'),
('nổi tiếng', 'A'),
('ở', 'E'),
('TPHCM', 'Np'),
('bị', 'V'),
('truy quét', 'V')]

chunking

underthesea.chunking.chunk(sentence, format=None)[source]

Vietnamese chunking

Parameters:sentence ({unicode, str}) – raw sentence
Returns:tokens – tagged sentence
Return type:list of tuple with word, pos tag, chunking tag

Examples

>>> # -*- coding: utf-8 -*-
>>> from underthesea import chunk
>>> sentence = "Nghi vấn 4 thi thể Triều Tiên trôi dạt bờ biển Nhật Bản"
>>> chunk(sentence)
[('Nghi vấn', 'N', 'B-NP'),
('4', 'M', 'B-NP'),
('thi thể', 'N', 'B-NP'),
('Triều Tiên', 'Np', 'B-NP'),
('trôi dạt', 'V', 'B-VP'),
('bờ biển', 'N', 'B-NP'),
('Nhật Bản', 'Np', 'B-NP')]

ner

underthesea.ner.ner(sentence, format=None)[source]

Location and classify named entities in text

Parameters:sentence ({unicode, str}) – raw sentence
Returns:tokens – tagged sentence
Return type:list of tuple with word, pos tag, chunking tag, ner tag

Examples

>>> # -*- coding: utf-8 -*-
>>> from underthesea import ner
>>> sentence = "Ông Putin ca ngợi những thành tựu vĩ đại của Liên Xô"
>>> ner(sentence)
[('Ông', 'Nc', 'B-NP', 'O'),
('Putin', 'Np', 'B-NP', 'B-PER'),
('ca ngợi', 'V', 'B-VP', 'O'),
('những', 'L', 'B-NP', 'O'),
('thành tựu', 'N', 'B-NP', 'O'),
('vĩ đại', 'A', 'B-AP', 'O'),
('của', 'E', 'B-PP', 'O'),
('Liên Xô', 'Np', 'B-NP', 'B-LOC')]

classify

Install dependencies and download default model

$ pip install Cython
$ pip install future scipy numpy scikit-learn
$ pip install -U fasttext --no-cache-dir --no-deps --force-reinstall
$ underthesea data
underthesea.classification.classify(X, domain=None)[source]

Text classification

Parameters:
  • X ({unicode, str}) – raw sentence
  • domain ({None, 'bank'}) –
    domain of text
    • None: general domain
    • bank: bank domain
Returns:

tokens – categories of sentence

Return type:

list

Examples

>>> # -*- coding: utf-8 -*-
>>> from underthesea import classify
>>> sentence = "HLV ngoại đòi gần tỷ mỗi tháng dẫn dắt tuyển Việt Nam"
>>> classify(sentence)
['The thao']
>>> sentence = "Tôi rất thích cách phục vụ của nhân viên BIDV"
>>> classify(sentence, domain='bank')
('CUSTOMER SUPPORT',)

sentiment

Install dependencies

$ pip install future scipy numpy scikit-learn==0.19.0 joblib
underthesea.sentiment.sentiment(X, domain=None)[source]

Sentiment Analysis

Parameters:
  • X ({unicode, str}) – raw sentence
  • domain ({'bank'}) –
    domain of text
    • bank: bank domain
Returns:

tokens – sentiment of sentence

Return type:

list

Examples

>>> # -*- coding: utf-8 -*-
>>> from underthesea import sentiment
>>> sentence = "Vừa smartbidv, vừa bidv online mà lại k dùng chung 1 tài khoản đăng nhập, rắc rối!"
>>> sentiment(sentence, domain='bank')
('INTERNET BANKING#NEGATIVE',)

Indices and tables