Detection of language: Difference between revisions

From Artificial Neural Network for PHP
(New page: == FAQ == For information about dat-files have a view to the FAQ page. == Training == <source lang="php"> require_once '../ANN/Loader.php'; try { $network = ANN_Network::loadFro...)
 
 
(2 intermediate revisions by the same user not shown)
Line 8: Line 8:


require_once '../ANN/Loader.php';
require_once '../ANN/Loader.php';

use ANN\Network;
use ANN\StringValue;
use ANN\Values;


try
try
{
{
$network = ANN_Network::loadFromFile('strings.dat');
$objNetwork = Network::loadFromFile('strings.dat');
}
}
catch(Exception $e)
catch(Exception $e)
Line 17: Line 21:
print 'Creating a new one...';
print 'Creating a new one...';


$network = new ANN_Network(1, 8, 2);
$objNetwork = new Network(1, 8, 2);


$objStringValues = new ANN_StringValue(15); // As of ANN 2.1.1
$objStringValues = new StringValue(15); // As of ANN 2.1.1
$objStringValues->saveToFile('input_strings.dat');
$objStringValues->saveToFile('input_strings.dat');
$objValues = new ANN_Values;
$objValues = new Values;
$objValues->train()
$objValues->train()
Line 38: Line 42:
try
try
{
{
$objValues = ANN_Values::loadFromFile('values_strings.dat');
$objValues = Values::loadFromFile('values_strings.dat');
}
}
catch(Exception $e)
catch(Exception $e)
Line 45: Line 49:
}
}


$network->setValues($objValues);
$objNetwork->setValues($objValues);


$network->train();
$objNetwork->train();


$network->saveToFile('strings.dat');
$objNetwork->saveToFile('strings.dat');


$network->printNetwork();
$objNetwork->printNetwork();
</source>
</source>


Line 59: Line 63:


require_once '../ANN/Loader.php';
require_once '../ANN/Loader.php';

use ANN\Network;
use ANN\StringValue;
use ANN\Values;


try
try
{
{
$network = ANN_Network::loadFromFile('strings.dat');
$objNetwork = Network::loadFromFile('strings.dat');
}
}
catch(Exception $e)
catch(Exception $e)
Line 71: Line 79:
try
try
{
{
$objValues = ANN_Values::loadFromFile('values_strings.dat');
$objValues = Values::loadFromFile('values_strings.dat');
}
}
catch(Exception $e)
catch(Exception $e)
Line 80: Line 88:
try
try
{
{
$objStringValues = ANN_StringValue::loadFromFile('input_strings.dat');
$objStringValues = StringValue::loadFromFile('input_strings.dat');
}
}
catch(Exception $e)
catch(Exception $e)
Line 96: Line 104:
$objValues->input($objStringValues('Helló Wórld!')); // As of PHP 5.3.0
$objValues->input($objStringValues('Helló Wórld!')); // As of PHP 5.3.0


$network->setValues($objValues);
$objNetwork->setValues($objValues);


$network->printNetwork();
$objNetwork->printNetwork();


</source>
</source>

Latest revision as of 11:39, 1 June 2011

FAQ

For information about dat-files have a view to the FAQ page.

Training

require_once '../ANN/Loader.php';

use ANN\Network;
use ANN\StringValue;
use ANN\Values;

try
{
  $objNetwork = Network::loadFromFile('strings.dat');
}
catch(Exception $e)
{
  print 'Creating a new one...';

  $objNetwork = new Network(1, 8, 2);

  $objStringValues = new StringValue(15); // As of ANN 2.1.1
	
  $objStringValues->saveToFile('input_strings.dat');
	
  $objValues = new Values;
  
  $objValues->train()
    ->input($objStringValues->getInputValue('Hallo Welt'))
    ->output(1, 0) // German
    ->input($objStringValues->getInputValue('Hello World'))
    ->output(0, 1); // English
  
  $objValues->saveToFile('values_strings.dat');
  
  unset($objValues);
}

try
{
  $objValues = Values::loadFromFile('values_strings.dat');
}
catch(Exception $e)
{
  die('Loading of values failed');
}

$objNetwork->setValues($objValues);

$objNetwork->train();

$objNetwork->saveToFile('strings.dat');

$objNetwork->printNetwork();

Using trained network

require_once '../ANN/Loader.php';

use ANN\Network;
use ANN\StringValue;
use ANN\Values;

try
{
  $objNetwork = Network::loadFromFile('strings.dat');
}
catch(Exception $e)
{
  print 'Network cannot be loaded';
}

try
{
  $objValues = Values::loadFromFile('values_strings.dat');
}
catch(Exception $e)
{
  die('Loading of values failed');
}

try
{
  $objStringValues = StringValue::loadFromFile('input_strings.dat');
}
catch(Exception $e)
{
  die('Loading of input values failed');
}

$objValues->input($objStringValues->getInputValue('HAllo Welt'));
$objValues->input($objStringValues->getInputValue('Hello World'));
$objValues->input($objStringValues->getInputValue('Hálló Wélt'));
$objValues->input($objStringValues->getInputValue('Hélló Wórld'));
$objValues->input($objStringValues('Hßllo Welt'));   // As of PHP 5.3.0
$objValues->input($objStringValues('Hßlló Wórld'));  // As of PHP 5.3.0
$objValues->input($objStringValues('Hallo Welt!'));  // As of PHP 5.3.0
$objValues->input($objStringValues('Helló Wórld!')); // As of PHP 5.3.0

$objNetwork->setValues($objValues);

$objNetwork->printNetwork();