Created
August 19, 2014 17:03
-
-
Save furandon-pig/19ed2a33a80067781acb to your computer and use it in GitHub Desktop.
Googleサジェスト結果からChasenの辞書エントリを生成するスクリプトです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
######################################################################## | |
# # | |
# 短時間に大量のクエリを投げ続けるとGoogleからアクセス制限される場合が # | |
# あるので、スクリプトの連続実行は適当なインターバル(数秒程度)を入れて # | |
# ください。 # | |
# # | |
######################################################################## | |
# このスクリプトについて: | |
# 「第2回Webスクレイピング勉強会@東京」で発表した、Googleサジェスト | |
# 結果からChasenの辞書エントリを生成するスクリプトです。 | |
# | |
# * 第2回Webスクレイピング勉強会@東京 | |
# http://tokyoscrapper.connpass.com/event/7107/ | |
# | |
# 実行例: | |
# $ ./suggest2chadic.sh きるみ | |
# (品詞 (名詞 一般)) ((見出し語 (キルミーダンス 3641)) (読み キルミーダンス) (発音 キルミーダンス)) | |
# (品詞 (名詞 一般)) ((見出し語 (キルミーベイベー 3641)) (読み キルミーベイベー) (発音 キルミーベイベー)) | |
# | |
# 注意事項: | |
# * NetBSD-6.1-i386のLANG=ja_JP.eucJPな環境で動作確認しています。 | |
# * LANG=ja_JP.UTF-8な環境では一部スクリプトの修正が必要です。 | |
# * 別途nkf,wget,chasenが必要です。 | |
# 0: デバッグ無効 0以外: デバッグ有効 | |
DEBUG=0 | |
UA="'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.5; rv:8.0.1) Gecko/20100101 Firefox/8.0.1'" | |
GOOGLE='http://www.google.com/' | |
if [ -z "$1" ]; then | |
echo "ERROR: Googleサジェストへの単語を指定してください。" | |
echo " $ ./suggest2chadic.sh <単語>" | |
exit 1 | |
fi | |
keyword=$1 | |
tmp_dir=tmp.$$ | |
[ ! -d ${tmp_dir} ] && mkdir ${tmp_dir} | |
# 指定されたキーワードに対するGoogleサジェスト結果をXMLで取得する | |
QUERY_CMD=wget | |
${QUERY_CMD} \ | |
--quiet --user-agent=${UA} \ | |
-O ${tmp_dir}/suggest.xml \ | |
${GOOGLE}/complete/search\?q=${keyword}\&hl=ja\&output=toolbar | |
for i in `cat ${tmp_dir}/suggest.xml | sed -e "s/></ /g"` | |
do | |
echo $i | |
done \ | |
| grep 'data=' | cut -d= -f2 \ | |
| sed -e "s/\"//g" -e "s/\///g" \ | |
| iconv -f utf8 -t euc-jp \ | |
| sort | uniq > ${tmp_dir}/suggest_word.txt | |
# サジェスト結果の単語をChasenにかけ、未知語と判定された単語を収集する | |
# (ここで未知語として扱うのは、サジェスト結果の単語で完全一致したもののみ) | |
for i in `cat ${tmp_dir}/suggest_word.txt | chasen | grep 未知語 | awk '{ print $1 }'` | |
do | |
grep -w ^${i}$ ${tmp_dir}/suggest_word.txt | |
done > ${tmp_dir}/out_of_vocabulary.txt | |
# 収集した未知語をChasenのユーザ辞書への追加エントリとして出力する | |
# (現状は「名詞」固定で形態素生起コストも3641の固定値) | |
for i in `cat ${tmp_dir}/out_of_vocabulary.txt` | |
do | |
_KANA=`echo $i | nkf --katakana` | |
cat <<_EOF | |
(品詞 (名詞 一般)) ((見出し語 ($i 3641)) (読み ${_KANA}) (発音 ${_KANA})) | |
_EOF | |
done | |
[ ${DEBUG} -eq 0 ] && rm -rf ${tmp_dir} | |
# Copyright (c) 2014, @furandon_pig | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are | |
# met: | |
# | |
# 1. Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# 2. Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the | |
# distribution. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment