Skip to content

Instantly share code, notes, and snippets.

@pierre3
Created December 7, 2014 02:07
Show Gist options
  • Save pierre3/05b5522794c0d10d6bed to your computer and use it in GitHub Desktop.
Save pierre3/05b5522794c0d10d6bed to your computer and use it in GitHub Desktop.
https://labs.goo.ne.jp/ の固有表現抽出APIを使用して、文字列から固有表現を取得する PowerShell function
<#
.Synopsis
文字列から固有表現の抽出
.DESCRIPTION
https://labs.goo.ne.jp/ の固有表現抽出APIを使用して、文字列から固有表現を取得します
https://labs.goo.ne.jp/apiusage/ で取得したアプリケーションIDを $AppId パラメータに指定する必要があります。
.EXAMPLE
Get-GooLabsEntity "今日、田中君と山本君に渋谷で待ち合わせをしています。" -AppId $app_id
.EXAMPLE
Get-Content ".\data.txt" | Get-GooLabsEntity -AppId $app_id
#>
function Get-GooLabsEntity
{
[CmdletBinding()]
Param(
# 検索ワード
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
Position=0)]
[string[]]
$InputStrings,
[Parameter(Position=1)]
[string]
$AppId = $goo_labs_app_id
)
Begin
{
$sentence = ""
function doRequest
{
Param([string]$sentence)
Write-Verbose ">>> Invoke-WebRequest <<<"
$response = Invoke-WebRequest -Method Post -Uri https://labs.goo.ne.jp/api/entity `
-Body @{ app_id = $AppId; sentence = $sentence; }
[Text.Encoding]::GetEncoding("utf-8").GetString($response.RawContentStream.GetBuffer()) |
ConvertFrom-Json | % {
foreach($item in $_.ne_list){
[PSCustomObject]@{ Type = $item[1]; Value=$item[0] }
}
};
};
}
Process
{
foreach($text in $InputStrings)
{
Write-Verbose "input>> $text"
$sentence = "$sentence,$text"
if($sentence.Length -le 300)
{
continue
}
doRequest $sentence
$sentence = ""
}
}
End
{
if($sentence -ne "")
{
doRequest $sentence
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment