<?php

// Defineとsql.class.phpは呼出し元のファイルで呼び出してください

function WordGroupSearch($word){

	// 与えられた単語に対して、単語の候補を検索する
	// 返り値は単語の候補一覧（配列）

	$SQL = new DB();

	$kouhoArray = array();
	$kouhoidArray = array();
	$wordid = 0;

	// 指定された単語のIDを抽出
	$Query = "select wordid from original_word where word = '".$word."' and delflag = 0";
	$Column = $SQL->QueryEx($Query);
	if ( $Column ){
		$wordid = $Column["wordid"];
	}

	// 指定単語の類義語を抽出する
	$Query = "select wordid,kouhoid from original_kouho where ( wordid = ".$wordid." or kouhoid = ".$wordid." ) and delflag = 0";
	$Column = $SQL->QueryEx($Query);
	while ( $Column ){
		$targetid = -1;
		if ( $Column["wordid"] == $wordid ){
			$targetid = $Column["kouhoid"];
		}else if ( $Column["kouhoid"] == $wordid ){
			$targetid = $Column["wordid"];
		}else{
			;
		}
		$kouhoidArray[] = $targetid;
		$Column = $SQL->QueryEx();
	}

	// 抽出した類義語IDの単語を抽出する
	for ( $i=0; $i<count($kouhoidArray); $i++ ){
		$Query = "select word from original_word where wordid = ".$kouhoidArray[$i]." and delflag = 0";
		$Column = $SQL->QueryEx($Query);
		if ( $Column ){
			$kouhoArray[] = $Column["word"];
		}
	}

	$SQL->Close();

	return $kouhoArray;

	/*
	$inputWordidArray = array();
	$inputSynsetArray = array();
	$outputWordidArray = array();
	$outputWordArray = array();

	$Query = "select wordid from word where lemma = '".$word."' and lang = 'jpn';";
	$Column = $SQL->QueryEx($Query);
	while ( $Column ){
		$inputWordidArray[] = $Column["wordid"];
		$Column = $SQL->QueryEx();
	}

	for ( $j=0; $j<count($inputWordidArray); $j++ ){
		$Query = "select synset from sense where wordid = ".$inputWordidArray[$j];
		$Column = $SQL->QueryEx($Query);
		while ( $Column ){
			$inputSynsetArray[] = $Column["synset"];
			$Column = $SQL->QueryEx();
		}
	}

	for ( $j=0; $j<count($inputSynsetArray); $j++ ){
		$Query = "select wordid from sense where synset = '".$inputSynsetArray[$j]."'";
		$Column = $SQL->QueryEx($Query);
		while ( $Column ){
			$outputWordidArray[] = $Column["wordid"];
			$Column = $SQL->QueryEx();
		}
	}

	for ( $j=0; $j<count($outputWordidArray); $j++ ){
		$Query = "select lemma from word where wordid = ".$outputWordidArray[$j]." and lang = 'jpn';";
		$Column = $SQL->QueryEx($Query);
		while ( $Column ){
			$outputWordArray[] = $Column["lemma"];
			$Column = $SQL->QueryEx();
		}
	}

	$SQL->Close();

	return $outputWordArray;
	*/
}

function GetWordId($word){
	// 指定された単語のWordIDを抽出する
	$SQL = new DB();
	$Query = "select wordid from original_word where word = '".$word."' and delflag = 0";
	$Column =  $SQL->QueryEx($Query);
	$wordid = 0;
	if ( $Column ){
		$wordid = $Column["wordid"];
	}
	$SQL->Close();
	return $wordid;
}

function RegisterWord($word){

	// original_wordに単語を登録する
	// 登録後は、単語に割り振りした単語IDを返す
	$SQL = new DB();
	$Query = "insert into original_word (word,delflag) values ('".$word."',0)";
	$SQL->Query($Query);
	$Query = "select wordid from original_word where word = '".$word."' and delflag = 0";
	$Column =  $SQL->QueryEx($Query);
	$wordid = $Column["wordid"];
	$SQL->Close();

	return $wordid;
}

function RegisterKouho($wordid,$kouhoid){
	$SQL = new DB();
	if ( $wordid < $kouhoid){
		$Query = "insert into original_kouho (wordid,kouhoid,delflag) values (".$wordid.",".$kouhoid.",0)";
	}else{
		$Query = "insert into original_kouho (wordid,kouhoid,delflag) values (".$kouhoid.",".$wordid.",0)";
	}
	$SQL->Query($Query);
	$SQL->Close();
}

function DeleteKouho($wordid,$kouhoid){
	$SQL = new DB();
	if ( $wordid < $kouhoid){
		// $Query = "update original_kouho set delflag = 1 where wordid = ".$wordid." and kouhoid = ".$kouhoid;
		$Query = "delete from original_kouho where wordid = ".$wordid." and kouhoid = ".$kouhoid;
	}else{
		// $Query = "update original_kouho set delflag = 1 where wordid = ".$kouhoid." and kouhoid = ".$wordid;
		$Query = "delete from original_kouho where wordid = ".$kouhoid." and kouhoid = ".$wordid;
	}
	$SQL->Query($Query);
	$SQL->Close();
}

function DeleteWord($wordid){
	// 単語一覧から削除する
	$SQL = new DB();
	$Query = "delete from original_word where wordid = ".$wordid;
	$SQL->Query($Query);
	$SQL->Close();
}

function DeleteKouhoAll($wordid){

	//　候補をすべて削除する
	$SQL = new DB();
	$kouhoidArray = array();

	// 指定単語の類義語を抽出する
	$Query = "select wordid,kouhoid from original_kouho where ( wordid = ".$wordid." or kouhoid = ".$wordid." ) and delflag = 0";
	$Column = $SQL->QueryEx($Query);
	while ( $Column ){
		$targetid = -1;
		if ( $Column["wordid"] == $wordid ){
			$targetid = $Column["kouhoid"];
		}else if ( $Column["kouhoid"] == $wordid ){
			$targetid = $Column["wordid"];
		}else{
			;
		}
		$kouhoidArray[] = $targetid;
		$Column = $SQL->QueryEx();
	}

	// 候補の削除
	$Query = "delete from original_kouho where wordid = ".$wordid." or kouhoid = ".$wordid;
	$SQL->Query($Query);
	$SQL->Close();

	// 抽出した類義語IDの単語を抽出する
	for ( $i=0; $i<count($kouhoidArray); $i++ ){
		$SQL = new DB();
		$Query = "select count(*) as count from original_kouho where wordid = ".$kouhoidArray[$i]." or kouhoid = ".$kouhoidArray[$i];
		$Column = $SQL->QueryEx($Query);
		$SQL->Close();
		if ( $Column[0] == 0 ){
			DeleteWord($kouhoidArray[$i]);
		}
	}

}
?>