Location: PHPKode > projects > OpenBizMap > openbizmap/mydata/script/sys_ai.txt
<?php
/*
OpenDataBag - Data Web Interface
Copyright (C) 2004 Nawara

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

$sentence_array[]="Badacze odkryli błąd w technologii produkcji mikroprocesorów.";
$sentence_array[]="Pozwala on cyberprzestępcom na szpiegowanie użytkownika komputera.";
$sentence_array[]='Zespół pod kierunkiem niemieckiego kryptologa Jean-Pierre Seiferta stwierdził, że dzisiejsze układy scalone są bardziej narażone na atak, gdyż technologię ich produkcji zmieniono tak, by były jak najbardziej wydajne.';

$tag_array=array();

$idiom_array=search('sysGroupBy=reminder');
$tmp_kk=0;
reset($idiom_array);
foreach($idiom_array as $tmp_idiom_line)
{
  $idiom_priority=0+read($tmp_idiom_line,'Priority');
  $tmp_idiom_array[$idiom_priority.'_'.$tmp_kk]=$tmp_idiom_line;
  $tmp_kk++;
}
$idiom_array=$tmp_idiom_array;

ksort($idiom_array);

function build_tree($sentence,$idiom_array)
{
  $sentence_tree=$sentence;
  
  reset($idiom_array);
  foreach($idiom_array as $tmp_idiom_line)
  {
    $mid_pos=stripos('_ '.$sentence.' ',' '.read($tmp_idiom_line,'Part2').' ')-1;
    if($mid_pos>-1)
    {
      $sentence_tree=array();
      $sentence_tree['Priority']=read($tmp_idiom_line,'Priority');
      $sentence_tree['Part1']=read($tmp_idiom_line,'Part1');
      $sentence_tree['Fill1']=build_tree(trim(substr($sentence,0,$mid_pos)),$idiom_array);
      $sentence_tree['Part2']=read($tmp_idiom_line,'Part2');
      $sentence_tree['Fill2']=read($tmp_idiom_line,'Part2');
      $sentence_tree['Part3']=read($tmp_idiom_line,'Part3');
      $sentence_tree['Fill3']=build_tree(trim(substr($sentence,$mid_pos+strlen(read($tmp_idiom_line,'Part2')))),$idiom_array);
      break;
    }
  }
  
  return $sentence_tree;
}

function reduce_tree($sentence_tree)
{
  if(is_array($sentence_tree))
  {
    $sentence_tree['Fill1']=reduce_tree($sentence_tree['Fill1']);
    $sentence_tree['Fill3']=reduce_tree($sentence_tree['Fill3']);
    
    if(is_array($sentence_tree['Fill1']) and !is_array($sentence_tree['Fill1']['Fill1']) and $sentence_tree['Part1']==$sentence_tree['Fill1']['Part1'])
      $sentence_tree['Fill1']=$sentence_tree['Fill1']['Fill1'];
    
    if(is_array($sentence_tree['Fill3']) and !is_array($sentence_tree['Fill3']['Fill3']) and $sentence_tree['Part3']==$sentence_tree['Fill3']['Part3'])
      $sentence_tree['Fill3']=$sentence_tree['Fill3']['Fill3'];

    if(is_array($sentence_tree['Fill1']) and !is_array($sentence_tree['Fill1']['Fill3']) and $sentence_tree['Part1']==$sentence_tree['Fill1']['Part3'])
      $sentence_tree['Fill1']=$sentence_tree['Fill1']['Fill3'];

    if(is_array($sentence_tree['Fill3']) and !is_array($sentence_tree['Fill3']['Fill1']) and $sentence_tree['Part3']==$sentence_tree['Fill3']['Part1'])
      $sentence_tree['Fill3']=$sentence_tree['Fill3']['Fill1'];
  }
  return $sentence_tree;
}

function save_tree($sentence_tree)
{
  global $tag_array;
  
  if(is_array($sentence_tree))
  {
    $sentence_tree['Fill1']=save_tree($sentence_tree['Fill1']);
    $sentence_tree['Fill3']=save_tree($sentence_tree['Fill3']);
    $sentence_tree['MyBag']='reminder';
    $sentence_tree['sysGroupBy']='reminder';

    $search_line='';
    reset($sentence_tree);
    foreach($sentence_tree as $sentence_key => $sentence_value)
    {
      if(strlen(trim($sentence_value)) and $sentence_key!='MyBag')
        $search_line.='['.$sentence_key.'='.$sentence_value.'] ';
    }

    $existing_sentence_array=search($search_line);
    if(count($existing_sentence_array)==0)
    {
      $sentence_tree['Count']=1;
      save($sentence_tree);

      if($sentence_tree['Fill1']!='' and $sentence_tree['Fill2']!='' and $sentence_tree['Fill3']!='')
        echo 'NEW: '.$sentence_tree['Fill1'].' '.$sentence_tree['Fill2'].' '.$sentence_tree['Fill3'].CRLF;
      else
        echo '------ NEW: '.$sentence_tree['Fill1'].' '.$sentence_tree['Fill2'].' '.$sentence_tree['Fill3'].CRLF;
      //print_r($sentence_tree); 
    }
    else
    {
      $sentence_tree=line2array($existing_sentence_array[0]);
      $sentence_tree['Count']++;

      $tmp_tag_array=explode(' ',$sentence_tree['Tags']);
      reset($tmp_tag_array);
      foreach($tmp_tag_array as $tmp_tag)
      {
        $tag_array[$tmp_tag]++;
      }
      
      save($sentence_tree);

      if($sentence_tree['Fill1']!='' and $sentence_tree['Fill2']!='' and $sentence_tree['Fill3']!='')
        echo 'OLD: '.$sentence_tree['Fill1'].' '.$sentence_tree['Fill2'].' '.$sentence_tree['Fill3'].CRLF;
      else
        echo '------ OLD: '.$sentence_tree['Fill1'].' '.$sentence_tree['Fill2'].' '.$sentence_tree['Fill3'].CRLF;
      //print_r($sentence_tree); 
    }

    $sentence_tree='';
  }
  return $sentence_tree;
}

function heard($sentence)
{
  global $tag_array;
  global $idiom_array;
  
  $answer='';
  
  $sentence=trim($sentence);
  $sentence_words_array=explode(' ',$sentence);
  $sentence='';

  reset($sentence_words_array);
  foreach($sentence_words_array as $tmp_word)
  {
    if(trim($tmp_word))
    {
      $sentence=$sentence.' '.trim($tmp_word);
      if(strpos('_.?!',substr($tmp_word,-1))>0)
      {
        $known_words_array=search('Word='.$tmp_word.'$ mybag=wrds sysGroupBy=wrds');
        if(count($known_words_array)==0)
        {
          if(strlen($sentence)>3)
          {
            echo '<pre>';
            echo $sentence.CRLF;
          
            $tmp_tree=build_tree($sentence,$idiom_array);
            print_r($tmp_tree);

            $tag_array=array();
            save_tree($tmp_tree);

            $tmp_tree=reduce_tree($tmp_tree);
            //print_r($tmp_tree);
            
            save_tree($tmp_tree);
            arsort($tag_array);
            print_r($tag_array);
            
            echo '</pre>';
            //$answer.=trim(heard_sentence(trim($sentence))).' ';
          }
          $sentence='';
        }
      }
    }
  }
  return(trim($answer));
}


// ----------------------------------------------------------------------


$chat=get('chat');
$chatid=get('chatid');
$trans=get('trans');

if(!$chatid)
  $chatid=time();

$filesize=filesize(cfg_data_path.'/temp/chat/chat_'.$chatid.'.txt');
if(strlen($chat) and $trans==$filesize)
{
  $chat_state_array=$session_array['chat state'];
  
  addpath(cfg_data_path.'/temp/chat');
  $fp=fopen(cfg_data_path.'/temp/chat/chat_'.$chatid.'.txt','ab');
  fwrite($fp,date('Y-m-d H:i').' '.USERNAME.': '.$chat.CRLF);
  fclose($fp);

  //----------------------------------------------------------------
  $answer=heard($chat);
  
  if($answer)
  {
    $fp=fopen(cfg_data_path.'/temp/chat/chat_'.$chatid.'.txt','ab');
    fwrite($fp,date('Y-m-d H:i').' ODBAI: '.$answer.CRLF);
    fclose($fp);
  }
  //----------------------------------------------------------------

  
  clearstatcache();
  $filesize=filesize(cfg_data_path.'/temp/chat/chat_'.$chatid.'.txt');
  $session_array['chat state']=$chat_state_array;
}


//--------------------------------------------------------------------------------

$content=substr(file_get_contents(cfg_data_path.'/temp/chat/chat_'.$chatid.'.txt'),-300);
$content=substr($content,strpos($content,CRLF));
echo nl2br($content);

echo '<form id="chatform" name="chatform" action="index.php">';
echo '<input type="hidden" name="odb_sys" value="ai" />';
echo '<input type="hidden" name="odb_trans" value="'.$filesize.'" />';
echo '<input type="hidden" name="odb_chatid" value="'.$chatid.'" />';
echo '<textarea style="width:50%; height:10em; border:#000 1px solid;" id="odb_chat" name="odb_chat" ></textarea>';
echo '<br />';
echo '<input class="button" type="submit" value="Say" />';
echo '</form>';

echo '<script>document.chatform.odb_chat.focus();</script>';

echo '<br />';

//echo '<pre>';
//print_r($chat_state_array);
//echo '</pre>';




// --------------------- PARKING -----------------------------------------

/*
function heard_sentence($sentence)
{
  $answer='';
  
  // cutting -----------------------------------------------
  $cut_array=array();

  $sentence=trim($sentence);
  if(strpos('_.?!',substr($sentence,-1))>0)
    $sentence=substr($sentence,0,-1);
  $sentence=trim($sentence);
    
  $sentence_words_array=explode(' ',$sentence);
  $sentence='';
  reset($sentence_words_array);
  foreach($sentence_words_array as $tmp_word)
  {
    if(trim($tmp_word))
    {
      $sentence=$sentence.' '.trim($tmp_word);
      $known_words_array=search('Word='.$tmp_word.'$ mybag=wrds sysGroupBy=wrds');
      if(count($known_words_array)==0)
      {
        $new_word_array=array();
        $new_word_array['MyBag']='wrds';
        $new_word_array['sysGroupBy']='wrds';
        $new_word_array['Word']=$tmp_word;
        save($new_word_array);
        unset($new_word_array);
      }
      else
      {
        $tmp_line=$known_words_array[0];
    
        $tmp_level=read($tmp_line,'Level');
        if(strlen($tmp_level)>1)
        {
          $tmp_level=0+substr($tmp_level,1,1);
          if($tmp_level>0)
            $cut_array[$tmp_word]=$tmp_level;
        }
      }
    }
  }
  $sentence=trim($sentence);
  
  $knowledge_array[0]['text']=$sentence;
  $knowledge_array[0]['level']=0;
  
  reset($cut_array);
  foreach($cut_array as $tmp_cut_string => $tmp_cut_level)
  {
    $tmp_cut_string=' '.$tmp_cut_string.' ';
  
    $tmp_knowledge_array=$knowledge_array;
    $knowledge_array=array();
    $tmp_kk=0;
  
    reset($tmp_knowledge_array);
    foreach($tmp_knowledge_array as $tmp_phrase_array)
    {
      $tmp_phrase=' '.trim($tmp_phrase_array['text']).' ';
      $knowledge_array[$tmp_kk]['level']=$tmp_phrase_array['level'];

      $cut_place=stripos('_'.$tmp_phrase,$tmp_cut_string);
      if($cut_place>0)
      {
        $tmp_string=trim(substr($tmp_phrase,0,$cut_place-1));
        if(strlen($tmp_string))
        {
          $knowledge_array[$tmp_kk]['text']=$tmp_string;
          $tmp_kk++;
        }
        elseif($tmp_kk==0)
        {
          $knowledge_array[$tmp_kk]['text']='_ME_';
          $tmp_kk++;
        }
        
        $tmp_string=trim(substr($tmp_phrase,$cut_place-1));
        if(strlen($tmp_string))
        {
          $knowledge_array[$tmp_kk]['text']=$tmp_string;
          $knowledge_array[$tmp_kk]['level']=$tmp_cut_level;
          $tmp_kk++;
        }
      }
      else
      {
        $knowledge_array[$tmp_kk]['text']=trim($tmp_phrase);
        $tmp_kk++;
      }
    }
  }
  
  
  // building simple sentences
  echo '<pre>';
  for($tmp_mm=count($knowledge_array)-1;$tmp_mm>=0;$tmp_mm--)
  {
    $sentence='';
    $level_to_be_found=$knowledge_array[$tmp_mm]['level'];
    if($level_to_be_found>1)
    {
      $good_sentence=0;
      for($tmp_nn=$tmp_mm;$tmp_nn>=0;$tmp_nn--)
      {
        if($level_to_be_found>=$knowledge_array[$tmp_nn]['level'])
        {
          if($knowledge_array[$tmp_nn]['level']==1 or $knowledge_array[$tmp_nn]['level']==2)
            $good_sentence=1;
            
          $sentence=$knowledge_array[$tmp_nn]['text'].' '.$sentence;
          $level_to_be_found=$knowledge_array[$tmp_nn]['level']-1;
        }
      }

      if($good_sentence==1)
      {
        echo $sentence.CRLF;
        
        //saving sentence
        $known_sentences_array=search('Sentence='.$sentence.'$ mybag=snts sysGroupBy=snts');
        if(count($known_sentences_array)==0)
        {
          $new_sentence_array=array();
          $new_sentence_array['MyBag']='snts';
          $new_sentence_array['sysGroupBy']='snts';
          $new_sentence_array['Sentence']=$sentence;
          $new_sentence_array['Heard Count']=1;
          save($new_sentence_array);
          unset($new_sentence_array);
        }
        else
        {
          // FIXME only first sentence is taken
          $new_sentence_array=array();
          $new_sentence_array=line2array($known_sentences_array[0]);

          if(isset($new_sentence_array['Reaction']))
            $answer.=$new_sentence_array['Reaction'].' ';

          $new_sentence_array['Heard Count']++;
          save($new_sentence_array);
          unset($new_sentence_array);
        }
      }
    }
  }
  echo 'NOTME: '.$answer;
  print_r($knowledge_array);
  echo '</pre>';

  return(trim($answer));
}
*/

// simplifing -----------------------------------------------

// finding relations -----------------------------------------------

// show -----------------------------------------------

//echo '<pre>';
//print_r($knowledge_array);
//print_r($cut_array);
//echo '</pre>';

// trashcan
/*
$name_array=array(
'praktycznie dowolnego',
'w firmie',
);

reset($name_array);
foreach($name_array as $tmp_name)
{
  $sentence=str_replace($tmp_name,strtoupper($tmp_name),$sentence);
}

$sentence_words_array=explode(' ',$sentence);

$first_word_in_sentence=1;
$name_processing=0;
$tmp_kk=0;
reset($sentence_words_array);
foreach($sentence_words_array as $tmp_word)
{
  if(!$first_word_in_sentence)
  {
    if(!$name_processing)
    {
      if(check_ucfirst($tmp_word))
      {
        //name stars in ucfirst
        $tmp_kk++; 
        $name_processing=1;
      }
      else
      {
        //normal text continues
      }
    }
    else
    {
      if(check_ucfirst($tmp_word))
      {
        //name continues
      }
      else
      {
        $knowledge_array[$tmp_kk]='"'.$knowledge_array[$tmp_kk].'"';
        $tmp_kk++; //name ends
        $name_processing=0;
      }
    }
  }
  $first_word_in_sentence=0;

  $knowledge_array[$tmp_kk]=trim($knowledge_array[$tmp_kk].' '.$tmp_word);
}
*/

/*
$chat=get('chat');
$chatid=get('chatid');
$trans=get('trans');

if(!$chatid)
  $chatid=time();

$filesize=filesize(cfg_data_path.'/temp/chat/chat_'.$chatid.'.txt');
if(strlen($chat) and $trans==$filesize)
{
  $chat_state_array=$session_array['chat state'];
  
  addpath(cfg_data_path.'/temp/chat');
  $fp=fopen(cfg_data_path.'/temp/chat/chat_'.$chatid.'.txt','ab');
  fwrite($fp,date('Y-m-d H:i').' '.USERNAME.': '.$chat.CRLF);
  fclose($fp);

  //----------------------------------------------------------------
  $answer='';
  $tmp_search_result=search($chat.' -???');
  if(count($tmp_search_result)==0)
  {
    $variable_array['sysGroupBy']='talkbit';
    $variable_array['Sentence']=$chat;
    $variable_array['Answer']='???';
    save($variable_array);

    $chat_state_array['Last ai asked question']=$chat;

    $answer='???';
  }
  else
  {
    reset($tmp_search_result);
    foreach($tmp_search_result as $tmp_line)
    {
      $answer=read($tmp_line,'Answer');
      
      if($answer)
      {
        if($chat_state_array['Last ai asked question'])
        {
          $question_search_result=search('[Sentence='.$chat_state_array['Last ai asked question'].'] Answer=???');
          reset($question_search_result);
          foreach($question_search_result as $question_search_line)
          {
            $question_array=line2array($tmp_line);
            $question_array['Sentence']=read($question_search_line,'Sentence');
            save($question_array);
          }
          
          $chat_state_array['Last ai asked question']='';
        }

        $answer=str_replace('X',USERNAME,$answer);
        break;
      }
    }
  }
  
  if($answer)
  {
    $fp=fopen(cfg_data_path.'/temp/chat/chat_'.$chatid.'.txt','ab');
    fwrite($fp,date('Y-m-d H:i').' ODBAI: '.$answer.CRLF);
    fclose($fp);
  }
  //----------------------------------------------------------------

  
  clearstatcache();
  $filesize=filesize(cfg_data_path.'/temp/chat/chat_'.$chatid.'.txt');
  $session_array['chat state']=$chat_state_array;
}


//--------------------------------------------------------------------------------

$content=substr(file_get_contents(cfg_data_path.'/temp/chat/chat_'.$chatid.'.txt'),-300);
$content=substr($content,strpos($content,CRLF));
echo nl2br($content);

echo '<form id="chatform" name="chatform" action="index.php">';
echo '<input type="hidden" name="odb_sys" value="ai" />';
echo '<input type="hidden" name="odb_trans" value="'.$filesize.'" />';
echo '<input type="hidden" name="odb_chatid" value="'.$chatid.'" />';
echo '<input class="button" type="submit" value="Say" /><input style="width:50%; border:#000 1px solid;" id="odb_chat" name="odb_chat" />';
echo '</form>';

echo '<script>document.chatform.odb_chat.focus();</script>';

echo '<br />';

echo '<pre>';
print_r($chat_state_array);
echo '</pre>';
*/
?>
Return current item: OpenBizMap