<?php

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

require_once('../class/define.class.php');
require_once('../class/sql.class.php');

echo time();

$start = $_POST["start"];
$end   = $_POST["end"];

echo "<br/>". $start ." - ". $end."<br/>";

for ( $i=$start; $i<$end; $i++ ){

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

	//echo $i."<br/>";

	$SQL = new DB();

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

	$Query = "select word from original_word where wordid = ".$i;
	$Column = $SQL->QueryEx($Query);
	$word = $Column["word"];
	//echo $word."<br/>";

	$Query = "select wordid from word where lemma = '".$word."'";
	//echo $Query."<br/>";
	$Column = $SQL->QueryEx($Query);
	$wordid = $Column["wordid"];
	//echo $wordid."<br/>";

	$Query = "select synset from sense where wordid = ".$wordid;
	$Column = $SQL->QueryEx($Query);
	while ( $Column ){
		$inputSynsetArray[] = $Column["synset"];
		$Column = $SQL->QueryEx();
	}
	//print_r($inputSynsetArray);

	for ( $j=0; $j<count($inputSynsetArray); $j++ ){

		$Query = "select wordid from sense where synset = '".$inputSynsetArray[$j]."'";
		//echo $Query;
		$Column = $SQL->QueryEx($Query);
		$wordidArray = array();
		while ( $Column ){
			$wordidArray[] = $Column["wordid"];
			$Column = $SQL->QueryEx();
		}
		//print_r($wordidArray);

		for ( $k=0; $k<count($wordidArray); $k++ ){

			$wordid = $wordidArray[$k];
			$Query = "select lemma from word where wordid = ".$wordid;
			$Column = $SQL->QueryEx($Query);
			$word = $Column["lemma"];
			//echo $word."<br/>";

			$Query = "select wordid from original_word where word = '".$word."'";
			$Column = $SQL->QueryEx($Query);
			if ( $Column ){
				$wordid = $Column["wordid"];
				if ( $i < $wordid ){
					$Query = "insert into original_kouho values (".$i.",".$wordid.",0);";
					//echo $Query."  ".$word."<br/>";
					$SQL->Query($Query);
				}
			}
		}

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

	for ( $j=0; $j<count($outputWordidArray); $j++ ){
		// echo $i." ".$outputWordidArray[$j]."<br/>";
		if ( $i < $outputWordidArray[$j] ){
			$Query = "insert into original_kouho values (".$i.",".$outputWordidArray[$j].",0);";
			echo $Query."<br/>";
			$SQL->Query($Query);
		}
	}
	*/
	$SQL->Close();

}

echo time();

?>