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