﻿<html>
<!-- File Name: txt_tl6.php -->
<!-- Description: This file is an interface for the Text Analysis Tool  -->
<!-- Tool returns searches of XML Documents.
<!-- This file needs companion files: Search2.xsl, an XML file , and Wordlist file  -->
<!-- Last update on 7-31-05 -->
<head>

<title>Text Analysis Tool</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

</head>

<body>

<h1 align="center">Text Analysis Tool </h1>

<!-- Description table -->
<table> 

<TR>
    <TD class=menu colspan="5" bgcolor="#C0C0C0" bordercolor="#000000">
	Tool uses UTF-8 Characters.&nbsp; If you are having trouble displaying 
    special characters,
    <a target="_blank" href="http://courses.washington.edu/otap/otap_en/info_en/fonts.shtml">
    click here</a>.&nbsp; For a list of&nbsp; diagraphs used,
    <a target="_blank" href="http://courses.washington.edu/otap/info/c_diag.html">click here</a>. 
    For tools attributes
    <a target="_blank" href="http://students.washington.edu/ratucker/attributes.htm">click here</a> .</TD>
    </TR>
</table>


<!-- Examples Section -->

<ul id="collapsibleList"> <!-- Head Of Collapsible List -->
  	<li>
    	<script type="text/javascript">document.writeln('<img id="exampImage" src="up.png" width="15" height="8" 	alt="Open examples" onClick="toggle(\'exampImage\',\'exampList\');">');</script>
   	Click on the icon to show examples
	</li>


<!-- Nested Listed -->
<ul id="exampList" style="text-indent: 0; margin-left: 0; margin-right: 0; padding: 0">

<!-- Table of Examples in Collapsible List -->
<TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>

<!--First Example-->
<TR>
    <TD colspan="5" bgcolor="#FFFFFF" bordercolor="#000000">
	Shows the 6th couplet from all poems in the Necati 
    text.
    </TD>
</TR>
<TR>
   <TD bordercolor="#000000" bgcolor="#C0C0C0">
	Search for: <SELECT name=option2>
	<OPTION value=FOLIO>Couplet</OPTION> 
	</SELECT> 
   </TD>
   <TD bordercolor="#000000" bgcolor="#C0C0C0" >
	<input type="checkbox" name="diagraph" value="ON">
	Show UTF-8
   </TD>

   <TD bordercolor="#000000" bgcolor="#C0C0C0">
  	 <INPUT name=query2 size=10 value ="6"> 	    	
   </TD>
    <TD bordercolor="#000000" bgcolor="#C0C0C0">
	in XML File:
	<SELECT name=filename3>
		<OPTION value=Necati.xml>Necati.xml</OPTION> 
	</SELECT>

    </TD>
    <TD bordercolor="#000000" bgcolor="#C0C0C0">
        <input type="submit">&nbsp;
    </TD>
</TR>
  
<!--Second Example-->
<TR>
    <TD colspan="5" bgcolor="#FFFFFF" bordercolor="#000000">
	Show all lines that have an instance of&nbsp; the word &quot;o{n=}arur&quot; 
    	in the Necati text.
    </TD>
</TR>
<TR>
   <TD bordercolor="#000000" bgcolor="#C0C0C0">
	Search for: <SELECT name=option>
	<OPTION value=X_WRD>Word (exact)</OPTION>
	</SELECT> 
   </TD>
   <TD bordercolor="#000000" bgcolor="#C0C0C0">
	<input type="checkbox" name="diagraph" value="ON">
	Show UTF-8
   </TD>

   <TD bordercolor="#000000" bgcolor="#C0C0C0">
	<INPUT name=query2 size=10 value ="o{n=}arur"  > 
   </TD>
   <TD bordercolor="#000000" bgcolor="#C0C0C0">in XML File:
	<SELECT name=filename3>
		<OPTION value=Necati.xml>Necati.xml</OPTION> 
	</SELECT>
   </TD>
   <TD bordercolor="#000000" bgcolor="#C0C0C0">
        <input type="submit">&nbsp;
   </TD>
</TR>
<TR>
   <TD bordercolor="#000000" bgcolor="#FFFFFF" colspan="5">
	Try entering these examples in the tool below
   </TD>
</TR>
</TABLE>
</ul>
</ul>
<!-- end of Example Section -->


<script type="text/javascript">
  document.getElementById('collapsibleList').style.listStyle="none"; // remove list markers
  document.getElementById('exampList').style.display="none";           // collapse list

  // this function toggles the status of a list
  function toggle(image,list){
    var listElementStyle=document.getElementById(list).style;
    if (listElementStyle.display=="none"){
      listElementStyle.display="block";
      document.getElementById(image).src="down.png";
      document.getElementById(image).alt="Close examples";
    }else{
      listElementStyle.display="none";
      document.getElementById(image).src="up.png";
      document.getElementById(image).alt="Open examples";
    }
  }
</script>

<!-- Header -->
<h2 align="center">Analysis Tool</h2>

<!-- Start of PHP code -->
<?php

//Build array for filenames in menu
$i=0;
$filenames = array();
foreach (glob("*.xml") as $file) 
{
   $filenames[]=$file;
   $i++;

}

//Display Tool Interface
echo '

<FORM id=myform action="txt_tl6.php" method="post">
<TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
  <TBODY>
  <TR>
    <TD bgcolor="#339900" class=menu>
	Search for: <SELECT name=option>
	<OPTION value=DOC selected>Whole Document</OPTION>
	<OPTION value=BKN>Book/Selection</OPTION>
	<OPTION value=FLN>Folio/Page</OPTION> 
        <OPTION value=MLN>Line</OPTION>
	<OPTION value=WRD>Word (partial)</OPTION>
	<OPTION value=B_WRD>Word (begins with)</OPTION> 
        <OPTION value=E_WRD>Word (ends with)</OPTION>
	<OPTION value=X_WRD>Word (exact)</OPTION>
	<OPTION value=WRDLIST>Word List</OPTION>
	</SELECT> 
   </TD>
    <TD bgcolor="#339900"  class=menu>
	<input type="checkbox" name="diagraph">
	Show UTF-8
   </TD>

    <TD bgcolor="#339900"  class=menu><INPUT name=query size=10 > 
	    	
   </TD>
    <TD bgcolor="#339900" class=menu>in XML File:
	<SELECT name=filename>';

//insert filenames into part of menu
for($i=0; $i < count($filenames); $i++)
{

	echo '<OPTION value=';
	echo $filenames[$i];
	if(i==0)
		echo' selected';
	echo'>';
	echo $filenames[$i];
	echo'</OPTION>';
	
	
}
	
echo '        </SELECT>

    </TD>
    <td bgcolor="#339900" class="menu">
        <input type="submit">&nbsp;
</td></TR>
  <TR>
    <TD class=outputcell id=output 
colSpan=4>&nbsp;</TD></TR></TBODY></TABLE></FORM>
</body>
</html>
';
//End Of Tool Interface


//Declare Diagrph arrays
$UTF8 = array('Ā','ā','ă','Ç','ç','Ḍ','ḍ','ė','Ğ','ğ','Ġ','ġ','Ḥ','ḥ','Ḫ','ẖ','Ī','ī','ĭ','İ','ı','Ḳ','ḳ','Ñ','ñ','Ö','ö','Ō','ō','Ş','ş','Ṣ','ṣ','S̲','s̲','Ṭ','ṭ','Ü','ü','Ū','ū','Ż','ż','Ẓ','ẓ','Ẕ','ẕ','ˀ','ʿ',' ̮','·');

$Diagraph = array('{A=}','{a=}','{a-}','{C,}','{c,}','{D!}','{d!}','{e.}','{G=}','{g=}','{G.}','{g.}','{H!}','{h!}','{H-}','{h-}','{I=}','{i=}','{i-}','{I.}','{i.}','{K!}','{k!}','{N=}','{n=}','{O:}','{o:}','{O=}','{o=}','{S,}','{s,}','{S!}','{s!}','{S-}','{s-}','{T!}','{t!}','{U:}','{u:}','{U=}','{u=}','{Z.}','{z.}','{Z!}','{z!}','{Z-}','{z-}','{*}','{@}','{--}','{..}');


// Get variables from HTML Form
$query = $_REQUEST['query'];
$option = $_REQUEST['option'];
$diagraph = $_REQUEST['diagraph'];

//must create a default for &filename to avoid empty string error when loading the xml into dom object bellow. 2007-05-17

$filename = $_REQUEST['filename'];
if(empty($filename)){
$filename = 'smNecati.xml';
}

$filename2 = 'search2.xsl';

//increase Time Limit OF PHP Functions(5 min)
set_time_limit(300);

//If Wordlist display pre-generated text
if($option == 'WRDLIST')
{
$wrdfile =  str_replace('xml','txt',$filename);
if (is_readable($wrdfile))
 {

	$lines = file($wrdfile);

	// Loop through our array
	foreach ($lines as $line_num => $line) {
	   echo "$line";
	}
       
   }

else
{
  echo ' WordList not available';
        
}



}

//IF NOT A WORD LIST
else
{



//If Search Term has UTF-8 Convert to Diagraph

	for($i=0; $i < count($UTF8); $i++)
	{ 
		If(strpos($query ,$UTF8[$i])!== False)
		{
			$query = str_replace($UTF8[$i],$Diagraph[$i],$query);
		}
	}

//Format variables for XSL stylesheet
	if($diagraph == "on")
		{
			$diagraph = "True";
		}
	if($option == 'B_WRD')
		{
			$query = " $query";
			
			$option = 'WRD';
		}
	else if($option == 'E_WRD')
		{
			$query = "$query ";

			$option = 'WRD';
		}
	else if($option == 'X_WRD')
		{
			$query = " $query ";

			$option = 'WRD';
		}

	else if($option == 'WRDLIST')
		{
			$filename2 ='wordlist.xsl';
		}	
	else if($option == 'DOC')
		{
			$filename2 ='doc.xsl';
		}	

// Load the XML document
$xml = new DOMDocument;
$xml->load($filename);

// Load the XSL stylesheet
$xsl = new DOMDocument;
$xsl->load($filename2);

// Configure the processor
$proc = new XSLTProcessor;

// Attach the xsl rules
$proc->importStyleSheet($xsl);

// Pass parameters to the XSL Stylesheet
$proc->setParameter('', 'option', $option);
$proc->setParameter('', 'query', $query);
$proc->setParameter('', 'diagraph', $diagraph);

// Output XHTML
echo $proc->transformToXML($xml);
}


//Function to make an object into an array

function object2array ( $object )
{
   if ( !is_object ( $object ) )
     return $object;

   $return = array ();

   $var = get_object_vars ( $object );

   while ( list ( $k, $v ) = each ( $var ) )
     $return [ $k ] = object2array ( $v );
   return $return;
}

//Function to flatten an array
function flattenArray($array) 
{
   $flatArray = array();
   foreach( $array as $subElement ) {
       if( is_array($subElement) )
           $flatArray = array_merge($flatArray, flattenArray($subElement));
       else
           $flatArray[] = $subElement;
   }
   return $flatArray;
} 


?>
