[PYTHON] Add a dictionary to MeCab

I'm sorry, I'm a beginner to post to Qiita, so I think the layout is strange. I would be grateful if you could give me some advice on layout and writing.

【Overview】

Procedure for adding a new word (user dictionary) to MeCab

【environment】

Mac OS X

【Notes】

  1. It is assumed that MeCab is installed
  2. The directory where the MeCab dictionary is stored differs depending on the environment, so check the directory location for each (in most cases, it is in / usr / local / lib / mecab / dic / ipadic).
  3. If you do not rewrite macabrc, you need to pass the path every time you execute the user dictionary (mecab -u user.dic). Open / usr / local / etc / mecabrc with an editor and rewrite as follows
 userdic	= /usr/local/lib/mecab/dic/ipadic/user.dic(left;Erase)

[File description]

1.create_dic.py (Program for creating newword_dic.csv from newword_list.txt for Python 3.X)

create_dic.py


if __name__ == "__main__":
        fin = open("newword_list.txt")
        fout = open("newword_dic.csv","w")
        for line in fin:
                line=line.rstrip('\n')
                word = line.split(',')
                cost = int(max(-36000, -400*len(word[0])**1.5))
                fout.write("%s,-1,-1,%d,noun,General,*,*,*,*,*,*,%s,%s\n" % (word[0],cost,word[1],word[1]))
        fin.close()
        fout.close()

2.newword_list.txt (list containing new words. Basically only this text file can be edited)

Sword Art Online, Sword Art Online Mankind has declined, mankind has declined Magical Girl Lyrical Nanoha, Magical Girl Lyrical Nanoha Artificial intelligence, artificial intelligence

  1. newword_dic.csv (MeCab format dictionary)

スクリーンショット 2016-10-07 14.07.13.png

【procedure】

  1. Add a new word to newword_list.txt as in the example (Example: Magical Girl Lyrical Nanoha, Mahousho Jorikaru Nanoha)
  2. Create a MeCab dictionary (newword_dic.csv) from newword_list.txt
  python create_dic.py
  1. Compile newword_dic.csv into a format (.dic) that can be used with MeCab and add it to the dictionary.
  sudo /usr/local/libexec/mecab/mecab-dict-index -d /usr/local/lib/mecab/dic/ipadic -u user.dic -f utf8 -t utf8 newword_dic.csv
  1. Move the added dictionary to the directory where the MeCab system dictionary exists.
  sudo mv user.dic /usr/local/lib/mecab/dic/ipadic
  1. Start mecab from the terminal   スクリーンショット 2016-10-07 14.13.03.png

If so, the user dictionary is correctly added to MeCab

References

http://tseiya.hatenablog.com/entry/2012/09/19/191114

Recommended Posts

Add a dictionary to MeCab
Add user dictionary to MeCab
[Morphological analysis] How to add a new dictionary to Mecab
MeCab: Add new words to user-defined dictionary (Windows)
To add a C module to MicroPython ...
Metaclass (wip) to generate a dictionary
Add a GPIO board to your computer. (1)
How to add a package with PyCharm
Various ways to create a dictionary (memories)
Script to create a Mac dictionary file
Add a Python virtual environment to VSCode
Add a command to mark similar files together
How to make a dictionary with a hierarchical structure.
How to add a Python module search path
Generate a MeCab dictionary from Nico Nico Pedia data
To add a module to python put in Julialang
[Wagtail] Add a login page to the Wagtail project
Add convolution to MNIST
How to use dictionary {}
Access to dictionary fields
How to convert a class object to a dictionary with SQLAlchemy
How to write a list / dictionary type of Python3
Generate a bash script to add Datadog monitor settings
A quick introduction to pytest-mock
Introduction to dictionary lookup algorithm
[Django] A pattern to add related records after creating a record
Create a dictionary in Python
Add / remove kernel to JupyterLab
Use MeCab to fetch readings
A road to intermediate Python
A super introduction to Linux
How to convert an array to a dictionary with Python [Application]
How to call a function
Add page number to PDF
Upload a file to Dropbox
Send a signal to subprocess
Add a function to heat transfer + heat input by temperature to heatrapy
Use MeCab to translate sloppy sentences in a "slow" way.
A handy function to add a column anywhere in a Pandas DataFrame
How to hack a terminal
The usual way to add a Kernel with Jupyter Notebook
Try to select a language
A relatively easy way to insert a NEologd dictionary on Windows-System Dictionary
Add System to pyenv-win versions
Expand a Python nested dictionary to do something like Pandas' MultiIndex
[Python] How to add rows and columns to a table (pandas DataFrame)
How to check the memory size of a dictionary in Python
Attempt to extend a function in the library (add copy function to pathlib)
How to write a Python class
Try to draw a Bezier curve
A memo to move Errbot locally
Add fields to features with ArcPy
Convert A4 PDF to A3 every 2 pages
Steps to create a Django project
Add cumulative ratio to matplotlib histogram
A memorandum to change to Manjaro Linux
Create a nested dictionary using defaultdict
How to make a slack bot
How to create a Conda package
Write standard output to a file
Add Python 2.7 Japanese documentation to Dash.app