added unit tests, and inserted the verbalizer class to the main context

Esse commit está contido em:
Felipe Nascimento de Moura
2010-12-19 18:05:16 -02:00
commit 6f8145841a
92 arquivos alterados com 291 adições e 74 exclusões
@@ -0,0 +1,49 @@
<?php
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/languages/pt-BR/IgnoreForms.php';
/**
* Test class for IgnoreForms.
* Generated by PHPUnit on 2010-12-19 at 11:23:40.
*/
class IgnoreFormsTest extends PHPUnit_Framework_TestCase {
/**
* @var IgnoreForms
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
$this->object = new IgnoreForms;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
public function testShouldBeIgnored() {
$this->assertTrue(IgnoreForms::shouldBeIgnored('felizmente'));
}
public function testShouldBeIgnored1() {
$this->assertTrue(IgnoreForms::shouldBeIgnored('provavelmente'));
}
public function testShouldBeUsed() {
$this->assertTrue(IgnoreForms::shouldBeUsed('trabalhará'));
}
public function testShouldBeUsed1() {
$this->assertTrue(IgnoreForms::shouldBeUsed('mente'));
}
}
?>
@@ -53,6 +53,12 @@ class InflectTest extends PHPUnit_Framework_TestCase {
public function testisSingular7() {
$this->assertTrue(Inflect::isSingular('atlas'));
}
public function testisSingular8() {
$this->assertTrue(Inflect::isSingular('professor'));
}
public function testisSingular9() {
$this->assertFalse(Inflect::isSingular('professores'));
}
public function testToPlural() {
$this->assertEquals(Inflect::toPlural('homem'), 'homens');
@@ -103,6 +109,9 @@ class InflectTest extends PHPUnit_Framework_TestCase {
public function testToSingular7() {
$this->assertEquals(Inflect::toSingular('alunas'), 'aluna');
}
public function testToSingular8() {
$this->assertEquals(Inflect::toSingular('professores'), 'professor');
}
public function testToFemale() {
$this->assertEquals(Inflect::toFemale('aluno'), 'aluna');
@@ -150,6 +159,9 @@ class InflectTest extends PHPUnit_Framework_TestCase {
public function testToMale8() {
$this->assertEquals(Inflect::toMale('doutora'), 'doutor');
}
public function testToMale9() {
$this->assertEquals(Inflect::toMale('professora'), 'professor');
}
public function testIsFemale() {
$this->assertTrue(Inflect::isFemale('mulher'));
@@ -181,5 +193,8 @@ class InflectTest extends PHPUnit_Framework_TestCase {
public function testIsFemale9() {
$this->assertTrue(Inflect::isFemale('pequenininha'));
}
public function testIsFemale10() {
$this->assertFalse(Inflect::isFemale('professor'));
}
}
?>
@@ -1,5 +1,6 @@
<?php
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/Mind.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/languages/pt-BR/Verbalizer.php';
/**
@@ -92,7 +93,6 @@ class VerbalizerTest extends PHPUnit_Framework_TestCase {
public function testIsVerb20() {
$this->assertFalse(Verbalizer::isVerb('violão'));
}
public function testIsVerb21() {
$this->assertTrue(Verbalizer::isVerb('falhar'));
}
@@ -126,9 +126,12 @@ class VerbalizerTest extends PHPUnit_Framework_TestCase {
public function testIsVerb31() {
$this->assertTrue(Verbalizer::isVerb('comeu'));
}
public function testIsVerb32() {
$this->assertFalse(Verbalizer::isVerb('professor'));
}
// testing the toInfinitive method
// NOTICE that its goas is about the present/future words...
// NOTICE that its goals is about the present/future words...
// past is not supported
public function testToInfinitive1() {
$this->assertEquals('saber', Verbalizer::toInfinitive('sei'));
@@ -0,0 +1,4 @@
de
que
da
do
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
+5
Ver Arquivo
@@ -24,6 +24,7 @@
public static $tokenizer;
public static $langPath= "";
public static $content= "";
public static $curLang= 'en';
/**
* This method returns or outputs messages using the L10N library
@@ -94,8 +95,12 @@
$this->defaults= parse_ini_file($path.'/mind3rd/env/defaults.ini');
$this->conf= parse_ini_file($path.'/mind3rd/env/mind.ini');
include($path.'/mind3rd/API/L10N/'.$this->defaults['default_human_language'].'.php');
Mind::$curLang= $this->defaults['default_human_languageName'];
Mind::$l10n= new $this->defaults['default_human_language']();
Mind::$langPath= $path.'/mind3rd/API/languages/';
Mind::$curLang= $this->defaults['default_human_language'];
$langPath= $path.'/mind3rd/API/languages/'.$this->defaults['default_human_languageName'].'/';
set_include_path(get_include_path() . PATH_SEPARATOR . $langPath);
}
/**
* function taken from: http://www.dasprids.de/blog/2008/08/22/getting-a-password-hidden-from-stdin-with-php-cli
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
+28 -11
Ver Arquivo
@@ -1,12 +1,8 @@
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Lexer
*
* This class sweeps the passed words and validates if
* its content matches with the valid chars
* @package cortex.analyst
* @author felipe
*/
class Lexer
@@ -16,6 +12,7 @@ class Lexer
public $lang= 'en';
private $content= "";
private $tokens= Array();
private $glue= '';
/**
* Thanks to saeedco (no more information found about him!)
@@ -58,6 +55,11 @@ class Lexer
return $array;
}
/**
* Validates the sent char
* @param char $letter
* @return boolean
*/
private function isValidChar($letter)
{
$letter= ($letter);
@@ -74,6 +76,12 @@ class Lexer
return false;
}
/**
* Performs a sweep over the sent content and replace any
* special char, also removing any invalid char.
* @param string $content
* @return string The cleaned content
*/
public function sweep($content)
{
$this->content= trim(str_replace("\n", ' ', $content));
@@ -84,13 +92,15 @@ class Lexer
// the fixed content;
$fixed= "";
// for each charactere on the content
// let's remove th invalid ones
for($i=0, $j=sizeof($this->content); $i<$j; $i++)
{
$letter= $this->content[$i];
if($this->isValidChar($letter))
$fixed.= $letter;
}
// preparing the tokens
foreach($this->tokens as $char=>$token)
{
$fixed= str_replace($char, $token, $fixed);
@@ -101,16 +111,22 @@ class Lexer
return sizeof(Mind::$content)>0? Mind::$content: false;
}
/**
* Returns the fixed char to the sent char
* @param string $char
* @return string
*/
public function translateChars($str)
{
$from = $this->replacements[0];
$to = $this->replacements[1];
return strtr($str, $from, $to);
}
public function __construct()
{
GLOBAL $_MIND;
$this->glue= chr('176');
$this->lang= Mind::$l10n->name;
$xml= simplexml_load_file(Mind::$langPath.$this->lang.'/lexics.xml');
include(Mind::$langPath.$this->lang.'/Inflect.php');
@@ -125,8 +141,9 @@ class Lexer
$this->replacements[0]= (string)$xml->replacements->from;
$this->replacements[1]= (string)$xml->replacements->to;
$this->tokens[' ']= chr('176');
$this->tokens['.']= chr('176').'.'.chr('176');
$this->tokens[' ']= $this->glue;
$this->tokens['.']= $this->glue.'.'.$this->glue;
$this->tokens[',']= $this->glue.','.$this->glue;
// prepare it to the next step
Mind::$canonic= new Canonic();
Arquivo normal → Arquivo executável
+33 -10
Ver Arquivo
@@ -1,33 +1,56 @@
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Canonic
*
* This class will take the content to be used, and take
* it to its canonical form
* @package cortex.analyst
* @author felipe
*/
class Canonic extends Inflect{
/**
* Takes a word to its canonic form
* @param string$word
* @return string
*/
public static function canonize($word)
{
if(!self::isSingular($word))
$word= self::toSingular($word);
if(self::isFemale($word))
$word= self::toMale($word);
echo $word."<br>";
return $word;
}
/**
* Brings all the words in the Array to their canonical form
* @param array $content
* @return Array
*/
public function sweep(Array $content)
{
array_map(function ($word)
$v= new Verbalizer;
$newContent= Array();
foreach($content as $word)
{
if(!Verbalizer::isVerb($word) && strlen($word) > 1)
{
//echo 'the canonic form of '.$word.' is '.Canonic::canonize($word);
$word= Canonic::canonize($word);
}
$newContent[]= $word;
}
/*array_map(function ($word)
{
print_r($v);
//echo Verbalizer::toInfinitive($word);
//if(Verbalizer::isVerb($word))
//return Verbalizer::toInfinitive($word);
Canonic::canonize($word);
},
$content);
print_r($content);
*/
print_r($newContent);
return $newContent;
}
public function __construct()
externo Arquivo normal → Arquivo executável
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
Ver Arquivo
Ver Arquivo
Ver Arquivo
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
Ver Arquivo
Ver Arquivo
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
Ver Arquivo
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
Ver Arquivo
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
+74
Ver Arquivo
@@ -0,0 +1,74 @@
<?php
/**
* This class provides a list of instructtions
* which define when a word should be ignored,
* plus a list of key words to ignore
* @package cortex.analyst
* @author felipe
*/
class IgnoreForms {
/**
* The list of words to be ignored
* @var Array
* @static
*/
public static $ignoreList= false;
/**
* A list of rules, to identify words to be ignored
* @var Array
* @static
*/
public static $ignoreRules= Array(
'/(.)mente$/'
);
/**
* This method reads the ignore.list file and
* parses it to an indexed array
* @static
* @static
* @name loadVerbs
*/
public static function loadIgnoreList()
{
$fR= fopen('ignore.list', 'rb');
self::$ignoreList= Array();
while (!feof($fR)){
$word= preg_replace('/\s/', '', fgets($fR, 4096));
self::$ignoreList[$word]= true;
}
}
/**
* Returns wheter the word should or not be ignored
* @name shouldBeIgnored
* @param string $word
* @return boolean
*/
public static function shouldBeIgnored($word)
{
if(!self::$ignoreList)
self::loadIgnoreList();
if(isset(self::$ignoreList[$word]))
return true;
foreach(self::$ignoreRules as $rule)
{
if(preg_match($rule, $word))
return true;
}
return false;
}
/**
* Returns if the word should be used
* @name shouldBeUsed
* @static
* @param string $word
* @return boolean
*/
public static function shouldBeUsed($word)
{
return !self::shouldBeIgnored($word);
}
}
Arquivo normal → Arquivo executável
+1 -1
Ver Arquivo
@@ -17,9 +17,9 @@
* This class should inflect words for different idioms
* changing its genre and number
* IN THIS CASE: pt-BR
*
* @name Inflect
* @author Felipe Nascimento de Moura <felipenmoura@gmail.com>
* @package cortex.analyst
*/
class Inflect implements inflection
{
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
+19 -7
Ver Arquivo
@@ -1,9 +1,9 @@
<?php
/**
* This class should identify verbs
* NOTICE that its goas is about the present/future words...
* NOTICE that its goals is about the present/future words...
* past is not supported
*
* @package cortex.analyst
* @author felipe
*/
class Verbalizer {
@@ -14,7 +14,6 @@ class Verbalizer {
public static $verbs= false;
/**
*
* @var AssocArray An array of verbs that may indicate a possibility
*/
static $possibilities= Array(
@@ -27,7 +26,7 @@ class Verbalizer {
);
/**
*
* @static
* @var AssocArray the possible flections a verb may suffer
*/
static $flections = array(
@@ -55,7 +54,7 @@ class Verbalizer {
);
/**
*
* @static
* @var AssocArray A list of fixed exceptions
*/
static $exceptions= Array(
@@ -77,6 +76,7 @@ class Verbalizer {
/**
* Returns if the passed word means the verb is a possibility
* @param string $word
* @static
* @return boolean
*/
public static function isAPosibility($string)
@@ -87,13 +87,15 @@ class Verbalizer {
/**
* Returns if the passed word is in the infinitive form
* @param string $word
* @static
* @return boolean
*/
public static function isInfinitive($string)
{
$isInVerBlist= self::isInVerbList($string);
foreach(self::$flections as $pattern=>$result)
{
if(preg_match('/'.$result.'$/i', $string) && self::isInVerbList($string))
if(preg_match('/'.$result.'$/i', $string) && $isInVerBlist)
return true;
}
return false;
@@ -104,12 +106,14 @@ class Verbalizer {
* Returns false if it is not in the verbs.list
* @param string $word
* @return string
* @static
*/
public static function toInfinitive($string)
{
$string= strtolower($string);
if(self::isInfinitive($string))
return $string;
if(isset(self::$exceptions[$string]))
{
return self::$exceptions[$string];
@@ -136,6 +140,7 @@ class Verbalizer {
* Verifies if the passed word is in the verbs.list
* @param string $word
* @return boolean
* @static
*/
public static function isInVerbList($string)
{
@@ -147,6 +152,7 @@ class Verbalizer {
/**
* Returns if the received word is a verb or not
* @param string $word
* @static
*/
public static function isVerb($word)
{
@@ -157,11 +163,17 @@ class Verbalizer {
}
/**
* This method reads the verbs.list file and
* parses it to an indexed array
* @name loadVerbs
* @static
*/
public static function loadVerbs()
{
$fR= fopen('verbs.list', 'rb');
if(!file_exists('verbs.list'))
$fR= fopen(Mind::$langPath.Mind::$l10n->name.'/verbs.list', 'rb');
else
$fR= fopen('verbs.list', 'rb');
self::$verbs= Array();
while (!feof($fR)){
$verb= preg_replace('/\s/', '', fgets($fR, 4096));
+4
Ver Arquivo
@@ -0,0 +1,4 @@
de
que
da
do
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
+52 -42
Ver Arquivo
@@ -8,48 +8,8 @@
*/
session_start();
define('_CONSOLE_LINE_LENGTH_', 80);
function __autoload($what)
{
GLOBAL $_MIND;
$what= preg_replace("/[ '\"\.\/]/", '', $what);
if(file_exists(_MINDSRC_.'/mind3rd/API/programs/'.$what.'.php'))
{
include(_MINDSRC_.'/mind3rd/API/programs/'.$what.'.php');
return true;
}
if(strpos($what, '\\')>=0)
{
$what= explode('\\', $what);
$what= array_pop($what);
}
$dirs= Array(
_MINDSRC_.'/mind3rd/API/external/Symfony/Component/Console/Input/',
_MINDSRC_.'/mind3rd/API/external/Symfony/Component/Console/Output/',
_MINDSRC_.'/mind3rd/API/external/Symfony/Component/Console/Command/',
_MINDSRC_.'/mind3rd/API/external/Symfony/Component/Console/Helper/',
_MINDSRC_.'/mind3rd/API/cortex/Lexer/',
_MINDSRC_.'/mind3rd/API/interfaces/',
_MINDSRC_.'/mind3rd/API/programs/',
_MINDSRC_.'/mind3rd/API/L10N/',
_MINDSRC_.'/mind3rd/API/classes/',
_MINDSRC_.'/mind3rd/API/cortex/tokenizer/',
_MINDSRC_.'/mind3rd/API/cortex/canonic/',
_MINDSRC_.'/mind3rd/API/cortex/sintaxer/'
);
for($i=0; $i<sizeof($dirs); $i++)
{
if(file_exists($dirs[$i].$what.'.php'))
{
require_once($dirs[$i].$what.'.php');
return true;
}
}
echo " [ERROR] Class not found: ".$what."\n";
exit;
return false;
}
require(_MINDSRC_.'/mind3rd/API/classes/Mind.php');
/*require(_MINDSRC_.'/mind3rd/API/interfaces/program.php');
require(_MINDSRC_.'/mind3rd/API/classes/Mind.php');
require(_MINDSRC_.'/mind3rd/API/classes/MindDB.php');
@@ -106,6 +66,56 @@
Mind::$lexer= new Lexer();
function __autoload($what)
{
GLOBAL $_MIND;
$what= preg_replace("/[ '\"\.\/]/", '', $what);
if(file_exists(_MINDSRC_.'/mind3rd/API/programs/'.$what.'.php'))
{
include(_MINDSRC_.'/mind3rd/API/programs/'.$what.'.php');
return true;
}
if(strpos($what, '\\')>=0)
{
$what= explode('\\', $what);
$what= array_pop($what);
}
$dirs= Array(
_MINDSRC_.'/mind3rd/API/external/Symfony/Component/Console/Input/',
_MINDSRC_.'/mind3rd/API/external/Symfony/Component/Console/Output/',
_MINDSRC_.'/mind3rd/API/external/Symfony/Component/Console/Command/',
_MINDSRC_.'/mind3rd/API/external/Symfony/Component/Console/Helper/',
_MINDSRC_.'/mind3rd/API/cortex/Lexer/',
_MINDSRC_.'/mind3rd/API/interfaces/',
_MINDSRC_.'/mind3rd/API/programs/',
_MINDSRC_.'/mind3rd/API/L10N/',
_MINDSRC_.'/mind3rd/API/classes/',
_MINDSRC_.'/mind3rd/API/cortex/tokenizer/',
_MINDSRC_.'/mind3rd/API/cortex/canonic/',
_MINDSRC_.'/mind3rd/API/cortex/sintaxer/'
);
for($i=0; $i<sizeof($dirs); $i++)
{
if(file_exists($dirs[$i].$what.'.php'))
{
require_once($dirs[$i].$what.'.php');
return true;
}
}
// let's check if it is a language
$langPath= _MINDSRC_.'/mind3rd/API/languages/'.$_MIND->defaults['default_human_languageName'].'/'.$what.'.php';
if(file_exists($langPath))
{
require_once($langPath);
return true;
}
echo " [ERROR] Class not found: ".$what."\n";
exit;
return false;
}
if($_REQ['env']=='shell')
include('shell.php');
else
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
externo Arquivo normal → Arquivo executável
Ver Arquivo
externo Arquivo normal → Arquivo executável
+1
Ver Arquivo
@@ -3,6 +3,7 @@
;
; the default idiom
default_human_language=ptBR
default_human_languageName=pt-BR
; default timezone
timezone=America/Sao_paulo
; the default programming language
externo Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
Ver Arquivo
Arquivo normal → Arquivo executável
+1 -1
Ver Arquivo
@@ -1,2 +1,2 @@
bem, sabemos que todo professor pode ter muitos alunos & que cada aluno pode ter vários professores.
Tamabém, que cada professora tem um chefe.
Também, que cada professora tem um chefe.
Arquivo normal → Arquivo executável
Ver Arquivo