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

node_fasttext

v0.0.7

Published

FastText for Nodejs

Downloads

4

Readme

Node_FastText

带监督的文本分类算法FastText

安装

npm install node_fasttext

使用

const fastText = require("node_fasttext");
const FastText = new fastText();

const options = {
    "input" : "./train.txt",
    "output" : "./model",
    "loss" : "softmax",
    "dim" : 200,
    "bucket" : 2000000
}

FastText.train("supervised" ,  options ).then( res => {
    console.log(res);
})

API

模型训练 FastText.train( type , options )

详细配置参数查看官方 配置文档

/**
* loadModel
* @param String type 模型类型 [supervised | cbow | skipgram]
* @param Object options 配置参数
* @return Promise 返回模型信息
*/

FastText.train("supervised" ,{
    "input" : "./train.txt",
    "output" : "./model",
}).then( res => {
    console.log(res);
    // { bucket: 2000000,
    //   dim: 100,
    //   epoch: 5,
    //   label: '__label__',
    //   label_count: 2,
    //   loss: 'ns',
    //   lr: 0.05,
    //   lrUpdateRate: 100,
    //   maxn: 6,
    //   minCount: 5,
    //   minCountLabel: 0,
    //   minn: 3,
    //   model: 'cbow',
    //   neg: 5,
    //   pretrainedVectors: '',
    //   t: 0.0001,
    //   thread: 12,
    //   token_count: 1209,
    //   verbose: 2,
    //   wordNgrams: 1,
    //   word_count: 27,
    //   ws: 5 }
})

测试分类 FastText.test( testFile , k )

必须先加载 supervised 模型,详细请查看官方 文档

/**
* nn
* @param String testFile 待测试分类的文件
* @param Number k 精度和查全率,默认值为 1
* @return Promise
*/

FastText.test( "./test.txt" , 2 ).then( res => {
    console.log(res)
    // { 'Number of examples': 68, 'P@3': 0.333333, 'R@3': 1 }
})

压缩模型 FastText.quantize( options )

必须先训练模型,output 文件名必须与原模型名相同, 详细请查看官方 配置文档

/**
* quantize
* @param Object options 配置参数
* @return Promise 返回模型信息
*/
FastText.quantize({
    "input" : "./train.txt",
    "output" : "./model",
}).then( res => {
    console.log(res)
})

加载模型 FastText.loadModel( file )

/**
* loadModel
* @param String file 模型文件路径
* @return Promise 返回模型信息
*/

FastText.loadModel("./model.bin").then( res => {
    console.log(res);
})

文本分类 FastText.predict( text , k )

模型必须是 supervised,详细请查看官方 文档

/**
* predict
* @param String text 文本内容
* @param Number k 按相似度降序排列,返回 n 个分类,默认值为 1
* @return Promise
*/

FastText.predict("This is Band of Outsiders on Twitter" , 2 ).then( res => {
    console.log(res);
    // [ { label: '__label__BAND', value: 0.49999999904767284 },
    // { label: '__label__ORGANIZATION', value: 0.49804691366168485 } ]
})

相似词查询 FastText.nn( word , k )

模型必须是 cbow 或 skipgram,详细请查看官方 文档

/**
* nn
* @param String word 词
* @param Number k 按相似度降序排列,返回 n 个相似的词,默认值为 1
* @return Promise
*/

FastText.nn( "word" , 2 ).then( res => {
    console.log(res)
    // [ { word: 'The', value: 1.3640304837694894 },
    // { word: 'you', value: 1.2910933389370642 } ]
})

计算两个词语的语义距离 FastText.similarity( what , with )

模型必须是 cbow 或 skipgram

/**
* similarity
* @param String what 一个词
* @param String with 另一个词
* @return Promise 余弦距离
*/

FastText.analogies( "山东", "江苏" ).then( res => {
    console.log(res)
    // 0.81871825
})

词语类比查询 FastText.analogies( words , k )

模型必须是 cbow 或 skipgram,详细请查看官方 文档

/**
* analogies
* @param Array words 词语,必须为3个词语
* @param Number k 按相似度降序排列,语义距离最近 n 个相似的词,默认值为 1
* @return Promise 
*/

FastText.analogies( ["berlin","germany","france"] , 2 ).then( res => {
    console.log(res)
    // [ { word: 'paris', value: 0.768954 },
    // { word: 'louveciennes', value: 0.765569 } ]
})

获取向量 FastText.getVector( text )

/**
* getVector
* @param String text 词语或句子
* @return Promise 
*/

FastText.getVector( "paris").then( res => {
    console.log(res)
    // { text: 'paris', value: [
    //     -0.49160531163215637,
    //     0.08759436011314392,
    //     0.12446501106023788,
    //     -0.21982385218143463,
    //     ...
    //     -0.2687559723854065
    // ]},
})