はじめに
最終回となる今回は,
この可視化では,
ソースコードのダウンロード
今回作成するプログラムのソースコードは,
特徴量ベクトルの生成
前回のプログラムでは,
このとき問題となるのは,
そこで,
リスト1 IndexMapper.
public class IndexMapper {
private Map<Object, Integer> map = new HashMap<Object, Integer>();
public int size() { return map.size(); }
public boolean contains(Object obj) {
// インデックスが割り振り済みであるかどうかを調べる
return map.containsKey(obj);
}
public void put(Object obj) {
if (!contains(obj)) {
// 新しいオブジェクトの場合,現在のサイズを新規インデックスとして割り振る
int newIndex = size();
map.put(obj, newIndex);
}
}
public int get(Object obj) {
// インデックスが割り振られていない場合は例外を発生
if (!contains(obj)) {
throw new IllegalArgumentException("Object is not found.");
}
// インデックスを返す
return map.get(obj);
}
}
IndexMapperクラスを使って,
ブックマークノードクラスの作成
タグのベクトル化が可能となったところで,
リスト2 BookmarkItem.
public class BookmarkItem extends Item {
...
public BookmarkItem(Bookmark bookmark, BookmarkDetail detail,
IndexMapper mapper) {
// ブックマーク数をそのまま面積化すると比率が極端になるので
// 平方根をとって調整する
super(bookmark.title, tagsToVector(detail, mapper),
Math.sqrt(detail.bookmarkCount));
this.bookmark = bookmark;
this.detail = detail;
}
private static MultiVector tagsToVector(BookmarkDetail detail,
IndexMapper mapper) {
MultiVector vector = new MultiVector(mapper.size());
for (String tag : detail.tags) {
if (mapper.contains(tag)) {
int index = mapper.get(tag);
vector.set(index, vector.get(index) + 1);
}
}
vector.normalize(); // ベクトルを正規化する
return vector;
}
}
MultiVectorオブジェクトを作成した後,