refactored from cabocha-wrapper, but is mostly rewritten to be more clear and flexible