Detection of language with classification
From Artificial Neural Network for PHP
FAQ
For information about dat-files have a view to the FAQ page.
Training
require_once '../ANN/Loader.php';
try
{
$objNetwork = ANN_Network::loadFromFile('strings.dat');
}
catch(Exception $e)
{
print 'Creating a new one...';
$objClassification = new ANN_Classification(2); // As of ANN 2.1.2
$objClassification->addClassifier('german');
$objClassification->addClassifier('english');
$objClassification->saveToFile('classifiers_strings.dat');
$objNetwork = new ANN_Network(1, 8, 2);
$objStringValues = new ANN_StringValue(15); // As of ANN 2.1.1
$objStringValues->saveToFile('input_strings.dat');
$objValues = new ANN_Values;
$objValues->train()
->input($objStringValues->getInputValue('Hallo Welt'))
->output($objClassification->getOutputValue('german'))
->input($objStringValues->getInputValue('Hello World'))
->output($objClassification('english')); // As of PHP 5.3.0
$objValues->saveToFile('values_strings.dat');
unset($objValues);
}
try
{
$objValues = ANN_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';
try
{
$objNetwork = ANN_Network::loadFromFile('strings.dat');
}
catch(Exception $e)
{
print 'Network cannot be loaded';
}
try
{
$objValues = ANN_Values::loadFromFile('values_strings.dat');
}
catch(Exception $e)
{
die('Loading of values failed');
}
try
{
$objStringValues = ANN_StringValue::loadFromFile('input_strings.dat');
}
catch(Exception $e)
{
die('Loading of input values failed');
}
try
{
$objClassification = ANN_Classification::loadFromFile('classifiers_strings.dat');
}
catch(Exception $e)
{
die('Loading of classification 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();
$arrOutputs = $objNetwork->getOutputs();
foreach($arrOutputs as $arrOutput)
print_r($objClassification->getRealOutputValue($arrOutput));