npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zh-classify

v0.1.1

Published

Chinese text classify

Downloads

4

Readme

zh-classify 中文文本分类

基于Node的中文文本分类器,内置中文情感分析模型,使用nodejieba分词

Features

  • [x] 基于Bayes的中文情感分析
  • [x] 基于Bayes的中文文本分类(多分类)

Install

Node > 7.6

npm install zh-classify

Guide

Bayes 中文情感分析

默认会加载一个中文情感极性分析的模型,使用购物外卖酒店评价微博语料 正负语料比例31197/32555

const Bayes = require('zh-classify').Bayes;

// 实例
const sentiment = new Bayes();

console.log(sentiment.clf('这个天气也太差了'));
// { neg: 0.9731274375543317, pos: 0.026872562445668324 }

Bayes 中文文本分类

训练模型

将分类文件放入一个分类文件夹(dirName)中 将每个类别的语料一个单独的txt文件,文件名会成为类别名

所以模型的分类数量和取决于文件的数量,例如下图会生成一个9分类的模型

每个类别文件内部每篇内容单独一行,utf-8编码

会在语料文件夹下生成dirName-model-bayes.json的模型文件

const Bayes = require('zh-classify').Bayes;

// 实例
const bayes = new Bayes();

// 训练模型
bayes.train('./cropus/news');


// 创建模型成功,路径:./cropus/news/news-model-bayes.json
// 耗时97秒

使用模型 创建分类器实例时调用模型路径,使用分类器

const Bayes = require('zh-classify').Bayes;

// 实例
const bayes = new Bayes('./cropus/news/news-model-bayes.json');

console.log(bayes.clf('好像有点感冒,我得吃点药了'));

// { '体育': 0.00032818303607162817,
//   '军事': 0.000012199216160954406,
//   '医疗': 0.9995621094898073,
//   '政治': 0.0000016534488152032236,
//   '教育': 0.00000541675514383486,
//   '环境': 0.00007680840939844948,
//   '经济': 0.000002647388656538752,
//   '艺术': 0.000010726221785414526,
//   '计算机': 2.5603416048904845e-7 }

Test

速度

python SnowNLP情感分析相同大小语料库进行对比

const Bayes = require('zh-classify').Bayes;

// 实例
const sentiment = new Bayes();


//执行10000次时间
let now = new Date();
for(let i=0;i<10000;i++){
    sentiment.clf('今天天气不好');
}


console.log(new Date().valueOf() - now.valueOf());
// 4868

大概4-5倍速度快于Python

from snownlp import SnowNLP
from datetime import datetime as date

sent = u'今天天气不好'

s = SnowNLP(sent)

# 测试10000耗时
now = date.now()
for i in range(10000):
    s.sentiments

print date.now() - now
# 0:00:15.238370

联系

[email protected]