Skip to content

Instantly share code, notes, and snippets.

@kei-s
Last active February 28, 2018 06:22
Show Gist options
  • Save kei-s/fe89d094d6f8c247598c2100ce1ced3c to your computer and use it in GitHub Desktop.
Save kei-s/fe89d094d6f8c247598c2100ce1ced3c to your computer and use it in GitHub Desktop.
mecab-python3 デバッグメモ
set breakpoint pending on
b _wrap_Tagger_parseToNode
#!/bin/sh
cd /src/github.com/taku910/mecab/
cd mecab/swig
make python
cd ../python
rm -rf build/
CFLAGS='-g3 -O0' python2.7 setup.py install
cp MeCab.py ../../../../SamuraiT/mecab-python3/
cp MeCab_wrap.cxx ../../../../SamuraiT/mecab-python3/
cd ../../../../SamuraiT/mecab-python3/
rm -rf build/
CFLAGS='-g3 -O0' python setup.py install
FROM python:3
RUN apt-get update
RUN apt-get install -y libmecab-dev mecab mecab-ipadic-utf8
RUN apt-get install -y gdb python2.7-dev
RUN mkdir /tools
RUN curl -sSfL http://prdownloads.sourceforge.net/swig/swig-3.0.12.tar.gz | tar -zxv -C /tools/ && \
cd /tools/swig-3.0.12 && \
./configure && \
make && \
make install
RUN wget -P ~ git.io/.gdbinit && \
mkdir ~/.gdbinit.d && \
echo 'set auto-load safe-path /' > ~/.gdbinit.d/init

docker イメージビルド

$ docker build -t mecab-python3 .

docker コンテナ起動

$ docker run -it --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -v ~/src:/src  mecab-python3 /bin/bash

mecab-python3 ビルド

$ cd /src/github.com/taku910/mecab/
$ cd mecab/swig
$ make python
$ cd ../python
$ rm -rf build/
$ CFLAGS='-g3 -O0' python2.7 setup.py install
$ cp MeCab.py ../../SamuraiT/mecab-python3/
$ cp MeCab_wrap.cxx ../../SamuraiT/mecab-python3/
$ cd ../../../../SamuraiT/mecab-python3/
$ rm -rf build/
$ CFLAGS='-g3 -O0' python setup.py install

gdb

$ gdb python
run sample.py
disp alloc2
disp buf2
disp result.surface
ni

おかしくなる場合、buf2 と result.surface が同じアドレスを指し、buf2 が delete されるためおかしくなる。

import MeCab
mecab = MeCab.Tagger()
# print(mecab.parse(""))
node = mecab.parseToNode("sampleこれはペン")
while node:
print(node.surface, "\t", node.feature)
node = node.next
# -*- coding: utf-8 -*-
import MeCab
mecab = MeCab.Tagger()
# print mecab.parse("sampleこれはペン")
node = mecab.parseToNode("sampleこれはペン")
while node:
print node.surface, "\t", node.feature
node = node.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment