DoboWiki
Top
> .NETプログラミング研究/100 をテンプレートにして作成
.NETプログラミング研究/100 をテンプレートにして作成
開始行:
#title(Yahoo! Japanの日本語形態素解析Web APIを使う)
#navi(.NETプログラミング研究)
#contents
*Yahoo! Japanの日本語形態素解析Web APIを使う [#ld3f8adf]
[[Yahoo! Japan>http://www.yahoo.co.jp/]]の[[日本語形態素...
日本語形態素解析Web APIは、RESTのみに対応しています。GET...
日本語形態素解析Web APIを利用するには、様々な制限がありま...
また、クレジットの表示が義務付けられています。詳しくは、...
さらに、1つのアプリケーションIDにつき24時間以内で50000件...
これらの制限については、ご使用前にかならずご自分でご確認...
**とりあえず使ってみる [#q9f63a1c]
日本語形態素解析Web APIで取得できる結果には、2種類ありま...
まずはとりあえず使ってみましょう。以下の例では、resultsパ...
#code(vbnet){{
'Imports System.IO
'Imports System.Net
'Imports System.Text
'Imports System.Xml
'形態素解析する文章
Dim text As String = "今日はいい天気です。明日もいい天気...
'HttpWebRequestを作成する
Dim webreq As HttpWebRequest = DirectCast( _
System.Net.WebRequest.Create( _
"http://jlp.yahooapis.jp/MAService/V1/parse"), Ht...
'POSTとしてデータを送信する
webreq.Method = "POST"
webreq.ContentType = "application/x-www-form-urlencoded"
'リクエストパラメータを作成する
Dim postStr As String = "appid=<あなたのアプリケーションI...
"results=ma&" & _
"sentence=" & Uri.EscapeUriString(text)
Dim postData As Byte() = Encoding.UTF8.GetBytes(postStr)
webreq.ContentLength = postData.Length
'データを送信する
Dim reqStrm As Stream = webreq.GetRequestStream()
reqStrm.Write(postData, 0, postData.Length)
reqStrm.Close()
'サーバーからの応答を受信するためのHttpWebResponseを取得...
Dim webres As HttpWebResponse = DirectCast(webreq.GetResp...
'応答データを受信するためのStreamを取得する
Dim st As Stream = webres.GetResponseStream()
Dim sr As New StreamReader(st)
'応答データをすべて読み込む
Dim resultXml As String = sr.ReadToEnd()
'閉じる
sr.Close()
st.Close()
webres.Close()
'結果を表示する
Console.WriteLine(resultXml)
}}
#code(csharp){{
//using System;
//using System.IO;
//using System.Net;
//using System.Text;
//using System.Xml;
//形態素解析する文章
string text = "今日はいい天気です。明日もいい天気になるで...
//HttpWebRequestを作成する
HttpWebRequest webreq = (HttpWebRequest)System.Net.WebReq...
"http://jlp.yahooapis.jp/MAService/V1/parse");
//POSTとしてデータを送信する
webreq.Method = "POST";
webreq.ContentType = "application/x-www-form-urlencoded";
//リクエストパラメータを作成する
string postStr = "appid=<あなたのアプリケーションID>&" +
"results=ma&" +
"sentence=" + Uri.EscapeUriString(text);
byte[] postData = Encoding.UTF8.GetBytes(postStr);
webreq.ContentLength = postData.Length;
//データを送信する
Stream reqStrm = webreq.GetRequestStream();
reqStrm.Write(postData, 0, postData.Length);
reqStrm.Close();
//サーバーからの応答を受信するためのHttpWebResponseを取得...
HttpWebResponse webres = (HttpWebResponse)webreq.GetRespo...
//応答データを受信するためのStreamを取得する
Stream st = webres.GetResponseStream();
StreamReader sr = new StreamReader(st);
//応答データをすべて読み込む
string resultXml = sr.ReadToEnd();
//閉じる
sr.Close();
st.Close();
webres.Close();
//結果を表示する
Console.WriteLine(resultXml);
}}
このコードを実行した結果は、以下のようになります。(実際...
#pre{{
<?xml version="1.0" encoding="UTF-8" ?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...
xmlns="urn:yahoo:jp:jlp"
xsi:schemaLocation="urn:yahoo:jp:jlp http://jlp.yahooapi...
<ma_result>
<total_count>15</total_count>
<filtered_count>15</filtered_count>
<word_list>
<word>
<surface>今日</surface>
<reading>きょう</reading>
<pos>名詞</pos>
</word>
<word>
<surface>は</surface>
<reading>は</reading>
<pos>助詞</pos>
</word>
<word>
<surface>いい</surface>
<reading>いい</reading>
<pos>形容詞</pos>
</word>
<word>
<surface>天気</surface>
<reading>てんき</reading>
<pos>名詞</pos>
</word>
<word>
<surface>です</surface>
<reading>です</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>。</surface>
<reading>。</reading>
<pos>特殊</pos>
</word>
<word>
<surface>明日</surface>
<reading>あした</reading>
<pos>名詞</pos>
</word>
<word>
<surface>も</surface>
<reading>も</reading>
<pos>助詞</pos>
</word>
<word>
<surface>いい</surface>
<reading>いい</reading>
<pos>形容詞</pos>
</word>
<word>
<surface>天気</surface>
<reading>てんき</reading>
<pos>名詞</pos>
</word>
<word>
<surface>に</surface>
<reading>に</reading>
<pos>助詞</pos>
</word>
<word>
<surface>なる</surface>
<reading>なる</reading>
<pos>動詞</pos>
</word>
<word>
<surface>でしょ</surface>
<reading>でしょ</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>う</surface>
<reading>う</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>。</surface>
<reading>。</reading>
<pos>特殊</pos>
</word>
</word_list>
</ma_result>
</ResultSet>
}}
resultsパラメータを"uniq"にして、出現頻度情報の結果のみを...
#pre{{
<?xml version="1.0" encoding="UTF-8" ?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...
xmlns="urn:yahoo:jp:jlp"
xsi:schemaLocation="urn:yahoo:jp:jlp http://jlp.yahooapi...
<uniq_result>
<total_count>15</total_count>
<filtered_count>15</filtered_count>
<word_list>
<word>
<count>2</count>
<surface>。</surface>
<reading/>
<pos>特殊</pos>
</word>
<word>
<count>2</count>
<surface>いい</surface>
<reading/>
<pos>形容詞</pos>
</word>
<word>
<count>2</count>
<surface>天気</surface>
<reading/>
<pos>名詞</pos>
</word>
<word>
<count>1</count>
<surface>う</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>でしょ</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>です</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>なる</surface>
<reading/>
<pos>動詞</pos>
</word>
<word>
<count>1</count>
<surface>に</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>は</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>も</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>今日</surface>
<reading/>
<pos>名詞</pos>
</word>
<word>
<count>1</count>
<surface>明日</surface>
<reading/>
<pos>名詞</pos>
</word>
</word_list>
</uniq_result>
</ResultSet>
}}
さらに、resultsパラメータを"ma,uniq"にして、通常の形態素...
#pre{{
<?xml version="1.0" encoding="UTF-8" ?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...
xmlns="urn:yahoo:jp:jlp"
xsi:schemaLocation="urn:yahoo:jp:jlp http://jlp.yahooapi...
<ma_result>
<total_count>15</total_count>
<filtered_count>15</filtered_count>
<word_list>
<word>
<surface>今日</surface>
<reading>きょう</reading>
<pos>名詞</pos>
</word>
<word>
<surface>は</surface>
<reading>は</reading>
<pos>助詞</pos>
</word>
<word>
<surface>いい</surface>
<reading>いい</reading>
<pos>形容詞</pos>
</word>
<word>
<surface>天気</surface>
<reading>てんき</reading>
<pos>名詞</pos>
</word>
<word>
<surface>です</surface>
<reading>です</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>。</surface>
<reading>。</reading>
<pos>特殊</pos>
</word>
<word>
<surface>明日</surface>
<reading>あした</reading>
<pos>名詞</pos>
</word>
<word>
<surface>も</surface>
<reading>も</reading>
<pos>助詞</pos>
</word>
<word>
<surface>いい</surface>
<reading>いい</reading>
<pos>形容詞</pos>
</word>
<word>
<surface>天気</surface>
<reading>てんき</reading>
<pos>名詞</pos>
</word>
<word>
<surface>に</surface>
<reading>に</reading>
<pos>助詞</pos>
</word>
<word>
<surface>なる</surface>
<reading>なる</reading>
<pos>動詞</pos>
</word>
<word>
<surface>でしょ</surface>
<reading>でしょ</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>う</surface>
<reading>う</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>。</surface>
<reading>。</reading>
<pos>特殊</pos>
</word>
</word_list>
</ma_result>
<uniq_result>
<total_count>15</total_count>
<filtered_count>15</filtered_count>
<word_list>
<word>
<count>2</count>
<surface>。</surface>
<reading/>
<pos>特殊</pos>
</word>
<word>
<count>2</count>
<surface>いい</surface>
<reading/>
<pos>形容詞</pos>
</word>
<word>
<count>2</count>
<surface>天気</surface>
<reading/>
<pos>名詞</pos>
</word>
<word>
<count>1</count>
<surface>う</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>でしょ</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>です</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>なる</surface>
<reading/>
<pos>動詞</pos>
</word>
<word>
<count>1</count>
<surface>に</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>は</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>も</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>今日</surface>
<reading/>
<pos>名詞</pos>
</word>
<word>
<count>1</count>
<surface>明日</surface>
<reading/>
<pos>名詞</pos>
</word>
</word_list>
</uniq_result>
</ResultSet>
}}
**日本語形態素解析Web APIが返す結果の説明 [#z7618d18]
results=ma の時は、形態素解析の結果がma_resultという要素...
word_list要素は、形態素のリストを表します。word_list要素...
results=ma の時はword_listの形態素の順番が元の文章のまま...
上記の例ではword要素内の要素は3つだけですが、リクエストパ...
word要素内に入る可能性がある要素と、その意味を以下に表で...
|要素|説明|h
|surface|形態素の表記。表層形。|
|reading|形態素の読みがな。読み。ひらがなで返される。uniq...
|pos|形態素の品詞。形容詞、形容動詞、感動詞、副詞、連体詞...
|baseform|形態素の基本形表記。原形。|
|feature|形態素の全情報。このフォーマットに関する説明が公...
|count|同一形態素の出現数。uniq_resultの場合のみ。|
**リクエスト時に指定できるパラメータ [#j0745cb3]
以下に、リクエスト時に指定できるパラメータを表にして説明...
|パラメータ|説明|h
|appid|必須パラメータ。登録済のアプリケーションIDを指定す...
|sentence|必須パラメータ。解析対象のテキスト。|
|results|必須パラメータ。解析結果の種類を指定する。"ma"、...
|response|取得したい形態素の情報を指定する。省略すると、"...
|filter|結果を取得したい形態素の品詞を数字で指定する。複...
|ma_response|"results=ma,uniq"とした時に、responseパラメ...
|ma_filter|"results=ma,uniq"とした時に、filterパラメータ...
|uniq_response|"results=ma,uniq"とした時に、responseパラ...
|uniq_filter|"results=ma,uniq"とした時に、filterパラメー...
|uniq_by_baseform|"results=uniq"の時、通常は全く同一の形...
**結果を解析して表示する [#w9176b77]
結果を解析して表示する例を示します。XMLの解析にはXmlDocum...
また、以前の例では省略していたresponseパラメータを指定し...
#code(vbnet){{
'Imports System.IO
'Imports System.Net
'Imports System.Text
'Imports System.Xml
'形態素解析する文章
Dim text As String = "今日はいい天気です。明日もいい天気...
'HttpWebRequestを作成する
Dim webreq As HttpWebRequest = DirectCast( _
System.Net.WebRequest.Create( _
"http://jlp.yahooapis.jp/MAService/V1/parse"), Ht...
'POSTとしてデータを送信する
webreq.Method = "POST"
webreq.ContentType = "application/x-www-form-urlencoded"
'リクエストパラメータを作成する
Dim postStr As String = "appid=<あなたのアプリケーションI...
"results=ma&" & _
"response=surface,reading,pos,baseform,feature&" & _
"sentence=" & Uri.EscapeUriString(text)
Dim postData As Byte() = Encoding.UTF8.GetBytes(postStr)
webreq.ContentLength = postData.Length
'データを送信する
Dim reqStrm As Stream = webreq.GetRequestStream()
reqStrm.Write(postData, 0, postData.Length)
reqStrm.Close()
'サーバーからの応答を受信するためのHttpWebResponseを取得...
Dim webres As HttpWebResponse = DirectCast(webreq.GetResp...
'応答データを受信するためのStreamを取得する
Dim st As Stream = webres.GetResponseStream()
Dim sr As New StreamReader(st)
'応答データをすべて読み込む
Dim resultXml As String = sr.ReadToEnd()
'閉じる
sr.Close()
st.Close()
webres.Close()
'XmlDocumentに読み込む
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(resultXml)
'ルートを取得
Dim rootElm As XmlElement = xmlDoc.DocumentElement
'情報の表示
If rootElm.Name = "ResultSet" Then
'形態素解析の結果が返されているときは、解析する
Dim maElm As XmlElement = rootElm("ma_result")
If maElm IsNot Nothing Then
Console.WriteLine("===== ma_result =====")
'形態素の総数
Console.WriteLine("total_count:{0}", maElm("total...
'フィルタにマッチした形態素数
Console.WriteLine("filtered_count:{0}", maElm("fi...
Console.WriteLine()
'形態素のリストを列挙する
Dim wlElm As XmlElement = maElm("word_list")
If wlElm IsNot Nothing Then
For Each e As XmlElement In wlElm.ChildNodes
'表記
If e("surface") IsNot Nothing Then
Console.WriteLine("surface:{0}", e("s...
End If
'読みがな
If e("reading") IsNot Nothing Then
Console.WriteLine("reading:{0}", e("r...
End If
'品詞
If e("pos") IsNot Nothing Then
Console.WriteLine("pos:{0}", e("pos")...
End If
'基本形表記
If e("baseform") IsNot Nothing Then
Console.WriteLine("baseform:{0}", e("...
End If
'全情報
If e("feature") IsNot Nothing Then
Console.WriteLine("feature:{0}", e("f...
End If
Console.WriteLine()
Next
End If
End If
End If
}}
#code(csharp){{
//using System;
//using System.IO;
//using System.Net;
//using System.Text;
//using System.Xml;
//形態素解析する文章
string text = "今日はいい天気です。明日もいい天気になるで...
//HttpWebRequestを作成する
HttpWebRequest webreq = (HttpWebRequest)System.Net.WebReq...
"http://jlp.yahooapis.jp/MAService/V1/parse");
//POSTとしてデータを送信する
webreq.Method = "POST";
webreq.ContentType = "application/x-www-form-urlencoded";
//リクエストパラメータを作成する
string postStr = "appid=<あなたのアプリケーションID>&" +
"results=ma&" +
"response=surface,reading,pos,baseform,feature&" +
"sentence=" + Uri.EscapeUriString(text);
byte[] postData = Encoding.UTF8.GetBytes(postStr);
webreq.ContentLength = postData.Length;
//データを送信する
Stream reqStrm = webreq.GetRequestStream();
reqStrm.Write(postData, 0, postData.Length);
reqStrm.Close();
//サーバーからの応答を受信するためのHttpWebResponseを取得...
HttpWebResponse webres = (HttpWebResponse)webreq.GetRespo...
//応答データを受信するためのStreamを取得する
Stream st = webres.GetResponseStream();
StreamReader sr = new StreamReader(st);
//応答データをすべて読み込む
string resultXml = sr.ReadToEnd();
//閉じる
sr.Close();
st.Close();
webres.Close();
//XmlDocumentに読み込む
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(resultXml);
//ルートを取得
XmlElement rootElm = xmlDoc.DocumentElement;
//情報の表示
if (rootElm.Name == "ResultSet")
{
//形態素解析の結果が返されているときは、解析する
XmlElement maElm = rootElm["ma_result"];
if (maElm != null)
{
Console.WriteLine("===== ma_result =====");
//形態素の総数
Console.WriteLine("total_count:{0}", maElm["total...
//フィルタにマッチした形態素数
Console.WriteLine("filtered_count:{0}", maElm["fi...
Console.WriteLine();
//形態素のリストを列挙する
XmlElement wlElm = maElm["word_list"];
if (wlElm != null)
{
foreach (XmlElement e in wlElm.ChildNodes)
{
//表記
if (e["surface"] != null)
{
Console.WriteLine("surface:{0}", e["s...
}
//読みがな
if (e["reading"] != null)
{
Console.WriteLine("reading:{0}", e["r...
}
//品詞
if (e["pos"] != null)
{
Console.WriteLine("pos:{0}", e["pos"]...
}
//基本形表記
if (e["baseform"] != null)
{
Console.WriteLine("baseform:{0}", e["...
}
//全情報
if (e["feature"] != null)
{
Console.WriteLine("feature:{0}", e["f...
}
Console.WriteLine();
}
}
}
}
}}
結果は、以下のようになります。
#pre{{
===== ma_result =====
total_count:15
filtered_count:15
surface:今日
reading:きょう
pos:名詞
baseform:今日
feature:名詞,名詞,*,今日,きょう,今日
surface:は
reading:は
pos:助詞
baseform:は
feature:助詞,係助詞,*,は,は,は
surface:いい
reading:いい
pos:形容詞
baseform:いい
feature:形容詞,形容,基本形,いい,いい,いい
surface:天気
reading:てんき
pos:名詞
baseform:天気
feature:名詞,名詞,*,天気,てんき,天気
surface:です
reading:です
pos:助動詞
baseform:です
feature:助動詞,助動詞です,基本形,です,です,です
surface:。
reading:。
pos:特殊
baseform:。
feature:特殊,句点,*,。,。,。
surface:明日
reading:あした
pos:名詞
baseform:明日
feature:名詞,名詞,*,明日,あした,明日
surface:も
reading:も
pos:助詞
baseform:も
feature:助詞,係助詞,*,も,も,も
surface:いい
reading:いい
pos:形容詞
baseform:いい
feature:形容詞,形容,基本形,いい,いい,いい
surface:天気
reading:てんき
pos:名詞
baseform:天気
feature:名詞,名詞,*,天気,てんき,天気
surface:に
reading:に
pos:助詞
baseform:に
feature:助詞,格助詞,*,に,に,に
surface:なる
reading:なる
pos:動詞
baseform:なる
feature:動詞,ラ五,基本形,なる,なる,なる
surface:でしょ
reading:でしょ
pos:助動詞
baseform:です
feature:助動詞,助動詞です,未然形,でしょ,でしょ,です
surface:う
reading:う
pos:助動詞
baseform:う
feature:助動詞,助動詞,*,う,う,う
surface:。
reading:。
pos:特殊
baseform:。
feature:特殊,句点,*,。,。,。
}}
"results=uniq"にして出現頻度情報の結果を取得するコードは...
#pre{{
===== uniq_result =====
total_count:15
filtered_count:15
count:2
surface:。
reading:
pos:特殊
baseform:。
feature:特殊,*,*,。,,。
count:2
surface:いい
reading:
pos:形容詞
baseform:いい
feature:形容詞,*,*,いい,,いい
count:2
surface:天気
reading:
pos:名詞
baseform:天気
feature:名詞,*,*,天気,,天気
count:1
surface:う
reading:
pos:助動詞
baseform:う
feature:助動詞,*,*,う,,う
count:1
surface:でしょ
reading:
pos:助動詞
baseform:です
feature:助動詞,*,*,でしょ,,です
count:1
surface:です
reading:
pos:助動詞
baseform:です
feature:助動詞,*,*,です,,です
count:1
surface:なる
reading:
pos:動詞
baseform:なる
feature:動詞,*,*,なる,,なる
count:1
surface:に
reading:
pos:助詞
baseform:に
feature:助詞,*,*,に,,に
count:1
surface:は
reading:
pos:助詞
baseform:は
feature:助詞,*,*,は,,は
count:1
surface:も
reading:
pos:助詞
baseform:も
feature:助詞,*,*,も,,も
count:1
surface:今日
reading:
pos:名詞
baseform:今日
feature:名詞,*,*,今日,,今日
count:1
surface:明日
reading:
pos:名詞
baseform:明日
feature:名詞,*,*,明日,,明日
}}
**最後に [#cefc2be6]
今年の.NETプログラミング研究はこれが最後です。今年一年お...
**コメント [#ued8a753]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]] [[:Category/ASP.NET]],2010-1...
終了行:
#title(Yahoo! Japanの日本語形態素解析Web APIを使う)
#navi(.NETプログラミング研究)
#contents
*Yahoo! Japanの日本語形態素解析Web APIを使う [#ld3f8adf]
[[Yahoo! Japan>http://www.yahoo.co.jp/]]の[[日本語形態素...
日本語形態素解析Web APIは、RESTのみに対応しています。GET...
日本語形態素解析Web APIを利用するには、様々な制限がありま...
また、クレジットの表示が義務付けられています。詳しくは、...
さらに、1つのアプリケーションIDにつき24時間以内で50000件...
これらの制限については、ご使用前にかならずご自分でご確認...
**とりあえず使ってみる [#q9f63a1c]
日本語形態素解析Web APIで取得できる結果には、2種類ありま...
まずはとりあえず使ってみましょう。以下の例では、resultsパ...
#code(vbnet){{
'Imports System.IO
'Imports System.Net
'Imports System.Text
'Imports System.Xml
'形態素解析する文章
Dim text As String = "今日はいい天気です。明日もいい天気...
'HttpWebRequestを作成する
Dim webreq As HttpWebRequest = DirectCast( _
System.Net.WebRequest.Create( _
"http://jlp.yahooapis.jp/MAService/V1/parse"), Ht...
'POSTとしてデータを送信する
webreq.Method = "POST"
webreq.ContentType = "application/x-www-form-urlencoded"
'リクエストパラメータを作成する
Dim postStr As String = "appid=<あなたのアプリケーションI...
"results=ma&" & _
"sentence=" & Uri.EscapeUriString(text)
Dim postData As Byte() = Encoding.UTF8.GetBytes(postStr)
webreq.ContentLength = postData.Length
'データを送信する
Dim reqStrm As Stream = webreq.GetRequestStream()
reqStrm.Write(postData, 0, postData.Length)
reqStrm.Close()
'サーバーからの応答を受信するためのHttpWebResponseを取得...
Dim webres As HttpWebResponse = DirectCast(webreq.GetResp...
'応答データを受信するためのStreamを取得する
Dim st As Stream = webres.GetResponseStream()
Dim sr As New StreamReader(st)
'応答データをすべて読み込む
Dim resultXml As String = sr.ReadToEnd()
'閉じる
sr.Close()
st.Close()
webres.Close()
'結果を表示する
Console.WriteLine(resultXml)
}}
#code(csharp){{
//using System;
//using System.IO;
//using System.Net;
//using System.Text;
//using System.Xml;
//形態素解析する文章
string text = "今日はいい天気です。明日もいい天気になるで...
//HttpWebRequestを作成する
HttpWebRequest webreq = (HttpWebRequest)System.Net.WebReq...
"http://jlp.yahooapis.jp/MAService/V1/parse");
//POSTとしてデータを送信する
webreq.Method = "POST";
webreq.ContentType = "application/x-www-form-urlencoded";
//リクエストパラメータを作成する
string postStr = "appid=<あなたのアプリケーションID>&" +
"results=ma&" +
"sentence=" + Uri.EscapeUriString(text);
byte[] postData = Encoding.UTF8.GetBytes(postStr);
webreq.ContentLength = postData.Length;
//データを送信する
Stream reqStrm = webreq.GetRequestStream();
reqStrm.Write(postData, 0, postData.Length);
reqStrm.Close();
//サーバーからの応答を受信するためのHttpWebResponseを取得...
HttpWebResponse webres = (HttpWebResponse)webreq.GetRespo...
//応答データを受信するためのStreamを取得する
Stream st = webres.GetResponseStream();
StreamReader sr = new StreamReader(st);
//応答データをすべて読み込む
string resultXml = sr.ReadToEnd();
//閉じる
sr.Close();
st.Close();
webres.Close();
//結果を表示する
Console.WriteLine(resultXml);
}}
このコードを実行した結果は、以下のようになります。(実際...
#pre{{
<?xml version="1.0" encoding="UTF-8" ?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...
xmlns="urn:yahoo:jp:jlp"
xsi:schemaLocation="urn:yahoo:jp:jlp http://jlp.yahooapi...
<ma_result>
<total_count>15</total_count>
<filtered_count>15</filtered_count>
<word_list>
<word>
<surface>今日</surface>
<reading>きょう</reading>
<pos>名詞</pos>
</word>
<word>
<surface>は</surface>
<reading>は</reading>
<pos>助詞</pos>
</word>
<word>
<surface>いい</surface>
<reading>いい</reading>
<pos>形容詞</pos>
</word>
<word>
<surface>天気</surface>
<reading>てんき</reading>
<pos>名詞</pos>
</word>
<word>
<surface>です</surface>
<reading>です</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>。</surface>
<reading>。</reading>
<pos>特殊</pos>
</word>
<word>
<surface>明日</surface>
<reading>あした</reading>
<pos>名詞</pos>
</word>
<word>
<surface>も</surface>
<reading>も</reading>
<pos>助詞</pos>
</word>
<word>
<surface>いい</surface>
<reading>いい</reading>
<pos>形容詞</pos>
</word>
<word>
<surface>天気</surface>
<reading>てんき</reading>
<pos>名詞</pos>
</word>
<word>
<surface>に</surface>
<reading>に</reading>
<pos>助詞</pos>
</word>
<word>
<surface>なる</surface>
<reading>なる</reading>
<pos>動詞</pos>
</word>
<word>
<surface>でしょ</surface>
<reading>でしょ</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>う</surface>
<reading>う</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>。</surface>
<reading>。</reading>
<pos>特殊</pos>
</word>
</word_list>
</ma_result>
</ResultSet>
}}
resultsパラメータを"uniq"にして、出現頻度情報の結果のみを...
#pre{{
<?xml version="1.0" encoding="UTF-8" ?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...
xmlns="urn:yahoo:jp:jlp"
xsi:schemaLocation="urn:yahoo:jp:jlp http://jlp.yahooapi...
<uniq_result>
<total_count>15</total_count>
<filtered_count>15</filtered_count>
<word_list>
<word>
<count>2</count>
<surface>。</surface>
<reading/>
<pos>特殊</pos>
</word>
<word>
<count>2</count>
<surface>いい</surface>
<reading/>
<pos>形容詞</pos>
</word>
<word>
<count>2</count>
<surface>天気</surface>
<reading/>
<pos>名詞</pos>
</word>
<word>
<count>1</count>
<surface>う</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>でしょ</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>です</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>なる</surface>
<reading/>
<pos>動詞</pos>
</word>
<word>
<count>1</count>
<surface>に</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>は</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>も</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>今日</surface>
<reading/>
<pos>名詞</pos>
</word>
<word>
<count>1</count>
<surface>明日</surface>
<reading/>
<pos>名詞</pos>
</word>
</word_list>
</uniq_result>
</ResultSet>
}}
さらに、resultsパラメータを"ma,uniq"にして、通常の形態素...
#pre{{
<?xml version="1.0" encoding="UTF-8" ?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...
xmlns="urn:yahoo:jp:jlp"
xsi:schemaLocation="urn:yahoo:jp:jlp http://jlp.yahooapi...
<ma_result>
<total_count>15</total_count>
<filtered_count>15</filtered_count>
<word_list>
<word>
<surface>今日</surface>
<reading>きょう</reading>
<pos>名詞</pos>
</word>
<word>
<surface>は</surface>
<reading>は</reading>
<pos>助詞</pos>
</word>
<word>
<surface>いい</surface>
<reading>いい</reading>
<pos>形容詞</pos>
</word>
<word>
<surface>天気</surface>
<reading>てんき</reading>
<pos>名詞</pos>
</word>
<word>
<surface>です</surface>
<reading>です</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>。</surface>
<reading>。</reading>
<pos>特殊</pos>
</word>
<word>
<surface>明日</surface>
<reading>あした</reading>
<pos>名詞</pos>
</word>
<word>
<surface>も</surface>
<reading>も</reading>
<pos>助詞</pos>
</word>
<word>
<surface>いい</surface>
<reading>いい</reading>
<pos>形容詞</pos>
</word>
<word>
<surface>天気</surface>
<reading>てんき</reading>
<pos>名詞</pos>
</word>
<word>
<surface>に</surface>
<reading>に</reading>
<pos>助詞</pos>
</word>
<word>
<surface>なる</surface>
<reading>なる</reading>
<pos>動詞</pos>
</word>
<word>
<surface>でしょ</surface>
<reading>でしょ</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>う</surface>
<reading>う</reading>
<pos>助動詞</pos>
</word>
<word>
<surface>。</surface>
<reading>。</reading>
<pos>特殊</pos>
</word>
</word_list>
</ma_result>
<uniq_result>
<total_count>15</total_count>
<filtered_count>15</filtered_count>
<word_list>
<word>
<count>2</count>
<surface>。</surface>
<reading/>
<pos>特殊</pos>
</word>
<word>
<count>2</count>
<surface>いい</surface>
<reading/>
<pos>形容詞</pos>
</word>
<word>
<count>2</count>
<surface>天気</surface>
<reading/>
<pos>名詞</pos>
</word>
<word>
<count>1</count>
<surface>う</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>でしょ</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>です</surface>
<reading/>
<pos>助動詞</pos>
</word>
<word>
<count>1</count>
<surface>なる</surface>
<reading/>
<pos>動詞</pos>
</word>
<word>
<count>1</count>
<surface>に</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>は</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>も</surface>
<reading/>
<pos>助詞</pos>
</word>
<word>
<count>1</count>
<surface>今日</surface>
<reading/>
<pos>名詞</pos>
</word>
<word>
<count>1</count>
<surface>明日</surface>
<reading/>
<pos>名詞</pos>
</word>
</word_list>
</uniq_result>
</ResultSet>
}}
**日本語形態素解析Web APIが返す結果の説明 [#z7618d18]
results=ma の時は、形態素解析の結果がma_resultという要素...
word_list要素は、形態素のリストを表します。word_list要素...
results=ma の時はword_listの形態素の順番が元の文章のまま...
上記の例ではword要素内の要素は3つだけですが、リクエストパ...
word要素内に入る可能性がある要素と、その意味を以下に表で...
|要素|説明|h
|surface|形態素の表記。表層形。|
|reading|形態素の読みがな。読み。ひらがなで返される。uniq...
|pos|形態素の品詞。形容詞、形容動詞、感動詞、副詞、連体詞...
|baseform|形態素の基本形表記。原形。|
|feature|形態素の全情報。このフォーマットに関する説明が公...
|count|同一形態素の出現数。uniq_resultの場合のみ。|
**リクエスト時に指定できるパラメータ [#j0745cb3]
以下に、リクエスト時に指定できるパラメータを表にして説明...
|パラメータ|説明|h
|appid|必須パラメータ。登録済のアプリケーションIDを指定す...
|sentence|必須パラメータ。解析対象のテキスト。|
|results|必須パラメータ。解析結果の種類を指定する。"ma"、...
|response|取得したい形態素の情報を指定する。省略すると、"...
|filter|結果を取得したい形態素の品詞を数字で指定する。複...
|ma_response|"results=ma,uniq"とした時に、responseパラメ...
|ma_filter|"results=ma,uniq"とした時に、filterパラメータ...
|uniq_response|"results=ma,uniq"とした時に、responseパラ...
|uniq_filter|"results=ma,uniq"とした時に、filterパラメー...
|uniq_by_baseform|"results=uniq"の時、通常は全く同一の形...
**結果を解析して表示する [#w9176b77]
結果を解析して表示する例を示します。XMLの解析にはXmlDocum...
また、以前の例では省略していたresponseパラメータを指定し...
#code(vbnet){{
'Imports System.IO
'Imports System.Net
'Imports System.Text
'Imports System.Xml
'形態素解析する文章
Dim text As String = "今日はいい天気です。明日もいい天気...
'HttpWebRequestを作成する
Dim webreq As HttpWebRequest = DirectCast( _
System.Net.WebRequest.Create( _
"http://jlp.yahooapis.jp/MAService/V1/parse"), Ht...
'POSTとしてデータを送信する
webreq.Method = "POST"
webreq.ContentType = "application/x-www-form-urlencoded"
'リクエストパラメータを作成する
Dim postStr As String = "appid=<あなたのアプリケーションI...
"results=ma&" & _
"response=surface,reading,pos,baseform,feature&" & _
"sentence=" & Uri.EscapeUriString(text)
Dim postData As Byte() = Encoding.UTF8.GetBytes(postStr)
webreq.ContentLength = postData.Length
'データを送信する
Dim reqStrm As Stream = webreq.GetRequestStream()
reqStrm.Write(postData, 0, postData.Length)
reqStrm.Close()
'サーバーからの応答を受信するためのHttpWebResponseを取得...
Dim webres As HttpWebResponse = DirectCast(webreq.GetResp...
'応答データを受信するためのStreamを取得する
Dim st As Stream = webres.GetResponseStream()
Dim sr As New StreamReader(st)
'応答データをすべて読み込む
Dim resultXml As String = sr.ReadToEnd()
'閉じる
sr.Close()
st.Close()
webres.Close()
'XmlDocumentに読み込む
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(resultXml)
'ルートを取得
Dim rootElm As XmlElement = xmlDoc.DocumentElement
'情報の表示
If rootElm.Name = "ResultSet" Then
'形態素解析の結果が返されているときは、解析する
Dim maElm As XmlElement = rootElm("ma_result")
If maElm IsNot Nothing Then
Console.WriteLine("===== ma_result =====")
'形態素の総数
Console.WriteLine("total_count:{0}", maElm("total...
'フィルタにマッチした形態素数
Console.WriteLine("filtered_count:{0}", maElm("fi...
Console.WriteLine()
'形態素のリストを列挙する
Dim wlElm As XmlElement = maElm("word_list")
If wlElm IsNot Nothing Then
For Each e As XmlElement In wlElm.ChildNodes
'表記
If e("surface") IsNot Nothing Then
Console.WriteLine("surface:{0}", e("s...
End If
'読みがな
If e("reading") IsNot Nothing Then
Console.WriteLine("reading:{0}", e("r...
End If
'品詞
If e("pos") IsNot Nothing Then
Console.WriteLine("pos:{0}", e("pos")...
End If
'基本形表記
If e("baseform") IsNot Nothing Then
Console.WriteLine("baseform:{0}", e("...
End If
'全情報
If e("feature") IsNot Nothing Then
Console.WriteLine("feature:{0}", e("f...
End If
Console.WriteLine()
Next
End If
End If
End If
}}
#code(csharp){{
//using System;
//using System.IO;
//using System.Net;
//using System.Text;
//using System.Xml;
//形態素解析する文章
string text = "今日はいい天気です。明日もいい天気になるで...
//HttpWebRequestを作成する
HttpWebRequest webreq = (HttpWebRequest)System.Net.WebReq...
"http://jlp.yahooapis.jp/MAService/V1/parse");
//POSTとしてデータを送信する
webreq.Method = "POST";
webreq.ContentType = "application/x-www-form-urlencoded";
//リクエストパラメータを作成する
string postStr = "appid=<あなたのアプリケーションID>&" +
"results=ma&" +
"response=surface,reading,pos,baseform,feature&" +
"sentence=" + Uri.EscapeUriString(text);
byte[] postData = Encoding.UTF8.GetBytes(postStr);
webreq.ContentLength = postData.Length;
//データを送信する
Stream reqStrm = webreq.GetRequestStream();
reqStrm.Write(postData, 0, postData.Length);
reqStrm.Close();
//サーバーからの応答を受信するためのHttpWebResponseを取得...
HttpWebResponse webres = (HttpWebResponse)webreq.GetRespo...
//応答データを受信するためのStreamを取得する
Stream st = webres.GetResponseStream();
StreamReader sr = new StreamReader(st);
//応答データをすべて読み込む
string resultXml = sr.ReadToEnd();
//閉じる
sr.Close();
st.Close();
webres.Close();
//XmlDocumentに読み込む
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(resultXml);
//ルートを取得
XmlElement rootElm = xmlDoc.DocumentElement;
//情報の表示
if (rootElm.Name == "ResultSet")
{
//形態素解析の結果が返されているときは、解析する
XmlElement maElm = rootElm["ma_result"];
if (maElm != null)
{
Console.WriteLine("===== ma_result =====");
//形態素の総数
Console.WriteLine("total_count:{0}", maElm["total...
//フィルタにマッチした形態素数
Console.WriteLine("filtered_count:{0}", maElm["fi...
Console.WriteLine();
//形態素のリストを列挙する
XmlElement wlElm = maElm["word_list"];
if (wlElm != null)
{
foreach (XmlElement e in wlElm.ChildNodes)
{
//表記
if (e["surface"] != null)
{
Console.WriteLine("surface:{0}", e["s...
}
//読みがな
if (e["reading"] != null)
{
Console.WriteLine("reading:{0}", e["r...
}
//品詞
if (e["pos"] != null)
{
Console.WriteLine("pos:{0}", e["pos"]...
}
//基本形表記
if (e["baseform"] != null)
{
Console.WriteLine("baseform:{0}", e["...
}
//全情報
if (e["feature"] != null)
{
Console.WriteLine("feature:{0}", e["f...
}
Console.WriteLine();
}
}
}
}
}}
結果は、以下のようになります。
#pre{{
===== ma_result =====
total_count:15
filtered_count:15
surface:今日
reading:きょう
pos:名詞
baseform:今日
feature:名詞,名詞,*,今日,きょう,今日
surface:は
reading:は
pos:助詞
baseform:は
feature:助詞,係助詞,*,は,は,は
surface:いい
reading:いい
pos:形容詞
baseform:いい
feature:形容詞,形容,基本形,いい,いい,いい
surface:天気
reading:てんき
pos:名詞
baseform:天気
feature:名詞,名詞,*,天気,てんき,天気
surface:です
reading:です
pos:助動詞
baseform:です
feature:助動詞,助動詞です,基本形,です,です,です
surface:。
reading:。
pos:特殊
baseform:。
feature:特殊,句点,*,。,。,。
surface:明日
reading:あした
pos:名詞
baseform:明日
feature:名詞,名詞,*,明日,あした,明日
surface:も
reading:も
pos:助詞
baseform:も
feature:助詞,係助詞,*,も,も,も
surface:いい
reading:いい
pos:形容詞
baseform:いい
feature:形容詞,形容,基本形,いい,いい,いい
surface:天気
reading:てんき
pos:名詞
baseform:天気
feature:名詞,名詞,*,天気,てんき,天気
surface:に
reading:に
pos:助詞
baseform:に
feature:助詞,格助詞,*,に,に,に
surface:なる
reading:なる
pos:動詞
baseform:なる
feature:動詞,ラ五,基本形,なる,なる,なる
surface:でしょ
reading:でしょ
pos:助動詞
baseform:です
feature:助動詞,助動詞です,未然形,でしょ,でしょ,です
surface:う
reading:う
pos:助動詞
baseform:う
feature:助動詞,助動詞,*,う,う,う
surface:。
reading:。
pos:特殊
baseform:。
feature:特殊,句点,*,。,。,。
}}
"results=uniq"にして出現頻度情報の結果を取得するコードは...
#pre{{
===== uniq_result =====
total_count:15
filtered_count:15
count:2
surface:。
reading:
pos:特殊
baseform:。
feature:特殊,*,*,。,,。
count:2
surface:いい
reading:
pos:形容詞
baseform:いい
feature:形容詞,*,*,いい,,いい
count:2
surface:天気
reading:
pos:名詞
baseform:天気
feature:名詞,*,*,天気,,天気
count:1
surface:う
reading:
pos:助動詞
baseform:う
feature:助動詞,*,*,う,,う
count:1
surface:でしょ
reading:
pos:助動詞
baseform:です
feature:助動詞,*,*,でしょ,,です
count:1
surface:です
reading:
pos:助動詞
baseform:です
feature:助動詞,*,*,です,,です
count:1
surface:なる
reading:
pos:動詞
baseform:なる
feature:動詞,*,*,なる,,なる
count:1
surface:に
reading:
pos:助詞
baseform:に
feature:助詞,*,*,に,,に
count:1
surface:は
reading:
pos:助詞
baseform:は
feature:助詞,*,*,は,,は
count:1
surface:も
reading:
pos:助詞
baseform:も
feature:助詞,*,*,も,,も
count:1
surface:今日
reading:
pos:名詞
baseform:今日
feature:名詞,*,*,今日,,今日
count:1
surface:明日
reading:
pos:名詞
baseform:明日
feature:名詞,*,*,明日,,明日
}}
**最後に [#cefc2be6]
今年の.NETプログラミング研究はこれが最後です。今年一年お...
**コメント [#ued8a753]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]] [[:Category/ASP.NET]],2010-1...
ページ名:
▲
▼
[
トップ
] [
新規
|
子ページ作成
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]