goo辞書を使って英単語を検索するスクリプト

PowerShellからgoo辞書を使いたくなったので作ってみた。

Find-Word.ps1

param([string]$word)

if($args[0] -eq "-?" -or $word.Length -eq 0) {
    $commandName = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name)
	Write-Host @"

Name:
    $commandName

Description:
    Finds a word by using Goo dictionary.

Usage:
    $commandName [[-word] <string>]

    -word <string>
        The word to find.
        
Require:
    New-PSObject

"@ -foregroundColor Yellow
    exit 1
}

$webReq = [Net.HttpWebRequest]::Create(
    "http://dictionary.goo.ne.jp/search.php?MT=$word&kind=ej&from=Mozilla/Firefox"
)
$webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)"
$webRes = $webReq.GetResponse()

$sr = New-Object IO.StreamReader(
    $webRes.GetResponseStream(), [Text.Encoding]::GetEncoding("EUC-JP")
)
# 単語が表示されているタグのパターン
$wordPtrn = New-Object Text.RegularExpressions.Regex("(</i>|<b>(?<word>.*?)</b>)(?<mean>.*?)<br>")
# その中に含まれている余計なタグを削除するパターン
$escapePtrn = New-Object Text.RegularExpressions.Regex("(<.*?>|\&nbsp;)")

function Escape([string]$value) {
    if($value.Length -eq 0) { return $word }
    else {
        $escapePtrn.Replace($value, "").Trim()
    }
}

$buf = New-Object IO.StringReader($sr.ReadToEnd())
$sr.Close()
$webRes.Close()

# 翻訳された単語の一覧までとばす。
while($buf.ReadLine() -notmatch '<div class=\"mainlst\">') {}
while(($line = $buf.ReadLine()) -notmatch '</div>') {
    $wordPtrn.Matches($line) | % {
        New-PSObject @{
            Word=Escape($_.Groups["word"].Value);
            Meaning=Escape($_.Groups["mean"].Value)
        }
    }
}

やってることは単純にHttpリクエストを送りレスポンスを取得して、正規表現で単語を解析してオブジェクトで出力するだけ。

使い方

PS > Find-Word help

Word                                                   Meaning                                               
----                                                   -------                                               
help                                                   助ける; 助けて…させる, 手伝う; (食物を)よそう, 給仕する (〜 them to some b...
cannot help doing / cannot help but do                 …せざるを得ない.                                             
cannot help …('s) doing                                人が…するのはどうしようもない.                                      
help forward                                           助成する.                                                 
help off                                               手伝ってぬがせる[車からおろす] ((with)).                            
help on                                                手伝って着せる[車に乗せる] ((with)).                              
help out                                               手伝う; 救い出す; 手伝って出してやる.                                 
I can't help it./ It cannot be helped.                 どうにもしようがない.                                           
help oneself                                           人に頼らずにやっていく; ((否定文で)) 自分(の感情)を抑える; 自由に取って食べる[飲む] ((...
Help yourself.                                         どうぞご自由に.                                              
May I help you?                                        (店員が客に)何にいたしましょうか.                                    
So help me (God)!                                      神に誓って, ほんとうに.                                         
help                                                   助け, 救助; 手伝う人, 役立つもの[人] ((to)); お手伝いさん, 従業員; ((複数扱い)...
be beyond help                                         (患者などが)助かる見込みがない.                                     
be of help                                             役に立つ, 助けになる.                                          
help balloon                                           【コンピュータ】ヘルプメッセージの吹き出し.                                
help・er                                                ━━ n.                                                 
helper application                                     【コンピュータ】ヘルパー・アプリケーション.                                
helper -cell                                           【免疫】ヘルパーT細胞 ((抗体や他のT細胞の産出力を高めたり,免疫システムを活性化する)).       
help file                                              【コンピュータ】ヘルプメッセージの入ったファイル.                             
help・ful                                               ━━ a. 助けになる, 役に立つ ((to)).                             
help・ful・ly                                            ad.                                                   
help・ful・ness                                          n.                                                    
help・ing                                               ━━ n. 手助け; ひと盛り.                                      
helping hand                                           援助の手, 助力.                                             
help・less                                              ━━ a. 手も足も出ない, 無力な ((against)); 頼りない, 無力の ((to do))...
help・less・ly                                           ad.                                                   
help・less・ness                                         n.                                                    
help・line                                              〔英〕 ヘルプライン ((悩み事などの電話相談)); 情報提供電話サービス.                
help・mate                                              , help・meet 協力者; ((特に)) 妻.                            
help menu                                              【コンピュータ】ヘルプ[説明]・メニュー.                                 
help screen                                            【コンピュータ】ヘルプ[説明]画面.