-
Notifications
You must be signed in to change notification settings - Fork 3
/
CombineMorphElement.php
executable file
·227 lines (210 loc) · 6.5 KB
/
CombineMorphElement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* Данный класс предназначен для создания узлов дерева, хранящегося в CombineMorph
* и по сути он выполняет всю работу по конструированию дерева, созданию случайных текстов и т.д.
*
* @author mihanentalpo
*/
class CombineMorphElement
{
//Скалярное значение, например простая строка "привет"
const TYPE_SCALAR=0;
//Последовательность - массив из разных типов нодов
const TYPE_SEQUENCE=1;
//Массив из разных элементов, из которых нужно выбрать один случайный
const TYPE_OR=2;
//Массив из разных элементов, которые нужно соединить в случайной последовательности
const TYPE_AND=3;
/**
* Тип данного элемента, по умолчанию - скаляр
* @var integer
*/
public $type=self::TYPE_SCALAR;
/**
* Массив дочерних элементов, используется для всего кроме TYPE_SCALAR
* @var array
*/
public $items = array();
/**
* Текст, хранящийся в скалярном элементе
* @var string
*/
public $scalarText = "";
/**
* Функция, вычисляющая факториал числа
* @param integer $n
* @return integer
*/
protected function fact($n)
{
$f = 1;
for($i=2;$i<=$n;$i++)
{
$f=$f * $i;
}
return $f;
}
/**
* Создать элемент. Обычно его создаёт CombineMorph. Вручную создавать его нет смысла
* @param string $src строка с текстом, который может содержать ссылки на "распарсенные элементы" в виде %#число#%, например %#1#%
* @param array $items массив строк, которые вырезаны из первоначальной строки и должны быть использованы при анализе текста $src как значения, на которые указывают #&, #& и т.д.
*
*/
public function __construct($src,$foundItems=array())
{
static $deep=0;
$deep++;
$spc = str_repeat(" ",$deep);
//echo "{$spc}Parsing text...\n";
if (substr($src,0,1)=='{' && substr($src,-1,1)=='}')
{
//echo "{$spc}It's an OR item: $src\n";
$this->type = self::TYPE_OR;
$src = substr($src,1,-1);
$parts = explode("|",$src);
foreach($parts as $part)
{
$this->items[] = new CombineMorphElement($part,$foundItems);
}
}
else if(substr($src,0,1)=='[' && substr($src,-1,1)==']')
{
//echo "{$spc}It's an AND item: $src\n";
$src = substr($src,1,-1);
$this->type=self::TYPE_AND;
$parts = explode("|",$src);
foreach($parts as $part)
{
$this->items[] = new CombineMorphElement($part,$foundItems);
}
}
else if(preg_match_all('|%#(?P<itemNum>[0-9]+)#%|',$src,$res,PREG_SET_ORDER))
{
//echo "{$spc}It's a sequence: $src\n";
$this->type = self::TYPE_SEQUENCE;
$this->items = array();
$lastPos=0;
foreach($res as &$itm)
{
$lastPos = strpos($src,$itm['0']);
$itm['pos'] = $lastPos;
}
unset($itm);
if ($res[0]['pos']>0)
{
$prepend = substr($src,0,$res[0]['pos']);
//echo $prepend . "\n";
$this->items[] = new CombineMorphElement($prepend,$foundItems);
}
foreach($res as $num=>$itm)
{
$this->items[] = new CombineMorphElement($foundItems[$itm['itemNum']],$foundItems);
if ($num+1 < count($res))
{
$between = substr($src,$itm['pos']+strlen($itm[0]),$res[$num+1]['pos']-($itm['pos']+strlen($itm[0])));
//echo "Between $num and ". ($num+1) . ": '" . $between . "'";
$this->items[] = new CombineMorphElement($between, $foundItems);
//echo " not last\n";
}
}
$lastItm = end($res);
if ($lastItm['pos'] + strlen($lastItm[0]) + 1 < strlen($src))
{
$after = substr($src,$lastItm['pos'] + strlen($lastItm[0]));
$this->items[] = new CombineMorphElement($after, $foundItems);
//echo "After: " . $after . "\n";
}
}
else
{
//echo "{$spc}It's a scalar: '$src'\n";
$this->scalarText = $src;
}
$deep--;
}
/**
* Возвращает случайны вариант текста текущего объекта, а также, всех вложенных объектов
* @param boolean $addSpanOnRandom Добавлять <span class='random-combine-element'>вокруг любых элементов кроме скаляров</span> нужно для отладки
* @return string
*/
public function getRandomVariant($addSpanOnRandom=false)
{
if ($addSpanOnRandom)
{
$decor = function($text){return "<span class='random-combine-element'>" . $text . "</span>";};
}
else
{
$decor = function($text){return $text;};
}
$res="";
switch ($this->type)
{
case self::TYPE_SCALAR:
return $this->scalarText;
break;
case self::TYPE_SEQUENCE:
foreach($this->items as $item)
{
$res .= $item->getRandomVariant($addSpanOnRandom);
}
return $res;
break;
case self::TYPE_AND:
shuffle($this->items);
foreach($this->items as $item)
{
$res .= " " . $item->getRandomVariant($addSpanOnRandom);
}
$res = trim($res);
return $decor($res);
break;
case self::TYPE_OR:
$key = array_rand($this->items);
$res = $this->items[$key]->getRandomVariant($addSpanOnRandom);
return $decor($res);
break;
}
}
/**
* Подсчитывает количество вариантов текста, которые могут быть сгенерированы данным элементом
* @return int
*/
public function getVariantsCount()
{
switch ($this->type)
{
case self::TYPE_SCALAR:
return 1;
break;
case self::TYPE_SEQUENCE:
$res = 1;
foreach($this->items as $item)
{
$res = $res * $item->getVariantsCount();
}
if ($res==0) echo "sequence res=0\n";
return $res;
break;
case self::TYPE_AND:
$res = $this->fact(count($this->items));
foreach($this->items as $item)
{
$res = $res * $item->getVariantsCount();
}
if ($res==0) echo "AND res=0\n";
return $res;
break;
case self::TYPE_OR:
$res = 0;
foreach($this->items as $item)
{
$res = $res + $item->getVariantsCount();
}
if ($res==0) echo "OR res=0\n";
return $res;
break;
}
}
}
?>