First steps on entities, properties and relations

Esse commit está contido em:
Felipe Nascimento de Moura
2011-02-09 01:36:38 -02:00
commit 3e743cb54d
6 arquivos alterados com 203 adições e 10 exclusões
+18 -1
Ver Arquivo
@@ -7,12 +7,29 @@
*/
class MindEntity {
public $name;
public $properties= Array();
public $relations= Array();
public static function isEntity($definition)
{
return strpos($definition, ":")? false: true;
}
public function MindEntity()
public function addProperty(MindProperty $property)
{
$this->properties[$property->name]= $property;
return $this;
}
public function addRef(MindRelation &$rel)
{
$this->relations[]= &$rel;
return $this;
}
public function MindEntity($word)
{
$this->name= (string)$word;
}
}
+1 -1
Ver Arquivo
@@ -144,6 +144,6 @@
{
$this->definition= $definition;
$this->parse();
echo json_encode($this)."\n";
//echo json_encode($this)."\n";
}
}
+66
Ver Arquivo
@@ -0,0 +1,66 @@
<?php
/**
* Represents a relation between two Entities
*
* @author felipe
*/
class MindRelation {
public $name;
private $linkTypes = Array('possibility', 'must', 'action');
private $linkType = 'action';
private $quantifiers = Array(0, 1, 'n');
private $min = 0;
private $max = 'n';
private $verb = '';
private $focus = null;
private $rel = null;
public function setLinkType($linkType)
{
if(in_array($linkType, $this->linkTypes))
{
$this->linkType= $linkType;
return $this;
}
return false;
}
public function setMin($min)
{
if(in_array($min, $this->quantifiers))
{
$this->min= $min;
return $this;
}
return false;
}
public function setMax($max)
{
if(in_array($max, $this->quantifiers))
{
$this->max= $max;
return $this;
}
return false;
}
public function setUsedVerb($verb)
{
$this->verb= (string)$verb;
return $this;
}
public function setEntities(MindEntity &$focus, MindEntity &$rel)
{
$this->focus= &$focus;
$this->rel= &$rel;
return $this;
}
public function MindRelation($relName)
{
$this->name= (string)$relName;
}
}
+114 -6
Ver Arquivo
@@ -23,6 +23,17 @@
*/
class Analyst {
public static $entities= Array();
public static $relations= Array();
public static function getUniverse()
{
return Array(
'entities'=>self::$entities,
'relations'=>self::$relations
);
}
/**
* This method receives each expression and analizes
* the best way to act and to store the understood
@@ -35,11 +46,60 @@ class Analyst {
*/
public static function analize($expression, $structure, $structureKeys){
// foreach token
// setting up
$tmpProperties= Array();
$i= 0;
$focus= null;
$linkVerb= null;
$min= null;
$max= 'n';
$linkType= 'action';
// foreach token
foreach($structureKeys as $token)
{
$word= $expression[$i];
$i++;
// storing the current used verb
if($token==Tokenizer::MT_VERB)
{
$linkVerb= $word;
continue;
}
if(
$min == null &&
(
$token == Tokenizer::MT_NONE ||
$token == Tokenizer::MT_ONE
)
)
{
$min= ($token == Tokenizer::MT_ONE)? 1: 0;
continue;
}
if(
$min != null &&
(
$token == Tokenizer::MT_MANY ||
$token == Tokenizer::MT_ONE
)
)
{
$max= ($token == Tokenizer::MT_ONE)? 1: 'n';
}
// verifying the way the entities will be linked
if($token==Tokenizer::MT_QMAY)
{
$linkType= 'possibility';
continue;
}
if($token==Tokenizer::MT_QMUST)
{
$linkType= 'must';
continue;
}
// if it is a substantive
if($token==Tokenizer::MT_SUBST)
@@ -47,13 +107,61 @@ class Analyst {
// if it is an entity
if(MindEntity::isEntity($word))
{
//echo "it is an entity!\n";
}else{ // otherwise, it must be a property
$tmpProperty= new MindProperty($word);
// yeah, I know it looks crazy, but try to follow my thoughts
// let's instantiate a new Entity in case it has not been
// instantiated before
if(!isset(self::$entities[$word]))
self::$entities[$word]= new MindEntity($word);
// each instruction *should* have one focused entity
// we will use the first entity on each expression as focus
if(!$focus)
{
$focus= self::$entities[$word];
}else{
/*
* here, if it is an entity and the focused
* entity has already been selected, it means
* it is the second entity on the instruction, so,
* it is a relation between entities(or should be)
*/
$rel= self::$entities[$word];
/*
* we will use this relationName as index on an
* indexed array to speed up the search for
* relations in the future
*/
$relationName= $focus->name."_".$rel->name;
// let's create the relation itself
$curRelation= new MindRelation($relationName);
$curRelation->setLinkType($linkType)
->setMin($min)
->setMax($max)
->setUsedVerb($linkVerb)
->setEntities($focus, $rel);
// now, both entities will POINT to the same relation
$focus->addRef($curRelation);
$rel->addRef($curRelation);
// and let's use the relation name as index, as said before
self::$relations[$relationName]= &$curRelation;
}
}else{
// ok, after that, this is just easy, now :)
// let's store all the properties to a temporary array
$tmpProperties[]= new MindProperty($word);
}
}
$i++;
}
return true;
// adding the properties to the focused entity
// we're doing it now, because according to the selected idiom
// the sequence of focused entity and properties may vary
if(sizeof($tmpProperties)>0 && $focus)
{
foreach($tmpProperties as $prop)
$focus->addProperty($prop);
}
}
}
+3 -2
Ver Arquivo
@@ -16,8 +16,9 @@ class Canonic{
*/
public static function canonize($word)
{
if(!pt\Inflect::isSingular($word)) /* TODO: fix it*/
$word= pt\Inflect::toSingular($word);
$inflec= Mind::$currentProject['idiom']."\Inflect";
if(!$inflec::isSingular($word)) /* TODO: fix it*/
$word= $inflec::toSingular($word);
/*if(self::isFemale($word)) // demands more tests
// apparently, female substantives are brought to a wrong male form
$word= self::toMale($word);*/
+1
Ver Arquivo
@@ -67,6 +67,7 @@ class Syntaxer {
// let's analize it, now
Analyst::analize($expression, $struct, $tokens);
}
print_r(Analyst::getUniverse());
return $this;
}
}