language detection example

This commit is contained in:
Arkadiusz Kondas committed 2016-07-07 00:53:38 +02:00
commit c24978693b
6 files changed
+195

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
/vendor/
humbuglog.*
/bin/phpunit
+15
View File
@@ -0,0 +1,15 @@
# PHP-ML - Machine Learning library for PHP - Examples
Examples of the use of PHP-ML library
## Examples
* LanguageDetection.php - classifier build for language detection
## License
PHP-ML is released under the MIT Licence. See the bundled LICENSE file for details.
## Author
Arkadiusz Kondas (@ArkadiuszKondas)
+17
View File
@@ -0,0 +1,17 @@
{
"name": "php-ai/php-ml-examples",
"type": "project",
"description": "Examples of the use of PHP-ML library",
"license": "MIT",
"keywords": ["machine learning","pattern recognition","computational learning theory","artificial intelligence"],
"homepage": "https://github.com/php-ai/php-ml-examples",
"authors": [
{
"name": "Arkadiusz Kondas",
"email": "arkadiusz.kondas@gmail.com"
}
],
"require": {
"php-ai/php-ml": "dev-develop"
}
}
Generated
+67
View File
@@ -0,0 +1,67 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "1342a147b788083a903bdd811cf8b1a8",
"content-hash": "073a4d8adce8080a2e60222db7a59192",
"packages": [
{
"name": "php-ai/php-ml",
"version": "dev-develop",
"source": {
"type": "git",
"url": "https://github.com/php-ai/php-ml.git",
"reference": "6c7416a9c40b596afce625617dce07892ccaa2fc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-ai/php-ml/zipball/6c7416a9c40b596afce625617dce07892ccaa2fc",
"reference": "6c7416a9c40b596afce625617dce07892ccaa2fc",
"shasum": ""
},
"require": {
"php": ">=7.0.0"
},
"require-dev": {
"phpunit/phpunit": "^5.2"
},
"type": "library",
"autoload": {
"psr-0": {
"Phpml": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Arkadiusz Kondas",
"email": "arkadiusz.kondas@gmail.com"
}
],
"description": "PHP-ML - Machine Learning library for PHP",
"homepage": "https://github.com/php-ai/php-ml",
"keywords": [
"artificial intelligence",
"computational learning theory",
"machine learning",
"pattern recognition"
],
"time": "2016-07-06 22:29:58"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {
"php-ai/php-ml": 20
},
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}
+61
View File
@@ -0,0 +1,61 @@
"sentence","language"
"Hello, do you know what time the movie is tonight?","english"
"I am calling to make reservations","english"
"I would like to know if it is at all possible to check in","english"
"What time does the swimming pool open?","english"
"Where is the games room?","english"
"We must call the police.","english"
"How many pupils are there in your school?","english"
"Twenty litres of unleaded, please.","english"
"Is it near here?","english"
"Do I have to change?","english"
"Which counter do I go to to change money?","english"
"I would like two postcards, please.","english"
"There was a big explosion.","english"
"Where is the television room?","english"
"Where's the nearest railway station?","english"
"The storms caused flooding.","english"
"Where is the duty free shop?","english"
"I witnessed it happening.","english"
"I would like two postcards, please.","english"
"How about going to the cinema?","english"
"Où est la boulangerie?","french"
"Je voudrais une boîte de chocolates.","french"
"Y a-t-il un autre hôtel près d'ici?","french"
"Vérifiez la batterie, s'il vous plaît.","french"
"La banque ouvre à quelle heure?","french"
"Est-ce que je peux l'écouter?","french"
"Vous devez faire une déclaration de perte.","french"
"Combien des élèves y a-t-il dans votre collège?","french"
"Est-ce qu-il ya un car qui va à l'aéroport?","french"
"Nous voudrions rester jusqu'à Dimanche prochain.","french"
"Je ne comprends pas.","french"
"Voici une ordonnance pour des comprimés.","french"
"Je n'ai qu'un billet de cinquante francs.","french"
"À quelle heure commence le dernière séance?","french"
"Faut-il changer?","french"
"Il n'avait pas la priorité.","french"
"Puis-je prendre les places à l'avance?","french"
"Elle a trente et un ans.","french"
"Pour aller à la pharmacie, s'il vous plaît?","french"
"Voulez-vous me peser ce colis, s'il vous plaît.","french"
"Ciao, ti va di andare al cinema?","italian"
"Mi sai dire quando aprono il negozio?","italian"
"Quando c'è l'ultimo autobus?","italian"
"Lo posso provare addosso?","italian"
"C'è una presa elettrica per il nostro caravan?","italian"
"Vorrei una scatola di cioccolatini.","italian"
"Non e' stata colpa mia.","italian"
"Sono molto scarso in fisica.","italian"
"Vorrei del vino bianco.","italian"
"Me lo puo' spiegare, per piacere.","italian"
"Vorrei chiamare a spese del ricevente.","italian"
"Ho perso il mio passaporto.","italian"
"Posso avere i biglietti del cinema per favore?","italian"
"Vorrei un pacchetto di biscotti.","italian"
"Quali materie studi (studia)?","italian"
"Quanto frequenti sono gli autobus?","italian"
"A che ora sara' la prossima raccolta?","italian"
"Vorrei prenotare un posto.","italian"
"Per favore, quanto costa spedire una lettera in Germania?","italian"
"Mio padre e' un programmatore.","italian"
1 sentence language
2 Hello, do you know what time the movie is tonight? english
3 I am calling to make reservations english
4 I would like to know if it is at all possible to check in english
5 What time does the swimming pool open? english
6 Where is the games room? english
7 We must call the police. english
8 How many pupils are there in your school? english
9 Twenty litres of unleaded, please. english
10 Is it near here? english
11 Do I have to change? english
12 Which counter do I go to to change money? english
13 I would like two postcards, please. english
14 There was a big explosion. english
15 Where is the television room? english
16 Where's the nearest railway station? english
17 The storms caused flooding. english
18 Where is the duty free shop? english
19 I witnessed it happening. english
20 I would like two postcards, please. english
21 How about going to the cinema? english
22 Où est la boulangerie? french
23 Je voudrais une boîte de chocolates. french
24 Y a-t-il un autre hôtel près d'ici? french
25 Vérifiez la batterie, s'il vous plaît. french
26 La banque ouvre à quelle heure? french
27 Est-ce que je peux l'écouter? french
28 Vous devez faire une déclaration de perte. french
29 Combien des élèves y a-t-il dans votre collège? french
30 Est-ce qu-il ya un car qui va à l'aéroport? french
31 Nous voudrions rester jusqu'à Dimanche prochain. french
32 Je ne comprends pas. french
33 Voici une ordonnance pour des comprimés. french
34 Je n'ai qu'un billet de cinquante francs. french
35 À quelle heure commence le dernière séance? french
36 Faut-il changer? french
37 Il n'avait pas la priorité. french
38 Puis-je prendre les places à l'avance? french
39 Elle a trente et un ans. french
40 Pour aller à la pharmacie, s'il vous plaît? french
41 Voulez-vous me peser ce colis, s'il vous plaît. french
42 Ciao, ti va di andare al cinema? italian
43 Mi sai dire quando aprono il negozio? italian
44 Quando c'è l'ultimo autobus? italian
45 Lo posso provare addosso? italian
46 C'è una presa elettrica per il nostro caravan? italian
47 Vorrei una scatola di cioccolatini. italian
48 Non e' stata colpa mia. italian
49 Sono molto scarso in fisica. italian
50 Vorrei del vino bianco. italian
51 Me lo puo' spiegare, per piacere. italian
52 Vorrei chiamare a spese del ricevente. italian
53 Ho perso il mio passaporto. italian
54 Posso avere i biglietti del cinema per favore? italian
55 Vorrei un pacchetto di biscotti. italian
56 Quali materie studi (studia)? italian
57 Quanto frequenti sono gli autobus? italian
58 A che ora sara' la prossima raccolta? italian
59 Vorrei prenotare un posto. italian
60 Per favore, quanto costa spedire una lettera in Germania? italian
61 Mio padre e' un programmatore. italian
+32
View File
@@ -0,0 +1,32 @@
<?php
include 'vendor/autoload.php';
use Phpml\Dataset\CsvDataset;
use Phpml\Dataset\ArrayDataset;
use Phpml\FeatureExtraction\TokenCountVectorizer;
use Phpml\Tokenization\WordTokenizer;
use Phpml\CrossValidation\RandomSplit;
use Phpml\Metric\Accuracy;
use Phpml\Classification\SVC;
use Phpml\SupportVectorMachine\Kernel;
$dataset = new CsvDataset('data/languages.csv', 1);
$vectorizer = new TokenCountVectorizer(new WordTokenizer());
$samples = [];
foreach ($dataset->getSamples() as $sample) {
$samples[] = $sample[0];
}
$vectorizer->transform($samples);
$dataset = new ArrayDataset($samples, $dataset->getTargets());
$randomSplit = new RandomSplit($dataset, 0.25);
$classifier = new SVC(Kernel::RBF, 100);
$classifier->train($randomSplit->getTrainSamples(), $randomSplit->getTrainLabels());
$predictedLabels = $classifier->predict($randomSplit->getTestSamples());
echo Accuracy::score($randomSplit->getTestLabels(), $predictedLabels);