2012年10月19日金曜日

groovyでk-means++法を使用してクラスタリングする

groovyでk-means++法を使用してクラスタリングするには、以下のコードを実行します。
@Grab(group='org.apache.commons', module='commons-math3', version='3.0')
import org.apache.commons.math3.stat.clustering.*
import java.util.*

def points = [
new EuclideanIntegerPoint([10,10] as int[]),
new EuclideanIntegerPoint([12,11] as int[]),
new EuclideanIntegerPoint([30,23] as int[]),
new EuclideanIntegerPoint([91,20] as int[]),
new EuclideanIntegerPoint([95,17] as int[]),
new EuclideanIntegerPoint([11,89] as int[]),
new EuclideanIntegerPoint([12,85] as int[]),
new EuclideanIntegerPoint([7,88] as int[])
]

def kmpp = new KMeansPlusPlusClusterer(new Random())
def clusters = kmpp.cluster(points, 4, 10)
def cn = 0
for(cluster in clusters){
  println "cluster : ${cn}"
  for(point in cluster.getPoints()){
    println point.getPoint()
  }
  cn++
}

2012年10月8日月曜日

groovyとFreeTTSで音声合成する

groovyとFreeTTSで音声合成するには、以下のコードを実行します。
import com.sun.speech.freetts.*
System.setProperty("com.sun.speech.freetts.voice.defaultAudioPlayer",
  "com.sun.speech.freetts.audio.SingleFileAudioPlayer")
def voice = VoiceManager.getInstance().getVoice("kevin16")
voice.allocate()
// Hello, Groovy. こんにちはgroovy!
voice.speak("Hello, Groovy. kon nitch were, Groovy!")
voice.deallocate()
// ubuntuのplayerを使ってwavファイル再生
"aplay freetts.wav".execute()


インストール手順(ubuntu)
1)freetts-1.2.2-bin.zipをhttp://freetts.sourceforge.net/docs/index.phpからダウンロード
2)sudo apt-get install sharutils (jsapi.shでuuencodeを使用するので)
3)unzip freetts-1.2.2-bin.zip
4)解凍したlibディレクトリに移動してchmod +x jsapi.sh
5)./jsapi.sh
6)cp *.jar ~/.groovy/lib

動作環境
OpenJDK1.7.0_03, groovy 2.0.1, Ubuntu12.04

2012年5月26日土曜日

groovyとMongoHQのREST APIで、コレクションを一覧表示する

groovyとMongoHQのREST APIで、コレクションを一覧表示するには、以下のコードを実行します。
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.3')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import groovy.json.*

def _apikey = "your-key"
def base = "https://api.mongohq.com/databases"
def db = "mydb"
def httpclient = new DefaultHttpClient()
def method = new HttpGet("${base}/${db}/collections?_apikey=${_apikey}")
response = httpclient.execute(method)

println response.getStatusLine().getStatusCode()
def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
for(col in json){
  println "name:${col.name}"
  println "storageSize:${col.storageSize}"
}

動作環境
groovy 1.8.6

関連情報
groovyとMongoDBのまとめ

2012年5月24日木曜日

groovyとMongoHQのREST APIで、コレクションを削除する

groovyとMongoHQのREST APIで、コレクションを削除するには、以下のコードを実行します。
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.3')
import org.apache.http.client.entity.*
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.protocol.*
import groovy.json.*

def _apikey = "your-key"
def base = "https://api.mongohq.com/databases"
def db = "mydb"
def col = "your-collection"

httpclient = new DefaultHttpClient()
method = new HttpDelete("${base}/${db}/collections/${col}?_apikey=${_apikey}")
response = httpclient.execute(method)

println response.getStatusLine().getStatusCode()
println response.getEntity().getContent().text

動作環境
groovy 1.8.6

関連情報
groovyとMongoDBのまとめ

2012年5月22日火曜日

groovyとMongoHQのREST APIで、コレクション内のドキュメントを削除する

groovyとMongoHQのREST APIで、コレクション内のドキュメントを削除するには、以下のコードを実行します。
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.3')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.entity.*
import groovy.json.*

def _apikey = "your-key"
def base = "https://api.mongohq.com/databases"
def db = "mydb"
def col = "employee"
def doc_id = "your-document-id"

def httpclient = new DefaultHttpClient()
def method = new HttpDelete(
  "${base}/${db}/collections/${col}/documents/${doc_id}?_apikey=${_apikey}")
response = httpclient.execute(method)

println response.getStatusLine().getStatusCode()
println response.getEntity().getContent().text

動作環境
groovy 1.8.6

関連情報
groovyとMongoDBのまとめ

2012年5月21日月曜日

groovyとmongolabのREST APIで、コレクションを列挙する

groovyとmongolabのREST APIで、コレクションを列挙するには、以下のコードを実行します。
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.3')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import groovy.json.*

def apikey = "your-key"
def base = "https://api.mongolab.com/api/1"
def db = "mydb"
def httpclient = new DefaultHttpClient()
def method = new HttpGet("${base}/databases/${db}/collections?apiKey=${apikey}")
response = httpclient.execute(method)

println response.getStatusLine().getStatusCode()
def json = new JsonSlurper().parseText(response.getEntity().getContent().text)
for(col in json){
  println "name:${col}"
}

動作環境
groovy 1.8.6

関連情報
groovyとMongoDBのまとめ

2012年5月20日日曜日

groovyとMongoHQのREST APIで、コレクション内のドキュメントを更新する

groovyとMongoHQのREST APIで、コレクション内のドキュメントを更新するには、以下のコードを実行します。
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.3')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.entity.*
import groovy.json.*

def _apikey = "your-key"
def base = "https://api.mongohq.com/databases"
def db = "mydb"
def col = "employee"
def doc_id = "your-document-id"
def json = new JsonBuilder()
json (
  document:[
    '$set': ["name":"テスト2"]
  ],
  safe:true
)

def httpclient = new DefaultHttpClient()
def method = new HttpPut(
  "${base}/${db}/collections/${col}/documents/${doc_id}?_apikey=${_apikey}")
method.setHeader("Content-Type", "application/json; charset=utf-8")
method.setEntity(new StringEntity(json.toString(), "UTF-8"))
response = httpclient.execute(method)

println response.getStatusLine().getStatusCode()
println response.getEntity().getContent().text

動作環境
groovy 1.8.6

関連情報
groovyとMongoDBのまとめ