# TF取得基本テスト
# TFは、Term Frequencyの略で、term(単語または文字列）の文書内での出現頻度を意味する。

Begin;
Initialize;
# テスト用のDBを作る
InitializeSession "";
Command "create database TESTDB";
TerminateSession;

InitializeSession "TESTDB";

#１−① 　word indexで次のようなデータベースを作成し、
#		create table doc ( d_text ntext);
Command  "create table T1( C1 ntext , L language)";
Command "create fulltext index FTIndex on T1(C1) language column L
	hint 'delayed, inverted=(normalized=true, indexing=word,
	tokenizer=DUAL:JAP:ALL:1 @NORMRSCID:1 @UNARSCID:1)'";

#   次のデータを追加する。   
#"good bye,world"
#"hello,world"
#"good evening,world"
#"world,world"
#"world,world,world"
#"bye,bye"
#"bye,bye,world"

Command  "insert into T1 (C1) values (?)" ["good bye,world"];
Command  "insert into T1 (C1) values (?)" ["hello,world"];
Command  "insert into T1 (C1) values (?)" ["good evening,world"];
Command  "insert into T1 (C1) values (?)" ["world,world"];
Command  "insert into T1 (C1) values (?)" ["world,world,world"];
Command  "insert into T1 (C1) values (?)" ["bye,bye"];
Command  "insert into T1 (C1) values (?)" ["bye,bye,world"];

#############################################################################################
#次のSQL文を実行し、結果が正しいかどうか確認する。
#SQL>  select TF(d_text)  from doc where d_text contains  wordlist('bye','hello');
#{Tf(d_text)}
#{{1,0}}
#{{0,1}}
#{{2,0}}
#{{2,0}}
Command  "select TF(C1)  from T1 where C1 contains  wordlist('bye','hello')";

##############################################################################################
#SQL>  select rowid,TF(d_text)  from doc where d_text contains  wordlist('bye','hello');
#{ROWID,Tf(d_text)}
#{0,{1,0}}
#{1,{0,1}}
#{5,{2,0}}
#{6,{2,0}}
Command  "select rowid,TF(C1)  from T1 where C1 contains  wordlist('bye','hello')";

#############################################################################################
#SQL>  select rowid,TF(d_text)  from doc where d_text contains  wordlist('good','hello');
#{ROWID,Tf(d_text)}
#{0,{1,0}}
#{1,{0,1}}
#{2,{1,0}}
Command  "select rowid,TF(C1)  from T1 where C1 contains  wordlist('good','hello')";

##############################################################################################
#SQL>  select rowid,TF(d_text)  from doc where d_text contains  wordlist('good','world');
#{ROWID,Tf(d_text)}
#{0,{1,1}}
#{1,{0,1}}
#{2,{1,1}}
#{3,{0,2}}
#{4,{0,3}}
#{6,{0,1}}
Command  "select rowid,TF(C1)  from T1 where C1 contains  wordlist('good','world')";

##################################################################################################
#SQL>  select rowid,TF(d_text)  from doc where d_text contains  wordlist('good','world','evening');
#{ROWID,Tf(d_text)}
#{0,{1,1,0}}
#{1,{0,1,0}}
#{2,{1,1,1}}
#{3,{0,2,0}}
#{4,{0,3,0}}
#{6,{0,1,0}}
Command  "select rowid,TF(C1)  from T1 where C1 contains  wordlist('good','world','evening')";

###########################################################################################################
#SQL>  select rowid,TF(d_text)  from doc where d_text contains  wordlist('hello','good','world','evening');
#{ROWID,Tf(d_text)}
#{0,{0,1,1,0}}
#{1,{1,0,1,0}}
#{2,{0,1,1,1}}
#{3,{0,0,2,0}}
#{4,{0,0,3,0}}
#{6,{0,0,1,0}}
Command  "select rowid,TF(C1)  from T1 where C1 contains  wordlist('hello','good','world','evening')";

###########################################################################################################
#SQL>  select rowid,TF(d_text)  from doc where d_text contains  wordlist('bye');
#{ROWID,Tf(d_text)}
#{0,{1}}
#{5,{2}}
#{6,{2}}
#SQL>  select rowid,TF(d_text)  from doc where d_text contains  wordlist('abc');
#{ROWID,Tf(d_text)}
Command  "select rowid,TF(C1)  from T1 where C1 contains  wordlist('bye')";


#１−② rowid,score値によるソート（昇順、降順）した場合、TFの表示が各rowidに対応して表示されるかを確認
Command "select ROWID, C1 from T1";
Command "select ROWID, C1 from T1 order by ROWID desc";

Command "select(ROWID, score(C1)) from T1 where C1 like ('%good%') order by C1 desc";
Command "select(ROWID, score(C1)) from T1 where C1 like ('%good%') order by C1 asc";
Command "select(ROWID, score(C1)) from T1 where C1 like ('%world%') order by C1 desc";
Command "select(ROWID, score(C1)) from T1 where C1 like ('%world%') order by C1 asc";
Command "select(ROWID, score(C1)) from T1 where C1 like ('%bye%') order by C1 desc";
Command "select(ROWID, score(C1)) from T1 where C1 like ('%bye%') order by C1 asc";

#ngram INDEXを削除
Command  "drop index FTIndex";

# 全文ファイルテスト用の表を消去
Command "drop table T1";

TerminateSession;
Terminate;
End;
