LoginSignup
3

More than 5 years have passed since last update.

文章から人名のみ取り出す

Posted at

文章から人名のみ取り出す方法です.

MeCabを使います.辞書はneologdを使用します.検索してインストールしてください.

human-name.py
# -*- coding:utf-8 -*-

import MeCab
# neologdを使用するための設定
m = MeCab.Tagger(' -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd')
human_name = []

def get_name(document):
    node = m.parseToNode(document).next
    while node:
        nodeFeature = node.feature.split(",")
        # 人名のみ取得(名詞,固有名詞,人名)
        if nodeFeature[0] == "名詞" and nodeFeature[1] == "固有名詞" and nodeFeature[2] == "人名":
            human_name.append(node.surface)
        node = node.next

    return human_name

if __name__=="__main__":
    get_name("俳優の田中哲司(51)が6月2日発売の『フライデー』(講談社)で女性との密会を報じられた問題で、田中が8日、所属事務所を通じコメントを発表。「私の軽率な行動でお騒がせしまして、関係者の皆様には大変ご迷惑をおかけしましたこと、誠に申し訳ございません」と報道について謝罪した。【写真】妻・仲間由紀恵、真っ赤な肩出しドレスを披露同誌では、田中が有名ヘアスタイリストの女性と数年前から深い仲にあるとして、田中が女性のマンションに向かう姿を写真とともに報道。文書では、報道について謝罪し、「家族には大変つらい思いをさせてしまい、今後このような思いをさせないと心から謝罪しました。皆様の信頼を回復するべく芝居に精進して参りますと共に心よりお詫び申し上げます」とコメントした。田中は2014年9月に女優の仲間由紀恵(37)と結婚している。")
    print(human_name)
出力結果
['田中哲司', '田中', '仲間由紀恵', '田中', '田中', '田中', '仲間由紀恵']

リストで返ってくるので適当に処理して使ってください.

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3