LoginSignup
52
48

More than 5 years have passed since last update.

Googleスプレッドシートで形態素解析

Last updated at Posted at 2014-08-13

Google Apps Script と Yahoo! の日本語形態素解析API を使って、Googleスプレッドシート上で形態素解析を行います。下記の記事の内容をまとめ直したものです。

日本語形態素解析APIの設定

まず、Yahoo!デベロッパーネットワークにて、アプリケーションIDを取得します。やり方は、下記のブログが参考になります。

スクリプト

yahooTextSegmentation.gs
/**
 * Yahoo! 日本語形態素解析APIの利用
 */

var ss = SpreadsheetApp.getActiveSpreadsheet();
var mySheet = ss.getActiveSheet();

function yahooTextSegmentation(cell)
{  
  var myAppid = "ここにアプリケーションIDを入力";
  var text = cell;
  var myUrl = "http://jlp.yahooapis.jp/MAService/V1/parse?appid="+myAppid+"&results=ma&sentence="+text;

  Utilities.sleep(1000);
  var myXml = UrlFetchApp.fetch(myUrl);
  var myDoc = XmlService.parse(myXml.getContentText());
  var namespace = XmlService.getNamespace("urn:yahoo:jp:jlp");
  var root = myDoc.getRootElement();
  var ma_result = root.getChild("ma_result", namespace);
  var word_list = ma_result.getChild("word_list", namespace);
  var word_array = word_list.getChildren("word", namespace);
  var surface = word_array[0].getChild("surface", namespace);

  var array = [];
  for (var i=0; i < word_array.length; i++) {
    array[i] = [
      word_array[i].getAllContent()[0].asElement().getText(),
      word_array[i].getAllContent()[1].asElement().getText(),
      word_array[i].getAllContent()[2].asElement().getText()
    ];
  }
  return array;
}

使い方

Googleスプレッドシート上で、任意のセルにテキストを入力(例として、A2セル)し、=yahooTextSegmentation(A2)とすると、下記のようにテキストが形態素解析されます。

alt

52
48
0

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
52
48