Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mecab error #22

Merged
merged 2 commits into from
Oct 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions mrep/morph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

class MeCabParser(object):
def __init__(self, arg=''):
model = MeCab.Model_create(arg)
if model is None:
raise Exception('Cannot initialize mecab')
self.model = model
# it may throw RuntimeError
self.model = MeCab.Model(arg)

def parse(self, s):
tagger = self.model.createTagger()
Expand Down
7 changes: 6 additions & 1 deletion scripts/mrep
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ if __name__ == '__main__':
else:
printer = mrep.printer.Printer(color_begin, color_end)

parser = mrep.morph.MeCabParser(args.mecab_arg)
try:
parser = mrep.morph.MeCabParser(args.mecab_arg)
except Exception as e:
sys.stderr.write('Fail to initialize MeCab\n')
sys.stderr.write('%s\n' % e)
sys.exit(4)

if len(args.file) > 0:
for file in args.file:
Expand Down
6 changes: 6 additions & 0 deletions test/mrep_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ def test_no_pattern(self):
def test_version(self):
out = self.call('--version')
self.assertTrue(__version__ in str(out))

def test_invalid_mecab_arg(self):
with self.assertRaises(subprocess.CalledProcessError) as cm:
self.call('--mecab-arg="--unknown-argument"', '.')

self.assertEqual(4, cm.exception.returncode)