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

mecab-ffi

v0.2.0

Published

A node.js module for binding MeCab asynchronously using foreign function interface.

Downloads

16

Readme

node-mecab-ffi

A node.js module for binding MeCab asynchronously using foreign function interface. This module supports MacOS(surely Unix/Linux) and multi-thread safety.

If you want to use this module for Korean, please read the Korean README

Installation

node-mecab-ffi depends on MeCab v0.996 or higher.

Warning: To use libmecab in MacOS, you must install the newest gcc, automake and autoconf first and compile MeCab and dictonary with it. Refer to here. Otherwise it could split errors which cannot find dictionary directory or showing abort trap when you try to parse input strings.

Via npm:

$ npm install mecab-ffi

Via git directly:

$ npm install git+https://github.com/xissy/node-mecab-ffi.git

Quick Start

Load in the module

var mecab = require('mecab-ffi');

Parse a string

Asynchronously

//Korean Example
mecab.parse('한글 형태소 분석기를 테스트', function(err, result) {
  console.log (result);
});

//Result
[ [ '한글', 'NNG', '*', 'T', '한글', '*', '*', '*', '*', '*' ],
[ '형태소',
  'NNG',
  '*',
  'F',
  '형태소',
  'Compound',
  '*',
  '*',
  '형태+소',
  '형태/NNG/*/1/1+형태소/Compound/*/0/2+소/NNG/*/1/1' ],
[ '분석기',
  'NNG',
  '*',
  'F',
  '분석기',
  'Compound',
  '*',
  '*',
  '분석+기',
  '분석/NNG/*/1/1+분석기/Compound/*/0/2+기/NNG/*/1/1' ],
[ '를', 'JKO', '*', 'T', '를', '*', '*', '*', '*', '*' ],
[ '테스트', 'NNG', '*', 'F', '테스트', '*', '*', '*', '*', '*' ] ]

//Japaness Example
mecab.parse('ホリエモン市', function(err, result) {
  console.log (result);
});

//Result
[ [ 'ホリエモン', 'SL', '*', '*', '*', '*', '*', '*', '*', '*' ],
  [ '市', 'NNG', '*', 'F', '시', '*', '*', '*', '*', '*' ] ]

Synchronously

result = mecab.parseSync('한글 형태소 분석기를 테스트');
console.log (result);

//Result

[ [ '한글', 'NNG', '*', 'T', '한글', '*', '*', '*', '*', '*' ],
  [ '형태소',
	'NNG',
	'*',
	'F',
	'형태소',
	'Compound',
	'*',
	'*',
	'형태+소',
	'형태/NNG/*/1/1+형태소/Compound/*/0/2+소/NNG/*/1/1' ],
  [ '분석기',
	'NNG',
	'*',
	'F',
	'분석기',
	'Compound',
	'*',
	'*',
	'분석+기',
	'분석/NNG/*/1/1+분석기/Compound/*/0/2+기/NNG/*/1/1' ],
  [ '를', 'JKO', '*', 'T', '를', '*', '*', '*', '*', '*' ],
  [ '테스트', 'NNG', '*', 'F', '테스트', '*', '*', '*', '*', '*' ] ]

Extract nouns

Noun list

mecab.extractNounMap('한글 형태소 분석기를 테스트', function(err, result) {
	console.log (result);
})

//Result
{ '한글': 1, '한글 형태소': 1, '형태소': 1, '형태소 분석기': 1, '분석기': 1, '테스트': 1 }

Noun Map

var string = "황사바람 부는 거리에서 전경들이 점심을 먹는다.";
mecab.extractNounMap(string, function(err, result) {
	console.log (result);
})

//Result
{ '황사': 1,
  '황사 바람': 1,
  '바람': 1,
  '바람 부': 1,
  '부': 1,
  '거리': 1,
  '전경': 1,
  '점심': 1 }

Noun list sorted by count

var string = "황사바람 부는 거리에서 전경들이 점심을 먹는다.";
mecab.extractSortedNounCounts(string, function(err, result) {
	console.log (result);
});

//Result
[ { noun: '황사', count: 1 },
  { noun: '황사 바람', count: 1 },
  { noun: '바람', count: 1 },
  { noun: '바람 부', count: 1 },
  { noun: '부', count: 1 },
  { noun: '거리', count: 1 },
  { noun: '전경', count: 1 },
  { noun: '점심', count: 1 } ]

Get Dice-Coefficient

by two strings

var string1 = "황사바람 부는 거리에서 전경들이 점심을 먹는다.";
var string2 = "강풍이 불고 거리에서 전경이 밥을 먹는다.";
mecab.getDiceCoefficientByString(string1, string2, function(err, result) {
	console.log (result);
});

//Result 
2

by two noun maps

You muse use three Async functions to make two maps and to compare. Without questions, you can use callback as below:

var string1 = "황사바람 부는 거리에서 전경들이 점심을 먹는다.",
    string2 = "강풍이 불고 거리에서 전경이 밥을 먹는다.",
    shareData = {};

mecab.extractNounMap(string1, function (err, mapA) {
    shareData["mapA"] = mapA;
    mecab.extractNounMap(string2, function (err, mapB) {
        shareData["mapB"] = mapB;
        mecab.getDiceCoefficientByNounMap(shareData.mapA, shareData.mapB, function(err, result) {
            console.log (result);
	   });
    }); 
});

//Result
2

You, however, can use Promise patter, new ara of javascript with ES6 or promise library like bluebird. With bluebird, the code is easier to read and understand.

var mecab = require('mecab-ffi'),
  Promise = require('bluebird'),
  join = Promise.join;

var fAsyncExtractNounMap = Promise.promisify(mecab.extractNounMap),
    fAsyncgetDiceCoefficientByNounMap = Promise.promisify(mecab.getDiceCoefficientByNounMap),
    string1 = "황사바람 부는 거리에서 전경들이 점심을 먹는다.",
    string2 = "강풍이 불고 거리에서 전경이 밥을 먹는다.";

var fMap1 = fAsyncExtractNounMap(string1),
    fMap2 = fAsyncExtractNounMap(string2);

join (fMap1, fMap2, function (map1, map2) {
    return fAsyncgetDiceCoefficientByNounMap (map1, map2);
}).then(function (result) {
    console.log (result)
})

//Result
2

License

Released under the MIT License

Copyright (c) 2013 Taeho Kim [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Bitdeli Badge