Skip to content

JCLdic Usage

BrambleXu edited this page May 22, 2020 · 5 revisions

JCLdic Usage

CSV format Usage

jcldic_csv_usage.py

MeCab format Usage

Preparation

Downlaod MeCab and mecab-python3 first. I recommend this article, MeCab Usage and Add User Dictionary to MeCab, which contains related content.

We take the JCL_slim MeCab Dic for instruction.

Create a new directory to store user dictionary and move the jcl_slim_mecab.dic to the directory:

$ mkdir /usr/local/lib/mecab/dic/user_dict
$ mv jcl_slim_mecab.dic /usr/local/lib/mecab/dic/user_dict

Change mecabrc file:

vim /usr/local/etc/mecabrc

Below is my mecabrc setting. The ; notation means comment. dicdir is the system dictionary path, and userdic is the user dictionary path.

dicdir =  /usr/local/lib/mecab/dic/ipadic
;dicdir =  /usr/local/lib/mecab/dic/mecab-ipadic-neologd
;dicdir = /usr/local/lib/mecab/dic/jumandic
;dicdir = /usr/local/lib/mecab/dic/unidic

userdic = /usr/local/lib/mecab/dic/user_dict/jcl_slim_mecab.dic
; output-format-type = wakati
; input-buffer-size = 8192

; node-format = %m\n
; bos-format = %S\n
; eos-format = EOS\n

If you have multiple user dictionary files, you could write them in one line. I split the JCL full version to two files because MeCab cannot compile the single file due to the large file size.

userdic = /usr/local/lib/mecab/dic/user_dict/jcl_full_mecab_1.dic,/usr/local/lib/mecab/dic/user_dict/jcl_full_mecab_2.dic

Command line

Before adding userdic:

echo "TISインテックグループのTIS株式会社は、自然言語処理で企業名認識を行うための辞書JCLdic(日本会社名辞書)を無償公開。" | mecab

TIS     名詞,一般,*,*,*,*,*
インテック      名詞,固有名詞,組織,*,*,*,インテック,インテック,インテック
グループ        名詞,一般,*,*,*,*,グループ,グループ,グループ
の      助詞,連体化,*,*,*,*,の,ノ,ノ
TIS     名詞,一般,*,*,*,*,*
株式会社        名詞,一般,*,*,*,*,株式会社,カブシキガイシャ,カブシキガイシャ
は      助詞,係助詞,*,*,*,*,は,ハ,ワ
、      記号,読点,*,*,*,*,、,、,、
......
EOS

After adding userdic:

echo "TISインテックグループのTIS株式会社は、自然言語処理で企業名認識を行うための辞書JCLdic(日本会社名辞書)を無償公開。" | mecab

TIS     名詞,固有名詞,組織,*,*,*,有限会社TIS,*,*
インテック      名詞,固有名詞,組織,*,*,*,株式会社インテック,*,*
グループ        名詞,一般,*,*,*,*,グループ,グループ,グループ
の      助詞,連体化,*,*,*,*,の,ノ,ノ
TIS株式会社     名詞,固有名詞,組織,*,*,*,TIS株式会社,*,*
は      助詞,係助詞,*,*,*,*,は,ハ,ワ
、      記号,読点,*,*,*,*,、,、,、
......
EOS

Or we can use different user dictionary by -u option:

echo "TISインテックグループのTIS株式会社は、自然言語処理で企業名認識を行うための辞書JCLdic(日本会社名辞書)を無償公開。" | mecab -u /usr/local/lib/mecab/dic/user_dict/jcl_medium_mecab.dic

Python

Considering the trade-off between dictionary size and searching performance, we delete zenkaku(全角) names and only preserve the hankaku(半角) names. For example, we delete '株式会社KADOKAWA' but preserve '株式会社KADOKAWA'. If you deal with text with JCLdic, we recommend first normalize the text to hankaku format.

import unicodedata

text = unicodedata.normalize('NFKC', text) # convert zenkaku to hankaku

1 parse method

When creating Mecab tagger, we can specify system dictionary by -d option, and user dictionary by -u option.

# template
tagger = MeCab.Tagger('-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd -u /usr/local/lib/mecab/dic/userdic/userdic.dic')

The parsing code.

import unicodedata
import MeCab

# 1 specify user dictionary by -u option
# tagger = MeCab.Tagger('-u /usr/local/lib/mecab/dic/user_dict/jcl_slim_mecab.dic')

# 2 import multiple dictionaries by mecabrc
tagger = MeCab.Tagger('-r /usr/local/etc/mecabrc')

text = 'TISインテックグループのTIS株式会社は、自然言語処理で企業名認識を行うための辞書JCLdic(日本会社名辞書)を無償公開。'

# convert zenkaku to hankaku
text = unicodedata.normalize('NFKC', text) 

# parse
print(tagger.parse(text))

Output:

TIS     名詞,固有名詞,組織,*,*,*,有限会社TIS,*,*
インテック      名詞,固有名詞,組織,*,*,*,株式会社インテック,*,*
グループ        名詞,一般,*,*,*,*,グループ,グループ,グループ
の      助詞,連体化,*,*,*,*,の,ノ,ノ
TIS株式会社     名詞,固有名詞,組織,*,*,*,TIS株式会社,*,*
は      助詞,係助詞,*,*,*,*,は,ハ,ワ
、      記号,読点,*,*,*,*,、,、,、
...
EOS

2 parseToNode method

We can pick up the company names by 組織 keyword:

import unicodedata
import MeCab

# 1 specify dictionary by option
# tagger = MeCab.Tagger('-u /usr/local/lib/mecab/dic/user_dict/jcl_slim_mecab.dic')

# 2 import multiple dictionaries by mecabrc
tagger = MeCab.Tagger('-r /usr/local/etc/mecabrc')

text = 'TISインテックグループのTIS株式会社は、自然言語処理で企業名認識を行うための辞書JCLdic(日本会社名辞書)を無償公開。'

# convert zenkaku to hankaku
text = unicodedata.normalize('NFKC', text) 

# parse
node = tagger.parseToNode(text)
result = []

while node:
    # node feature map: 品詞,品詞細分類1,品詞細分類2,品詞細分類3,活用形,活用型,原形,読み,発音
    # example:   TIS: ['名詞', '固有名詞', '組織', '*', '*', '*', '有限会社TIS', '*', '*']
    if node.feature.split(",")[2] == '組織':
        result.append(node.surface)
    node = node.next

print(result)
# ['TIS', 'インテック', 'TIS株式会社']