Skip to main content

weibo-preprocess-toolkit

Weibo Preprocess Toolkit

Getting Started

Installation

pip install weibo-preprocess-toolkit

Tutorial

from weibo_preprocess_toolkit import WeiboPreprocess

preprocess = WeiboPreprocess()

test_weibo = "所以我都不喝蒙牛 #南京·大行宫[地点]#,一直不喜欢蒙牛。謝駿毅 赞[122]转发[11] [超话] 收藏09月11日 18:57 "

# traditional2simplified
print(preprocess.traditional2simplified(test_weibo))
# 所以我都不喝蒙牛 #南京·大行宫[地点]#,一直不喜欢蒙牛。谢骏毅 赞[122]转发[11] [超话] 收藏09月11日 18:57

# clean weibo with simplified Chinese
print(preprocess.clean(test_weibo))
# 所以我都不喝蒙牛 一直不喜欢蒙牛 谢骏毅

# clean weibo 
print(preprocess.clean(test_weibo, simplified=False))
# 所以我都不喝蒙牛 一直不喜欢蒙牛 謝駿毅

# seg weibo, keep stop words
print(preprocess.cut(test_weibo))
# ['所以', '我', '都', '不喝', '蒙牛', '#', '南京', '·', '大行宫', '[', '地点', ']', '#', ',', '一直', '不喜欢', '蒙牛', '。', '謝駿毅', '赞', '[', '122', ']', '转发', '[', '11', ']', '[', '超话', ']', '收藏', '09', '月', '11', '日', '18', ':', '57', '\xa0']

# seg weibo, don't keep stop words
print(preprocess.cut(test_weibo, keep_stop_word=False))
# ['都', '不喝', '蒙牛', '#', '南京', '·', '大行宫', '[', '地点', ']', '#', ',', '不喜欢', '蒙牛', '。', '謝駿毅', '赞', '[', '122', ']', '转发', '[', '11', ']', '[', '超话', ']', '收藏', '09', '月', '11', '日', '18', ':', '57', '\xa0']

# clean and cut weibo, keep_stop_words, simplified Chinese
print(preprocess.preprocess(test_weibo))
# 所以 我 都 不喝 蒙牛 一直 不喜欢 蒙牛 谢骏毅
print(preprocess.preprocess(test_weibo, simplified=False, keep_stop_word=False))
# 都 不喝 蒙牛 不喜欢 蒙牛 謝駿毅

Introduction

该工具用于微博文本的预处理:清洗 + 分词。

Inspiration

在中文 NLP 领域,文本清洗和分词对于模型的性能有着很大的影响,如果语料库和测试集/线上环境的文本清洗规则和分词工具不同,就会导致在语料库上训练出来的模型在测试集上效果很差。举例来说,语料库采用了清洗规则 Clean-A 和 分词工具 Seg-A 来清洗和分词微博,而用户在线上环境采用了另一种清洗规则 Clean-B 和另一种分词工具 Seg-B,那么线上环境就会产生很多不在语料库词典中的未登陆词(Unknown Words),这些未登陆词会导致预先训练好的模型,面对线上环境的另一种规则时,性能变差。

本人在对微博进行情感分析的过程中,总结了较多的微博清洗技巧和分词规则,并总结了一份微博情感分析词典用于优化 jieba 分词。所以我在这里尝试对微博的清洗和分词规则进行整理,同时也是为了保持语料库和线上环境的规则同步,为其他研究者和使用我的模型的人,提供一个和语料库匹配的清洗和分词规则。

Weibo Cleaning

本人对微博文本的清洗规则进行了整理,主要涉及到如下的规则:

  1. 中文繁体转简体
  2. 微博停用词规则1(正则表达式),包括 url, email, @某人, 地点,…… 等停用词规则
  3. 微博停用词规则2(正则表达式),包括 时间,数字和微博中常出现的无意义的词等停用词规则
  4. 微博特殊字符
  5. 其他细节处理

注意:考虑到停用词在词向量训练中衔接上下文的作用,本工具并没有对微博的停用词进行清洗

Weibo Seg

基于 jieba 分词对微博文本进行分词优化,优化的地方主要有两点:

  1. 扩种 jieba 分词词典,构建情感词典,优化情感分析的分词结果
  2. 对否定前缀词进行特殊处理

Dependencies

pip install jieba

Acknowledgment

jieba 结巴中文分词

nstools 中文繁体转简体

NTUSD 情感词典

哈工大停用词表

License

MIT

Metadata

Release files for weibo-preprocess-toolkit 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for weibo-preprocess-toolkit 1.1.0
File Size Uploaded Provenance Actions
weibo-preprocess-toolkit-1.1.0.tar.gz 200.3 kB Without provenance Details for weibo-preprocess-toolkit-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for weibo-preprocess-toolkit 1.1.0
File Interpreter ABI Platform Actions
weibo_preprocess_toolkit-1.1.0-py3-none-any.whl Python 3 none any Details for weibo_preprocess_toolkit-1.1.0-py3-none-any.whl

Total release size: 455.1 kB

Release files / weibo-preprocess-toolkit-1.1.0.tar.gz

Download URL weibo-preprocess-toolkit-1.1.0.tar.gz
Size 200.3 kB
Tags Source
SHA-256 checksum
How to use checksums
aa33bcc820c810aa5d4007ef0f903e41f4df66a1029c188e883996f78cb6bf7a
BLAKE2b-256 checksum
How to use checksums
c7e066ae4ab2cd1412689919135e7b5fbbbb5ee435df82c4df6ead5cdcc31890
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.0

Release files / weibo_preprocess_toolkit-1.1.0-py3-none-any.whl

Download URL weibo_preprocess_toolkit-1.1.0-py3-none-any.whl
Size 254.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c1ee6e0d1ca45e47a5afdf7f52f27b6efdca2203669230af56b9caa857fc37c0
BLAKE2b-256 checksum
How to use checksums
f2fa1ac334eeeef0546c8c4752435cb037995564272e1edb1b4354bcd4952272
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.0

Security for weibo-preprocess-toolkit 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page