2015年11月1日日曜日
2015年9月27日日曜日
JavaMail IMAPでGmailの「送信済みメール」を読む
おおむね次のようなコードでフォルダの階層構造を調べてみた。
void showFolders() throws MessagingException {
Properties props = ... // IMAP接続情報を設定する
Session session = Session.getInstance(props);
Store store = session.getStore("imap")
store.connect()
Folder f = store.getDefaultFolder();
showFolder(f)
store.close();
}
void showFolder(Folder f) throws MessagingException {
for(Folder _f: f.list()) {
System.out.println(_f.getFullName());
showFolder(_f);
}
}
特に自前でラベルを定義していない素に近いアカウントで調べた結果、以下のようなフォルダ構造として見えるようである。
(DefaultFolder) ├─INBOX ├─[Gmail] │ ├─すべてのメール │ ├─ゴミ箱 │ ├─下書き │ ├─迷惑メール │ ├─送信済みメール │ └─重要 ├─プライベート ├─仕事 ├─旅行 └─領収書
「送信済みメール」は[Gmail]のサブフォルダになっているが、次のようなパス指定でStoreからの直接取得もできるようである。
Store store = session.getStore("imap")
store.connect()
Folder f = store.getFolder("[Gmail]/送信済みメール");
それにしてもフォルダ名がローカライズされてしまっているのが扱いに面倒そうである。 試していないがGmailの設定を英語にしている場合は「Sent Mail」や「Drafts」などのフォルダ名になるんだろうと想像がつく。 言語設定に関係なく統一的に扱えるとプログラム側は楽なのに…
2014年9月22日月曜日
Mahoutでレコメンドを動かすまで(その2)
- 1列目 - おすすめしている人のID
- 2列目 - おすすめしているアイテムのID
- 3列目 - 嗜好の度合い(いくつでもいい?)
例えば次のような場合、1行目はユーザーIDが1の人が、アイテム番号100を気に入っており、その度合いが5を表しています。
1,100,5
1,200,3
1,300,3
2,100,5
2,200,5
2,400,3
2,900,4
3,200,4
3,300,5
3,700,4
4,200,5
4,400,4
4,600,5
4,900,5
5,100,5
5,200,3
5,400,4
5,500,6
5,700,5
6,100,3
6,200,4
6,400,2
6,700,3
7,100,4
7,700,5
7,800,5
7,900,5
列はどれも数値にする必要があります。現状持っているデータのユーザーIDなどが文字列の場合は変換テーブルを作って数値を割り振るような対応になるんでしょうね。
データが準備できたら以下のコマンドを使って早速計算をしてみます。
bin/mahout recommenditembased -i recommend.txt -o recommend_out -s SIMILARITY_PEARSON_CORRELATION
ここの環境だと計算に10秒近くかかりました。
結果は引数で指定したrecommend_outフォルダのファイルpart-r-00000に記録されます。 今回のサンプルの結果だと以下のようになります。各行がそれぞれのユーザーへのおすすめを示しており、1行目はユーザーIDが2の人へのおすすめはアイテム番号700で、スコアが3.9172406というのを示しているようです。
vmplanet@ubuntu:~/mahout-distribution-0.9$ cat recommend_out/part-r-00000
2 [700:3.9172406]
4 [700:4.19286]
5 [900:4.375]
6 [900:2.375]
7 [400:4.6099067]
数字だけ見ていると合っているのかはピンときませんが、一応レコメンドが計算できたようです。
vmplanet@ubuntu:~/mahout-distribution-0.9$ ls -l recommend_out/
合計 4
-rwxrwxrwx 1 vmplanet vmplanet 0 2014-09-18 20:33 part-r-00000
-rwxrwxrwx 1 vmplanet vmplanet 0 2014-09-18 20:33 _SUCCE
正確にはわかりませんが、元データが20行くらいになると結果が出てくるようです。
14/09/18 20:43:26 WARN mapred.LocalJobRunner: job_local1173485433_0001
java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 1
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:354)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at org.apache.mahout.cf.taste.hadoop.item.ItemIDIndexMapper.map(ItemIDIndexMapper.java:50)
データを変更して再度計算しようと思いコマンドを再実行したところ以下のエラーとなりました。
14/09/18 20:35:35 ERROR security.UserGroupInformation: PriviledgedActionException as:vmplanet cause:org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory temp/preparePreferenceMatrix/itemIDIndex already exists
出力先で指定したフォルダと、計算途中でできたtempフォルダがあるとエラーになるので、実行前には以下のコマンドできれいにしてから実行するといいでしょう。
rm -rf temp/ recommend_out/
Mahoutでレコメンドを動かすまで(その1)
機械学習というと何やらすごく神秘的なものを感じます。 その機械学習のためのツールにMahoutというものがあると聞き早速試してみることに。
まずはWindowsで動かしAmazonのレコメンドみたいなものをやってみようといろいろ試しました、が、いろいろなエラーに遭遇したため一旦挫折、まずはLinuxを使うことにしました。 Windowsの方はまた動かせるようになったら報告します。
Linux版の方でも動かすためにはいろいろ躓くポイントがありました。以下、とりあえず動かすまでにやったことを記します。環境は以下を使用しました。まずはお試しということでHadoopは無しでやっています。
- Ubuntu 10.0.4 Mahout 0.9
Mahoutをダウンロード、展開していきなりコマンドをたたくと、以下のように怒られます。環境変数JAVA_HOMEにJavaのインストール先を設定する必要があるようです。
vmplanet@ubuntu:~/mahout-distribution-0.9$ bin/mahout
Error: JAVA_HOME is not set.
MAHOUT_JAVA_HOMEがあるときはJAVA_HOMEのかわりにこちらが使われるとのことなので、この変数にJavaのインストール先を設定します。
export MAHOUT_JAVA_HOME=/usr/lib/jvm/java-6-openjdk
ちなみに環境変数の一覧はbin/mahoutの中を除くと説明が書いてあります。
使っていたLinux環境のメモリが少なかったのが原因ですがMAHOUT_JAVA_HOMEを設定して動かすと今度は以下のエラーになりました。
vmplanet@ubuntu:~/mahout-distribution-0.9$ bin/mahout
hadoop binary is not in PATH,HADOOP_HOME/bin,HADOOP_PREFIX/bin, running locally
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
ヒープを確保できなかったというエラー。確保するヒープサイズはMAHOUT_HEAPSIZEに設定するようです。単位はMBです。指定しない場合のデフォルトは1024らしいので、これだけ確保しようとして失敗していたのが原因のようです。
とりあえず以下のように少し少なめに設定しました。
export MAHOUT_HEAPSIZE=512
ここまで設定してやっと動くようになりました。 引数を何もつけないで実行すると使える分析アルゴリズムの一覧が表示されます。
vmplanet@ubuntu:~/mahout-distribution-0.9$ bin/mahout
hadoop binary is not in PATH,HADOOP_HOME/bin,HADOOP_PREFIX/bin, running locally
An example program must be given as the first argument.
Valid program names are:
arff.vector: : Generate Vectors from an ARFF file or directory
baumwelch: : Baum-Welch algorithm for unsupervised HMM training
canopy: : Canopy clustering
cat: : Print a file or resource as the logistic regression models would see it
cleansvd: : Cleanup and verification of SVD output
clusterdump: : Dump cluster output to text
clusterpp: : Groups Clustering Output In Clusters
cmdump: : Dump confusion matrix in HTML or text formats
concatmatrices: : Concatenates 2 matrices of same cardinality into a single matrix
cvb: : LDA via Collapsed Variation Bayes (0th deriv. approx)
cvb0_local: : LDA via Collapsed Variation Bayes, in memory locally.
evaluateFactorization: : compute RMSE and MAE of a rating matrix factorization against probes
fkmeans: : Fuzzy K-means clustering
hmmpredict: : Generate random sequence of observations by given HMM
itemsimilarity: : Compute the item-item-similarities for item-based collaborative filtering
kmeans: : K-means clustering
lucene.vector: : Generate Vectors from a Lucene index
lucene2seq: : Generate Text SequenceFiles from a Lucene index
matrixdump: : Dump matrix in CSV format
matrixmult: : Take the product of two matrices
parallelALS: : ALS-WR factorization of a rating matrix
qualcluster: : Runs clustering experiments and summarizes results in a CSV
recommendfactorized: : Compute recommendations using the factorization of a rating matrix
recommenditembased: : Compute recommendations using item-based collaborative filtering
regexconverter: : Convert text files on a per line basis based on regular expressions
resplit: : Splits a set of SequenceFiles into a number of equal splits
rowid: : Map SequenceFile to {SequenceFile, SequenceFile}
rowsimilarity: : Compute the pairwise similarities of the rows of a matrix
runAdaptiveLogistic: : Score new production data using a probably trained and validated AdaptivelogisticRegression model
runlogistic: : Run a logistic regression model against CSV data
seq2encoded: : Encoded Sparse Vector generation from Text sequence files
seq2sparse: : Sparse Vector generation from Text sequence files
seqdirectory: : Generate sequence files (of Text) from a directory
seqdumper: : Generic Sequence File dumper
seqmailarchives: : Creates SequenceFile from a directory containing gzipped mail archives
seqwiki: : Wikipedia xml dump to sequence file
spectralkmeans: : Spectral k-means clustering
split: : Split Input data into test and train sets
splitDataset: : split a rating dataset into training and probe parts
ssvd: : Stochastic SVD
streamingkmeans: : Streaming k-means clustering
svd: : Lanczos Singular Value Decomposition
testnb: : Test the Vector-based Bayes classifier
trainAdaptiveLogistic: : Train an AdaptivelogisticRegression model
trainlogistic: : Train a logistic regression using stochastic gradient descent
trainnb: : Train the Vector-based Bayes classifier
transpose: : Take the transpose of a matrix
validateAdaptiveLogistic: : Validate an AdaptivelogisticRegression model against hold-out data set
vecdist: : Compute the distances between a set of Vectors (or Cluster or Canopy, they must fit in memory) and a list of Vectors
vectordump: : Dump vectors from a sequence file to text
viterbi: : Viterbi decoding of hidden states from given output states sequence
お勧め商品などのレコメンドを計算するにはrecommenditembasedというアルゴリズムを使います。早速これを使ったレコメンドをしてみたいと思いますが長くなったので続きは次回。
2013年11月14日木曜日
Android Eclipse ADTでビルドできていたプロジェクトが急にコンパイルエラーになった時の対処法
自分のところではEclipseを落としてあげなおしたときに良く発生する。
原因としては自動生成のR.javaがなくなるなどgenフォルダの中身がおかしくなってコンパイルエラーというようである。
そうなってしまうと厄介だが、今のところ以下のいづれかで復旧できるようだ。
- プロジェクトをクリーンする。
- プロジェクトを右クリックし、"Android Tools"→"Fix Project Properties"を選択、その後プロジェクトをクリーンする。
- プロジェクトを一旦削除(ファイルは消さないこと!)。その後再度インポート。
ただ状況によってどれが効くというのが異なるので、いろいろ試してみることになる。
2013年8月19日月曜日
PowerPoint 中国語フォントでコピーされてしまう
たとえば「全日本人民会議」は貼り付けてもフォントが変わらない(スライド既定のMSゴシックのまま)、しかし「全国人民会議」を貼り付けるとPMingLiU になってしまう。
オプションを探したがオフにできそうな設定もない。以下でも同じ問題で困っている人がいるようである。
このフォーラムではパッチをあてるとなおることのようだが、とりあえずの回避として、確実に中国語でない文字を一文字入れてやるというのが簡単である。たとえば「全国人民会議あ」のような感じ。
2013年7月8日月曜日
Android 指を近づけただけで応答 FloatingTouch
XPERIAの場合APIとしてはView.OnHoverListenerあたりが呼ばれるようです。 HoverはPCではよく使うAPIですがAndroidにもあるので、いつ呼ばれるのだろうと思っていましたがこういう機能向けだったのですね。 Galaxyのほうも同じような仕様なんだろうか…。
うまくつかうとUIの表現方法が広がりそうですが実機が無いので試せないのが残念。 今後搭載端末が増えてきて標準的に使えるようになるのを期待したいです。
それにしてもコマーシャルを見ているとGalaxy特有の機能に見えてしまう。 SONYももっと宣伝すればいいのに… キラーアプリを作れなかったのか?