Skip to content

Instantly share code, notes, and snippets.

@kyu999
Last active August 29, 2015 14:04
Show Gist options
  • Save kyu999/59063fa4553cf44bac5f to your computer and use it in GitHub Desktop.
Save kyu999/59063fa4553cf44bac5f to your computer and use it in GitHub Desktop.
nodeから品詞を取る
def getFeature(node):
unicodedFeature = unicode(node.feature.split(",")[0], "utf-8")
return unicodedFeature
#node = tagger.parseToNode(sentence)
def wakati(self, sentence, tagger=None):
"""
文を単語に分解して、単語間に", "を挿入して返す
@param sentence: 分解対象の文(utf-8を想定)
@param tagger: MeCab.Taggerを指定できる. 何も指定しない場合関数内で作成する
@return 例外が発生しなければ単語で分解した文字列を返す
"""
try:
tagger = MeCab.Tagger('-O wakati')
morphemes = []
integration_times = 0
# wakatiで半角スペースが消えるので、位置を覚えておいてあとで、半角スペースを挿入する
half_with_space_indexes = [i for i, w in enumerate(sentence)
if w == ' ']
print("spaces ->", half_with_space_indexes)
node = tagger.parseToNode(sentence).next
while node:
print(node.surface)
if not node.surface:
node = node.next
continue
if self.getFeature(node) == u"助詞":
if len(morphemes) == 0:
last = ""
else:
last = morphemes.pop()
morphemes.append(last + node.surface)
integration_times += 1
else:
morphemes.append(node.surface)
current_position = len(''.join(morphemes))
print("current_position ->",current_position)
if (current_position + integration_times) in half_with_space_indexes:
print("add space")
morphemes.append(' ')
print(morphemes)
node = node.next
return unicode(self.separator.join(morphemes), 'utf-8')
except RuntimeError as e:
logging.warning((sentence, e))
return None
#MeCabに渡す文字列はencode,戻ってきた文字列はdecodeする
#MeCabに渡した文字列は必ず変数に入れておく
#基本はunicodeだぜ.utf-8が入るとややこしいから気をつける
#mecab encode&decode関連tips => http://shogo82148.github.io/blog/2012/12/15/mecab-python/
sentence = "「あいつは1424年に死ぬだろう。」ただしそれはあいつの責任じゃないことは誰もが知っていた。そう、1mmの虫でさえ。。。"
reload(title_splitter)
splitter = title_splitter.TitleSplitter()
wakatied = splitter.wakati(sentence)
print("wakatied")
print(wakatied)
regularized = splitter.regularize(wakatied)
print("regularized")
print(regularized)
breaks = splitter.get_break_positions(sentence)
print("breaks")
print(breaks)
こんなかんじになる。gunosy pcでは数字と年がくっつくからそこはおっけ。
>>> print(regularized)
「あいつは___1424___年に___死ぬ___だろ___う。」___ただし___それは___あいつの___責任じゃ___ない___ことは___誰もが___知って___い___た。___そう、___1___mmの___虫でさえ。。。
ーーーーーーーーーーーーーーーーーーーーーーーーーー
明日のTODO:
1. 数字+単位+単位のケースもくっつける (例) 一億円
2. 品詞を基準とした改行ルールも考える。現時点では助詞とその前の単語はくっつけてる
3. mecabによって消えた空白を戻すとこがうまくいってないのでそこを行う
4. 必要な単位を暇ならまた考える
@kyu999
Copy link
Author

kyu999 commented Aug 7, 2014

            if self.extractFeature(node) == u"助詞":
                if len(morphemes) == 0:
                    last = ""
                else:
                    last = morphemes.pop()

                morphemes.append(last + node.surface)    
                integration_times += 1                    

@kyu999
Copy link
Author

kyu999 commented Aug 7, 2014

def extractFeature(self, node):
    """
    単語の品詞を抽出
    """
    unicodedFeature = unicode(node.feature.split(",")[0], "utf-8")
    return unicodedFeature          

@kyu999
Copy link
Author

kyu999 commented Aug 7, 2014

  1. char ひらがな かつ next 漢字
  2. char 記号 後 非記号
  3. ひらがなの後 英数字はきっていい
  4. 数字とひらがなの間きる

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment