<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yukun&#039;s Blog &#187; Algorithm</title>
	<atom:link href="http://www.yukun.info/blog/tag/algorithm/feed" rel="self" type="application/rss+xml" />
	<link>http://www.yukun.info</link>
	<description>難しいことは分かりやすく、簡単なことは面白く紹介</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:33:59 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>検索エンジンを実装 (6)NOT演算</title>
		<link>http://www.yukun.info/blog/2008/06/java-subtraction.html</link>
		<comments>http://www.yukun.info/blog/2008/06/java-subtraction.html#comments</comments>
		<pubDate>Tue, 03 Jun 2008 15:00:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Search Engine]]></category>
		<category><![CDATA[Set]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/19700101/java%e3%81%a7%e5%85%a8%e6%96%87%e6%a4%9c%e7%b4%a2%e3%82%a8%e3%83%b3%e3%82%b8%e3%83%b3%e3%82%92%e6%a7%8b%e7%af%89-6not%e6%bc%94%e7%ae%97-building-a-full-text-search-engine-on-java-6subtraction-al</guid>
		<description><![CDATA[今回は集合演算のNOT演算ついて紹介します。この処理は、例として検索の際に「sky NOT rain」と指定すると、&#8221;sky&#8221;というキーワードを含むページから&#8221;rain&#8221;を &#8230; <a href="http://www.yukun.info/blog/2008/06/java-subtraction.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/06/java-subtraction.html">検索エンジンを実装 (6)NOT演算</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>今回は集合演算のNOT演算ついて紹介します。この処理は、例として検索の際に「sky NOT rain」と指定すると、&#8221;sky&#8221;というキーワードを含むページから&#8221;rain&#8221;を含むページを除きます。</p>
<h3>NOT演算処理の概要</h3>
<p><a href="http://www.yukun.info/wp-content/uploads/subtraction.gif"><img src="http://www.yukun.info/wp-content/uploads/subtraction-e1269626228977.gif" alt="NOT演算結果" title="subtraction" width="400" height="125" class="size-full wp-image-1476" /></a></p>
<p>上の図から、ある2つの語の転置インデックスリストをA, Bとします。ここで、リスト要素をそれぞれa, b(整数)とし演算結果を格納するリストをCとするとき、NOT演算は主に以下の処理内容を繰り返します。</p>
<ol>
<li>if a < b then 要素aをCの末尾に追加し、aにリストAの次の要素を代入</li>
<li>if a = b then A, Bが指す次の要素をa, bに代入</li>
<li>if a > b then bにリストBの次の要素を代入</li>
</ol>
<h3>ソースコード</h3>
<p>今回はNOT演算処理を行う部分(メソッド)のみを示します。後で示す実行結果は、<a href="http://www.yukun.info/blog/2008/05/java-intersection.html" title="検索エンジンを実装 (4)AND演算">前のブログラム</a>をベースにintersect()メソッドの挿入箇所を今回のものに置き換えたものです。</p>
<pre>
import java.util.ArrayList;
/**
 * 検索エンジンのNOT演算
 */
public class BooleanRetrieval {
  /**
   * NOT演算処理
   * @param postsSet 全ての検索語の転置インデックスリスト
   * @return 演算後の転置インデックスリスト
   */
  public static ArrayList&lt;integer&gt; subtract(ArrayList&lt;arrayList&lt;integer&gt;&gt; postsSet) {
    ArrayList&lt;integer&gt; result; // 最終演算結果
    if (postsSet == null) return null;
    int len = postsSet.size();
    if (len == 0) return null;
    else if (len == 1) return postsSet.get(0);
    result = postsSet.get(0);
    for (int i = 1; i &lt; len; i++) {
      result = subtract(result, postsSet.get(i));
    }
    return result;
  }

  public static ArrayList&lt;integer&gt; subtract(ArrayList&lt;integer&gt; p1, ArrayList&lt;integer&gt; p2) {
    ArrayList&lt;integer&gt; answer = new ArrayList&lt;integer&gt;(); // 2語の演算結果
    int len1 = p1.size();
    int len2 = p2.size();
    int i=0, j=0;
    while (i&lt;len1 &amp;&amp; j&lt;len2) {
      int diff = p1.get(i) - p2.get(j);
      if (diff == 0) {
        i++; j++;
      } else if (diff &lt; 0) {
        answer.add(p1.get(i));
        i++;
      } else {
        j++;
      }
    }
    while (i&lt;len1) { answer.add(p1.get(i++)); }
    while (j&lt;len2) { answer.add(p2.get(j++)); }
    return answer;
  }
}
</pre>
<h3>NOT演算の実行結果</h3>
<pre>単語          freq, docID
15           : 1, [2]
5            : 1, [2]
After        : 1, [1]
As           : 1, [1]
I            : 3, [0, 1, 2]

＜中略＞

that         : 1, [2]
the          : 3, [0, 1, 2]
think        : 3, [0, 1, 2]
this         : 1, [2]
time         : 2, [0, 2]
to           : 2, [0, 1]
touch        : 1, [1]
tremendously : 1, [1]
uncertainty  : 1, [1]
use          : 1, [2]
vigorous     : 1, [1]
wanna        : 1, [1]
well         : 1, [1]
what         : 1, [0]
when         : 1, [1]
where        : 1, [0]
why          : 1, [1]
with         : 1, [1]
検索語: the think
結果　:文書中に存在しません。
検索語: the use
結果　:文書ID [0, 1]に存在します。
検索語: the to
結果　:文書ID [2]に存在します。
検索語: quit</pre>
<p>おー、けっこうたのしくなってきましたねー。</p>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/05/java-union.html" rel="bookmark" title="2008年5月27日">検索エンジンを実装 (5)OR演算</a></li>
<li><a href="http://www.yukun.info/blog/2008/05/java-intersection.html" rel="bookmark" title="2008年5月20日">検索エンジンを実装 (4)AND演算</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram.html" rel="bookmark" title="2008年3月7日">検索エンジンを実装 (1)転置インデックス作成</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram-2.html" rel="bookmark" title="2008年3月16日">検索エンジンを実装 (2)出現位置とその文書ID</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram-3.html" rel="bookmark" title="2008年3月23日">検索エンジンを実装 (3)文書内の検索語を特定</a></li>
</ul>
<p><!-- Similar Posts took 10.090 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/06/java-subtraction.html">検索エンジンを実装 (6)NOT演算</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/06/java-subtraction.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>検索エンジンを実装 (5)OR演算</title>
		<link>http://www.yukun.info/blog/2008/05/java-union.html</link>
		<comments>http://www.yukun.info/blog/2008/05/java-union.html#comments</comments>
		<pubDate>Mon, 26 May 2008 15:00:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Search Engine]]></category>
		<category><![CDATA[Set]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/19700101/java%e3%81%a7%e5%85%a8%e6%96%87%e6%a4%9c%e7%b4%a2%e3%82%a8%e3%83%b3%e3%82%b8%e3%83%b3%e3%82%92%e6%a7%8b%e7%af%89-5or%e6%bc%94%e7%ae%97-building-a-full-text-search-engine-on-java-5union-algorithm</guid>
		<description><![CDATA[前回がAND演算でしたので今回はOR演算ついて紹介します。今記事で紹介している演算アルゴリズムよりも高効率なものは存在するようですが、今回は割愛します。 OR演算処理の概要 上の図から、ある2つの語の転置インデックスリス &#8230; <a href="http://www.yukun.info/blog/2008/05/java-union.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/05/java-union.html">検索エンジンを実装 (5)OR演算</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>前回が<a href="http://www.yukun.info/blog/2008/05/java-intersection.html" title="検索エンジンを実装 (4)AND演算 | Trump Code">AND演算</a>でしたので今回はOR演算ついて紹介します。今記事で紹介している演算アルゴリズムよりも高効率なものは存在するようですが、今回は割愛します。</p>
<h3>OR演算処理の概要</h3>
<p><a href="http://www.yukun.info/wp-content/uploads/union.gif"><img src="http://www.yukun.info/wp-content/uploads/union-e1269626394691.gif" alt="OR演算結果" title="union" width="400" height="106" class="size-full wp-image-1477" /></a></p>
<p>上の図から、ある2つの語の転置インデックスリストをA, Bとします。ここで、リスト要素をそれぞれa, b(整数)とし演算結果を格納するリストをCとするとき、OR演算は主に以下の処理内容を繰り返します。</p>
<ol>
<li>if a < b then 要素aをCの末尾に追加し、aにリストAの次の要素を代入</li>
<li>if a = b then 要素aをCの末尾に追加し、A, Bが指す次の要素をa, bに代入</li>
<li>if a > b then 要素bをCの末尾に追加し、bにリストBの次の要素を代入</li>
</ol>
<h3>ソースコード</h3>
<p>今回はOR演算処理を行う部分(メソッド)のみを示します。後で示す実行結果は、<a href="http://www.yukun.info/blog/2008/05/java-intersection.html" title="検索エンジンを実装 (4)AND演算">前回</a>ブログラムをベースにintersect(postsSet)の箇所を今回のものに置き換えたものです。</p>
<pre>
import java.util.ArrayList;
import java.util.Collections;
/**
 * 検索エンジンのOR演算
 */
public class BooleanRetrieval {
  /**
   * OR演算処理
   * @param postsSet 全ての検索語の転置インデックスリスト
   * @return 演算後の転置インデックスリスト
   */
  public static ArrayList&lt;integer&gt; union(ArrayList&lt;arrayList&lt;integer&gt;&gt; postsSet) {
    ArrayList&lt;integer&gt; result; // 最終演算結果
    if (postsSet == null) return null;
    int len = postsSet.size();
    if (len == 0) return null;
    else if (len == 1) return postsSet.get(0);
    result = postsSet.get(0);
    for (int i = 1; i &lt; len; i++) {
      result = union(result, postsSet.get(i));
    }
    return result;
  }

  public static ArrayList&lt;integer&gt; union(ArrayList&lt;integer&gt; p1, ArrayList&lt;integer&gt; p2) {
    ArrayList&lt;integer&gt; answer = new ArrayList&lt;integer&gt;(); // 2語の演算結果
    int len1 = p1.size();
    int len2 = p2.size();
    int i=0, j=0;
    while (i&lt;len1 &amp;&amp; j&lt;len2) {
      int diff = p1.get(i) - p2.get(j);
      if (diff == 0) {
        answer.add(p1.get(i));
        i++; j++;
      } else if (diff &lt; 0) {
        answer.add(p1.get(i));
        i++;
      } else {
        answer.add(p2.get(j));
        j++;
      }
    }
    while (i&lt;len1) { answer.add(p1.get(i++)); }
    while (j&lt;len2) { answer.add(p2.get(j++)); }
    return answer;
  }
}
</pre>
<h3>OR演算の実行結果</h3>
<pre>単語          freq, docID
15           : 1, [2]
5            : 1, [2]
After        : 1, [1]
As           : 1, [1]
I            : 3, [0, 1, 2]
＜中略＞
should       : 2, [0, 1]
so           : 2, [1, 2]
some         : 1, [2]
standard     : 1, [0]
such         : 2, [1, 2]
than         : 1, [2]
that         : 1, [2]
the          : 3, [0, 1, 2]
think        : 3, [0, 1, 2]
this         : 1, [2]
time         : 2, [0, 2]
to           : 2, [0, 1]
touch        : 1, [1]
tremendously : 1, [1]
uncertainty  : 1, [1]
use          : 1, [2]
vigorous     : 1, [1]
wanna        : 1, [1]
well         : 1, [1]
what         : 1, [0]
when         : 1, [1]
where        : 1, [0]
why          : 1, [1]
with         : 1, [1]
検索語: the when
結果　:文書ID [0, 1, 2]に存在します。
検索語: should so
結果　:文書ID [0, 1, 2]に存在します。
検索語: this touch
結果　:文書ID [1, 2]に存在します。
検索語: use than
結果　:文書ID [2]に存在します。
検索語: where why
結果　:文書ID [0, 1]に存在します。
検索語: quit</pre>
<p>おー、なんだかもりあがってきましたねー。</p>
<p>話は変わりますが、最近よく実感していることの一つに、ソフトウェアの仕様が変更されると場合によっては設計を根本から変える必要があること。</p>
<p>そして、手がけているソフトウェアが今後アップグレードを繰り返していくと予測できるなら、現時点で取り組んでいる設計に費やす時間はほどほどに、ということです。OO分析設計を用いて保守性・拡張性を高めるのもいいけれど、その設計上での拡張が今後の仕様変更に耐えうるとは限りません。</p>
<p>勿論、最初の設計が肝心であることには変わりないですけれど。それでも留意。</p>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/06/java-subtraction.html" rel="bookmark" title="2008年6月4日">検索エンジンを実装 (6)NOT演算</a></li>
<li><a href="http://www.yukun.info/blog/2008/05/java-intersection.html" rel="bookmark" title="2008年5月20日">検索エンジンを実装 (4)AND演算</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram.html" rel="bookmark" title="2008年3月7日">検索エンジンを実装 (1)転置インデックス作成</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram-2.html" rel="bookmark" title="2008年3月16日">検索エンジンを実装 (2)出現位置とその文書ID</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram-3.html" rel="bookmark" title="2008年3月23日">検索エンジンを実装 (3)文書内の検索語を特定</a></li>
</ul>
<p><!-- Similar Posts took 9.528 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/05/java-union.html">検索エンジンを実装 (5)OR演算</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/05/java-union.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>検索エンジンを実装 (4)AND演算</title>
		<link>http://www.yukun.info/blog/2008/05/java-intersection.html</link>
		<comments>http://www.yukun.info/blog/2008/05/java-intersection.html#comments</comments>
		<pubDate>Mon, 19 May 2008 15:00:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Search Engine]]></category>
		<category><![CDATA[Set]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[Strip]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/19700101/java%e3%81%a7%e5%85%a8%e6%96%87%e6%a4%9c%e7%b4%a2%e3%82%a8%e3%83%b3%e3%82%b8%e3%83%b3%e3%82%92%e6%a7%8b%e7%af%89-4and%e6%bc%94%e7%ae%97-building-a-full-text-search-engine-on-java-4intersection-a</guid>
		<description><![CDATA[AND演算処理の概要 上の図から、ある2つの語の転置インデックスリストをA, Bとします。ここで、要素をそれぞれa, b(整数)とし演算結果を格納するリストをCとするとき、AND演算は主に以下の処理内容を繰り返します。  &#8230; <a href="http://www.yukun.info/blog/2008/05/java-intersection.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/05/java-intersection.html">検索エンジンを実装 (4)AND演算</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<h3>AND演算処理の概要</h3>
<p><a href="http://www.yukun.info/wp-content/uploads/intersection.gif"><img src="http://www.yukun.info/wp-content/uploads/intersection-e1269626508439.gif" alt="AND演算結果" title="intersection" width="400" height="128" class="size-full wp-image-1478" /></a></p>
<p>上の図から、ある2つの語の転置インデックスリストをA, Bとします。ここで、要素をそれぞれa, b(整数)とし演算結果を格納するリストをCとするとき、AND演算は主に以下の処理内容を繰り返します。</p>
<ol>
<li>if a < b then aの次の要素をaに代入</li>
<li>if a = b then 要素aをCの末尾に追加しA, Bが指す要素を一つ進める</li>
</ol>
<h3>プログラムの主な処理内容</h3>
<ol>
<li>検索対象テキストを単語に分割。</li>
<li>単語を転置インデックスに登録。ここで、1単語あたりに格納する情報は、その単語の出現頻度とその文書ID。転置インデックスのデータ構造はTreeMapを使用しkeyに単語、valueはIndexRecordでputします。</li>
<li>ユーザからの標準入力をパースしAND演算(Intersectメソッドで実現しています)。</li>
</ol>
<p>以下に、ソースコードと実行結果を示します。</p>
<h3>IndexRecord.java</h3>
<p>1つのトークン(単語)に対するインデックス情報（docIDリストや出現頻度情報）</p>
<pre>
import java.util.ArrayList;

/*
 * 1つのトークン(単語)に対するインデックス情報
 */
public class IndexRecord {
  // 出現文書IDリスト(通常ソートの必要あり)
  private ArrayList&lt;integer&gt; posts;
  // 出現頻度(今回は同一doc内の頻度はカウントしない)
  private int freq;

  public IndexRecord(int id) {
    posts = new ArrayList&lt;integer&gt;();
    posts.add(id);
    freq = 1;
  }

  /** docIDをリストに追加(今回は同一docIDのtermはカウントしない) */
  public void addDocID(int id) {
    if (!existDocID(id)) {
      posts.add(id); // docIDの追加
      freq++; // 出現頻度のカウントアップ
    }
  }

  /** docIDが既にリスト中に存在するか否か */
  public boolean existDocID(int id) {
    return (posts.indexOf(id) != -1) ? true : false;
  }

  public String toString() {
    String str = freq + &quot;, &quot;+ posts;
    return str;
  }

  public ArrayList&lt;integer&gt; getPosts() {
    return posts;
  }
}
</pre>
<h3>BooleanTest.java (AND演算のテストプログラム)</h3>
<pre>
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.TreeMap;

/**
 * 検索エンジンのAND演算処理
 * Web page: http://www.yukun.info/
 * license GPL
 */
public class BooleanTest {
  // 検索対象テキスト
  static String doc0 = &quot;It is meaningless only to think my long further aims idly. &quot;+
  &quot;It is important to set my aims but at the same time I should confirm my present condition. &quot;+
  &quot;Unless I set the standard where I am in any level, I&#039;ll be puzzled about what I should do from now on. It&#039;s in my case.&quot;;
  static String doc1 = &quot;Today, I enjoyed playing with friends daytime. &quot;+
  &quot;After enjoying, I got back to my daily life with an vigorous power. &quot;+
  &quot;I should think so, but why did I feel touch of uncertainty and regret? &quot;+
  &quot;I wanna enjoy myself and another tremendously during the day when I&#039;ve played. &quot;+
  &quot;Well, As well as I commit play to quality, I&#039;ll choose such kinds of play.&quot;;
  static String doc2 = &quot;I&#039;ll manage the limited time in a day. &quot;+
  &quot;I think that I divide the time into some intervals such as 5 minutes, &quot;+
  &quot;15 minutes and more than one hour and so on. I&#039;ll make use of this character of the interval.&quot;;

  public static void main(String[] args) {
    ArrayList&lt;string&gt; docIDlist = new ArrayList&lt;string&gt;();
    // 文書を格納
    docIDlist.add(doc0);
    docIDlist.add(doc1);
    docIDlist.add(doc2);
    StringTokenizer st[] = new StringTokenizer[3];
    String stripChars = &quot;.,:;?!&quot;&#039;[]{}()&quot;; // 除外文字

    // 文字列を空白で区切るよう設定
    for (int i = 0; i &lt; st.length; i++) {
      st[i] = new StringTokenizer(docIDlist.get(i), &quot; &quot;);
    }
    // 転置インデックス用のMap
    TreeMap&lt;string, IndexRecord&gt; termMap = new TreeMap&lt;string , IndexRecord&gt;();
    // 分割されたトークンを取得
    for (int i = 0; i &lt; st.length; i++) {
      // ここでのパラメータiはdocIDを指すことと同じ
      while (st[i].hasMoreTokens()) {
        // 文字列トークンの先頭・末尾の文字をフィルタリング
        // org.apache.commons.lang.StringUtilsクラスを使用
        // http://commons.apache.org/lang/
        String term = StringUtils.strip(st[i].nextToken(), stripChars);
        //System.out.println(&quot;値 : &quot; + term);
        if(termMap.containsKey(term)) {
          // 登録されているtermならdocIDの追加とカウントアップ
          IndexRecord ir = termMap.get(term);
          ir.addDocID(i);
          termMap.put(term, ir);
        } else {
          // termMapに登録されていないtermならdocIDと合わせて登録
          termMap.put(term, new IndexRecord(i));
        }

      }
    } // for loop ends

    // termMapのデバッグプリント
    System.out.println(&quot;単語          freq, docID&quot;);
    for (String part : termMap.keySet()) {
      System.out.printf(&quot;%-12s : %sn&quot;, part, termMap.get(part));
    }

    BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
    String words = &quot;&quot;;
    while (true) {
      ArrayList&lt;arrayList&lt;integer&gt;&gt; postsSet = new ArrayList&lt;arrayList&lt;integer&gt;&gt;();
      System.out.print(&quot;検索語: &quot;);
      try {
        // ユーザからの標準入力を受付
        words = br.readLine();
        if (words.equals(&quot;quit&quot;)) break;
        // 入力文字列をパース
        StringTokenizer parser = new StringTokenizer(words, &quot; &quot;);
        while (parser.hasMoreTokens()) {
          String term = StringUtils.strip(parser.nextToken(), stripChars);
          // termMapに登録されている単語か否か
          if (termMap.containsKey(term)) {
            postsSet.add(termMap.get(term).getPosts());
          } else {
            postsSet = null;
            break;
          }
        }

        // AND演算処理
        ArrayList&lt;integer&gt; result = intersect(postsSet);
        System.out.print(&quot;結果　:&quot;);
        if (result == null || result.size() == 0)
          System.out.println(&quot;文書中に存在しません。&quot;);
        else
          System.out.println(&quot;文書ID &quot;+ result +&quot;に存在します。&quot;);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  } // main() ends

  // AND演算処理メソッド
  public static ArrayList&lt;integer&gt; intersect(ArrayList&lt;arrayList&lt;integer&gt;&gt; postsSet) {
    if (postsSet == null) return null;
    int len = postsSet.size();
    if (len == 0) return null;
    else if (len == 1) return postsSet.get(0);
    // postsSetを昇順にソート(演算回数の削減)
    Collections.sort(postsSet, new FreqComparator());
    ArrayList&lt; Integer &gt; result = postsSet.get(0);
    for (int i = 1; i &lt; len; i++) {
      result = intersect(result, postsSet.get(i));
    }
    return result;
  }

  public static ArrayList&lt;integer&gt; intersect(ArrayList&lt;integer&gt; p1, ArrayList&lt;integer&gt; p2) {
    ArrayList&lt;integer&gt; answer = new ArrayList&lt;integer&gt;();
    int len1 = p1.size();
    int len2 = p2.size();
    for (int i=0, j=0; i&lt; len1 &amp;&amp; j &lt; len2; ) {
      if (p1.get(i) == p2.get(j)) {
        answer.add(p1.get(i));
        i++; j++;
      } else if (p1.get(i) &lt; p2.get(j)) {
        i++;
      } else {
        j++;
      }
    }
    return answer;
  }
}
</pre>
<h3>FreqComparator.java (ArrayList要素のソート用)</h3>
<pre>

import java.util.ArrayList;
import java.util.Comparator;

public class FreqComparator implements Comparator&lt;object&gt;{
  public int compare(Object o1, Object o2){
    return ((ArrayList&lt;integer&gt;) o1).size() - ((ArrayList&lt;integer&gt;) o2).size();
  }
}
</pre>
<h3>実行結果</h3>
<pre>単語          freq, docID
15           : 1, [2]
5            : 1, [2]
After        : 1, [1]
As           : 1, [1]
I            : 3, [0, 1, 2]

＜中略＞

set          : 1, [0]
should       : 2, [0, 1]
so           : 2, [1, 2]
some         : 1, [2]
standard     : 1, [0]
such         : 2, [1, 2]
than         : 1, [2]
that         : 1, [2]
the          : 3, [0, 1, 2]
think        : 3, [0, 1, 2]
this         : 1, [2]
time         : 2, [0, 2]
to           : 2, [0, 1]
touch        : 1, [1]

＜中略＞

検索語: think that
結果　:文書ID [2]に存在します。
検索語: wanna play to
結果　:文書ID [1]に存在します。
検索語: the java
結果　:文書中に存在しません。
検索語: should so
結果　:文書ID [1]に存在します。
検索語: time to
結果　:文書ID [0]に存在します。
検索語: well only
結果　:文書中に存在しません。
検索語: the time
結果　:文書ID [0, 2]に存在します。
検索語: quit</pre>
<p>おー、なんだか楽しくなってきましたね。</p>
<h3>文字列の区切り方</h3>
<p>今回は検索対象テキストを英文に絞ったため、テキスト中の空白文字で区切ることでトークンを抽出できました。対して、日本語テキストの場合は区切り記号等は無い為、n-gramか形態素辞書などを用いてトークンに区切ることで実現できます。日本語文の区切り方は色々ありますが、中でも簡単な方法は、文字種（英文字、記号、ひらがな、カタカナ、漢字）の違いを区切りの境界と捉える方法です。</p>
<p>余談ですが、ブラウザやエディタ等で文字の上でダブルクリックするとカーソル下の文字列が選択状態になりますが、その範囲を決定する際に上述の方法が応用されているようです。ソフトによってはトリプルクリックするとカーソル下の行全体が選択状態になります（使うと編集が楽です）。</p>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/05/java-union.html" rel="bookmark" title="2008年5月27日">検索エンジンを実装 (5)OR演算</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/java-subtraction.html" rel="bookmark" title="2008年6月4日">検索エンジンを実装 (6)NOT演算</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram.html" rel="bookmark" title="2008年3月7日">検索エンジンを実装 (1)転置インデックス作成</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram-2.html" rel="bookmark" title="2008年3月16日">検索エンジンを実装 (2)出現位置とその文書ID</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram-3.html" rel="bookmark" title="2008年3月23日">検索エンジンを実装 (3)文書内の検索語を特定</a></li>
</ul>
<p><!-- Similar Posts took 25.217 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/05/java-intersection.html">検索エンジンを実装 (4)AND演算</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/05/java-intersection.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>検索エンジンを実装 (1)転置インデックス作成</title>
		<link>http://www.yukun.info/blog/2008/03/java-ngram.html</link>
		<comments>http://www.yukun.info/blog/2008/03/java-ngram.html#comments</comments>
		<pubDate>Fri, 07 Mar 2008 12:55:53 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[N-gram]]></category>
		<category><![CDATA[Search Engine]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/20080307/java%e3%81%a7n-gram%e6%b3%95%e3%81%ab%e3%82%88%e3%82%8b%e8%bb%a2%e7%bd%ae%e3%82%a4%e3%83%b3%e3%83%87%e3%83%83%e3%82%af%e3%82%b9%e4%bd%9c%e6%88%90%e3%81%9d%e3%81%ae1</guid>
		<description><![CDATA[今回はN-gramでテキストを分解します。N-gram法とは対象の文字列を一定のN文字単位で分解し、それの出現頻度を求める方法です。これによって、検索エンジンに使われる転置インデックスを作成したいと思います。転置インデッ &#8230; <a href="http://www.yukun.info/blog/2008/03/java-ngram.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/03/java-ngram.html">検索エンジンを実装 (1)転置インデックス作成</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>今回はN-gramでテキストを分解します。N-gram法とは対象の文字列を一定のN文字単位で分解し、それの出現頻度を求める方法です。これによって、検索エンジンに使われる転置インデックスを作成したいと思います。転置インデックスの作成方法にはN-gramの他に形態素解析があります。両者の性能の長短は<a href="http://ja.wikipedia.org/wiki/%E5%85%A8%E6%96%87%E6%A4%9C%E7%B4%A2#N-Gram"><span style="font-style:italic;">全文検索 &#8211; Wikipedia</span></a>に詳しく載っています。</p>
<h3>Javaソースコード(Make2gram.java)</h3>
<p>さて、まずは文字列を2単語に切り分けるプログラムを作成しました。<span style="color:#666666;">データ構造は単純にArrayListで、出現頻度も求めていません。</span></p>
<pre>
import java.io.*;
import java.util.*;
/**
 * N-gram法
 */
public class Make2gram {
  public static void main(String[] args) {
    final short nsepa = 1; // 2gram
    String line;
    ArrayList&lt;stringBuffer&gt; filelist = new ArrayList&lt;stringBuffer&gt;();
    ArrayList&lt;stringBuffer&gt; bigram = new ArrayList&lt;stringBuffer&gt;();

    if (args.length &lt; 1) { // コマンドライン引数の数
      System.out.println(&quot;How to use: java Make2gram [filename]&quot;);
      System.exit(1);
    }
    try {
      BufferedReader br = new BufferedReader(new FileReader(args[0]));
      while ((line = br.readLine()) != null) {
        //System.out.println(line);
        filelist.add(new StringBuffer(line)); // 入力ファイルは一行=一要素として格納
      }
      br.close();
    }
    catch (Exception e) {
      System.err.println(&quot;[main] : &quot; + e.toString());
    }
    for (Iterator it = filelist.iterator(); it.hasNext(); ) {
      StringBuffer str = (StringBuffer) it.next();
      int lm = str.length() - nsepa;
      for (int i = 0; i &lt; lm; i++) {
        StringBuffer bi = new StringBuffer(4); // 4Byte(2文字)分の容量(Javaの内部文字コードはUnicode)
        bi.append(str.charAt(i)); // append():文字列の末尾に追加
        bi.append(str.charAt(i+1));
        bigram.add(bi);
        //System.out.print(str.charAt(i));
        //System.out.println(str.charAt(i+1));
      }
    }
    // 2-gramを表示
    for (Iterator it = bigram.iterator(); it.hasNext(); ) {
      System.out.println(it.next());
    }
  }
}
</pre>
<h3>入力ファイル(text.txt)</h3>
<pre>検索された文書は「更新順」「ファイル名順」「文書のタイトル順」などにソートされる。
一般的な検索エンジンでは独自のランク付けルールも適用し「重要度」などと呼んでいるものもある。
</pre>
<h3>実行結果</h3>
<pre>検索
索さ
され
れた
た文
文書
書は
は「
「更
更新
新順
順」
」「

…＜省略＞…</pre>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram-2.html" rel="bookmark" title="2008年3月16日">検索エンジンを実装 (2)出現位置とその文書ID</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/java-ngram-3.html" rel="bookmark" title="2008年3月23日">検索エンジンを実装 (3)文書内の検索語を特定</a></li>
<li><a href="http://www.yukun.info/blog/2008/05/java-intersection.html" rel="bookmark" title="2008年5月20日">検索エンジンを実装 (4)AND演算</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/java-subtraction.html" rel="bookmark" title="2008年6月4日">検索エンジンを実装 (6)NOT演算</a></li>
<li><a href="http://www.yukun.info/blog/2008/05/java-union.html" rel="bookmark" title="2008年5月27日">検索エンジンを実装 (5)OR演算</a></li>
</ul>
<p><!-- Similar Posts took 9.970 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/03/java-ngram.html">検索エンジンを実装 (1)転置インデックス作成</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/03/java-ngram.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

