Completed
Pull Request — master (#23)
by Peter
01:51
created
AnalyzerText/Filter/WordList/WordList.php 2 patches
Indentation   +154 added lines, -154 removed lines patch added patch discarded remove patch
@@ -20,158 +20,158 @@
 block discarded – undo
20 20
  */
21 21
 abstract class WordList extends Filter
22 22
 {
23
-    /**
24
-     * Простые слова.
25
-     *
26
-     * @var array
27
-     */
28
-    private $simple = array();
29
-
30
-    /**
31
-     * Составные слова.
32
-     *
33
-     * Составные слова о части которого нам известно.
34
-     * Например слово пишется через тирэ
35
-     *
36
-     * @var array
37
-     */
38
-    private $composite = array();
39
-
40
-    /**
41
-     * Последовательности из набора слов.
42
-     *
43
-     * @var array
44
-     */
45
-    private $sequence = array();
46
-
47
-    /**
48
-     * @param Text $iterator Текст
49
-     */
50
-    public function __construct(Text $iterator)
51
-    {
52
-        parent::__construct($iterator);
53
-        $this->repackWordList();
54
-    }
55
-
56
-    /**
57
-     * Проверяет, является ли текущее слово допустимым
58
-     *
59
-     * @return bool
60
-     */
61
-    public function accept()
62
-    {
63
-        $word = $this->current();
64
-
65
-        return $this->isSequence($word) || $this->isSimple($word) || $this->isComposite($word);
66
-    }
67
-
68
-    /**
69
-     * Это последовательность.
70
-     *
71
-     * @param Word $word Слово
72
-     *
73
-     * @return bool
74
-     */
75
-    public function isSequence(Word $word)
76
-    {
77
-        $plain = $word->getPlain();
78
-        foreach ($this->sequence as $sequence) {
79
-            if ($sequence[0] === $plain) {
80
-                $sequence_length = count($sequence);
81
-                for ($i = 1; $i < $sequence_length; ++$i) {
82
-                    if (!($next_word = $this->getNextWord($i)) || $next_word->getPlain() != $sequence[$i]) {
83
-                        continue 2;
84
-                    }
85
-                }
86
-
87
-                // удаляем слова из последовательности
88
-                $key = $this->getText()->key();
89
-                for ($i = 1; $i < $sequence_length; ++$i) {
90
-                    $this->getText()->offsetUnset($key + $i);
91
-                }
92
-
93
-                return true;
94
-            }
95
-        }
96
-
97
-        return false;
98
-    }
99
-
100
-    /**
101
-     * Это простое слово.
102
-     *
103
-     * @param Word $word Слово
104
-     *
105
-     * @return bool
106
-     */
107
-    public function isSimple(Word $word)
108
-    {
109
-        return in_array($word->getPlain(), $this->simple);
110
-    }
111
-
112
-    /**
113
-     * Это составное слово.
114
-     *
115
-     * @param Word $word Слово
116
-     *
117
-     * @return bool
118
-     */
119
-    public function isComposite(Word $word)
120
-    {
121
-        foreach ($this->composite as $reg) {
122
-            if (preg_match($reg, $word->getWord())) {
123
-                return true;
124
-            }
125
-        }
126
-
127
-        return false;
128
-    }
129
-
130
-    /**
131
-     * Возвращает список слов.
132
-     *
133
-     * Возвращает список слов которые необходимо удалить или оставить
134
-     * Если слово составное и пишестся через тире, но одна из частей может менятся например:
135
-     * <code>
136
-     *   подай-ка, налей-ка, молоко-то сбежало, наценка-с
137
-     * </code>
138
-     * то нужно писать шаблон вида:
139
-     * <code>
140
-     *   [ '*-ка', '*-то', '*-с' ]
141
-     * </code>
142
-     * Для удаления последовательности слов ячейка слова должна представляться в виде набора слов разделенных пробелом
143
-     * <code>
144
-     *   [ 'вовсе не', 'несмотря на то что' ]
145
-     * </code>
146
-     * Так же есть возможность указывать регулярные выражения для отлавливания сложных конструкций
147
-     * <code>
148
-     *   // ААааа Аааа-а-а
149
-     *   [ '/^а+(\-а+)*$/ui' ]
150
-     * </code>
151
-     * В регулярное выражение передается оригинальное слово, а не урощенная форма
152
-     *
153
-     * @return array
154
-     */
155
-    abstract public function getWords();
156
-
157
-    /**
158
-     * Разбор набора шаблонов слов и составление условий поиска соответствий.
159
-     */
160
-    private function repackWordList()
161
-    {
162
-        $words = $this->getWords();
163
-        // разбор на категории
164
-        foreach ($words as $word) {
165
-            if ($word[0] == '/') { // регулярное выражение
166
-                $this->composite[] = $word;
167
-            } elseif (strpos($word, ' ') !== false) { // последовательность
168
-                $this->sequence[] = explode(' ', $word);
169
-            } elseif (strpos($word, '*') !== false) { // псевдо регулярка
170
-                // из записи *-то делаем регулярное выражение вида: /^.+?\-то$/ui
171
-                $this->composite[] = '/^'.str_replace('\*', '.+?', preg_quote($word, '/')).'$/ui';
172
-            } else { // простое слово
173
-                $this->simple[] = $word;
174
-            }
175
-        }
176
-    }
23
+	/**
24
+	 * Простые слова.
25
+	 *
26
+	 * @var array
27
+	 */
28
+	private $simple = array();
29
+
30
+	/**
31
+	 * Составные слова.
32
+	 *
33
+	 * Составные слова о части которого нам известно.
34
+	 * Например слово пишется через тирэ
35
+	 *
36
+	 * @var array
37
+	 */
38
+	private $composite = array();
39
+
40
+	/**
41
+	 * Последовательности из набора слов.
42
+	 *
43
+	 * @var array
44
+	 */
45
+	private $sequence = array();
46
+
47
+	/**
48
+	 * @param Text $iterator Текст
49
+	 */
50
+	public function __construct(Text $iterator)
51
+	{
52
+		parent::__construct($iterator);
53
+		$this->repackWordList();
54
+	}
55
+
56
+	/**
57
+	 * Проверяет, является ли текущее слово допустимым
58
+	 *
59
+	 * @return bool
60
+	 */
61
+	public function accept()
62
+	{
63
+		$word = $this->current();
64
+
65
+		return $this->isSequence($word) || $this->isSimple($word) || $this->isComposite($word);
66
+	}
67
+
68
+	/**
69
+	 * Это последовательность.
70
+	 *
71
+	 * @param Word $word Слово
72
+	 *
73
+	 * @return bool
74
+	 */
75
+	public function isSequence(Word $word)
76
+	{
77
+		$plain = $word->getPlain();
78
+		foreach ($this->sequence as $sequence) {
79
+			if ($sequence[0] === $plain) {
80
+				$sequence_length = count($sequence);
81
+				for ($i = 1; $i < $sequence_length; ++$i) {
82
+					if (!($next_word = $this->getNextWord($i)) || $next_word->getPlain() != $sequence[$i]) {
83
+						continue 2;
84
+					}
85
+				}
86
+
87
+				// удаляем слова из последовательности
88
+				$key = $this->getText()->key();
89
+				for ($i = 1; $i < $sequence_length; ++$i) {
90
+					$this->getText()->offsetUnset($key + $i);
91
+				}
92
+
93
+				return true;
94
+			}
95
+		}
96
+
97
+		return false;
98
+	}
99
+
100
+	/**
101
+	 * Это простое слово.
102
+	 *
103
+	 * @param Word $word Слово
104
+	 *
105
+	 * @return bool
106
+	 */
107
+	public function isSimple(Word $word)
108
+	{
109
+		return in_array($word->getPlain(), $this->simple);
110
+	}
111
+
112
+	/**
113
+	 * Это составное слово.
114
+	 *
115
+	 * @param Word $word Слово
116
+	 *
117
+	 * @return bool
118
+	 */
119
+	public function isComposite(Word $word)
120
+	{
121
+		foreach ($this->composite as $reg) {
122
+			if (preg_match($reg, $word->getWord())) {
123
+				return true;
124
+			}
125
+		}
126
+
127
+		return false;
128
+	}
129
+
130
+	/**
131
+	 * Возвращает список слов.
132
+	 *
133
+	 * Возвращает список слов которые необходимо удалить или оставить
134
+	 * Если слово составное и пишестся через тире, но одна из частей может менятся например:
135
+	 * <code>
136
+	 *   подай-ка, налей-ка, молоко-то сбежало, наценка-с
137
+	 * </code>
138
+	 * то нужно писать шаблон вида:
139
+	 * <code>
140
+	 *   [ '*-ка', '*-то', '*-с' ]
141
+	 * </code>
142
+	 * Для удаления последовательности слов ячейка слова должна представляться в виде набора слов разделенных пробелом
143
+	 * <code>
144
+	 *   [ 'вовсе не', 'несмотря на то что' ]
145
+	 * </code>
146
+	 * Так же есть возможность указывать регулярные выражения для отлавливания сложных конструкций
147
+	 * <code>
148
+	 *   // ААааа Аааа-а-а
149
+	 *   [ '/^а+(\-а+)*$/ui' ]
150
+	 * </code>
151
+	 * В регулярное выражение передается оригинальное слово, а не урощенная форма
152
+	 *
153
+	 * @return array
154
+	 */
155
+	abstract public function getWords();
156
+
157
+	/**
158
+	 * Разбор набора шаблонов слов и составление условий поиска соответствий.
159
+	 */
160
+	private function repackWordList()
161
+	{
162
+		$words = $this->getWords();
163
+		// разбор на категории
164
+		foreach ($words as $word) {
165
+			if ($word[0] == '/') { // регулярное выражение
166
+				$this->composite[] = $word;
167
+			} elseif (strpos($word, ' ') !== false) { // последовательность
168
+				$this->sequence[] = explode(' ', $word);
169
+			} elseif (strpos($word, '*') !== false) { // псевдо регулярка
170
+				// из записи *-то делаем регулярное выражение вида: /^.+?\-то$/ui
171
+				$this->composite[] = '/^'.str_replace('\*', '.+?', preg_quote($word, '/')).'$/ui';
172
+			} else { // простое слово
173
+				$this->simple[] = $word;
174
+			}
175
+		}
176
+	}
177 177
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -87,7 +87,7 @@
 block discarded – undo
87 87
                 // удаляем слова из последовательности
88 88
                 $key = $this->getText()->key();
89 89
                 for ($i = 1; $i < $sequence_length; ++$i) {
90
-                    $this->getText()->offsetUnset($key + $i);
90
+                    $this->getText()->offsetUnset($key+$i);
91 91
                 }
92 92
 
93 93
                 return true;
Please login to merge, or discard this patch.
AnalyzerText/Filter/Filter.php 2 patches
Indentation   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -17,75 +17,75 @@
 block discarded – undo
17 17
  */
18 18
 abstract class Filter extends \FilterIterator
19 19
 {
20
-    /**
21
-     * @param Text $text
22
-     */
23
-    public function __construct(Text $text)
24
-    {
25
-        parent::__construct($text);
26
-    }
20
+	/**
21
+	 * @param Text $text
22
+	 */
23
+	public function __construct(Text $text)
24
+	{
25
+		parent::__construct($text);
26
+	}
27 27
 
28
-    /**
29
-     * Возвращает текущее слово.
30
-     *
31
-     * @return Word
32
-     */
33
-    public function current()
34
-    {
35
-        return $this->getInnerIterator()->current();
36
-    }
28
+	/**
29
+	 * Возвращает текущее слово.
30
+	 *
31
+	 * @return Word
32
+	 */
33
+	public function current()
34
+	{
35
+		return $this->getInnerIterator()->current();
36
+	}
37 37
 
38
-    /**
39
-     * Возвращает текст
40
-     *
41
-     * @return Text
42
-     */
43
-    public function getText()
44
-    {
45
-        return $this->getInnerIterator();
46
-    }
38
+	/**
39
+	 * Возвращает текст
40
+	 *
41
+	 * @return Text
42
+	 */
43
+	public function getText()
44
+	{
45
+		return $this->getInnerIterator();
46
+	}
47 47
 
48
-    /**
49
-     * Возвращает текст
50
-     *
51
-     * @return Text
52
-     */
53
-    public function getInnerIterator()
54
-    {
55
-        return parent::getInnerIterator();
56
-    }
48
+	/**
49
+	 * Возвращает текст
50
+	 *
51
+	 * @return Text
52
+	 */
53
+	public function getInnerIterator()
54
+	{
55
+		return parent::getInnerIterator();
56
+	}
57 57
 
58
-    /**
59
-     * Заменяет слово в тексте.
60
-     *
61
-     * @param Word $word Слово
62
-     */
63
-    protected function replace(Word $word)
64
-    {
65
-        $this->getInnerIterator()->replace($word);
66
-    }
58
+	/**
59
+	 * Заменяет слово в тексте.
60
+	 *
61
+	 * @param Word $word Слово
62
+	 */
63
+	protected function replace(Word $word)
64
+	{
65
+		$this->getInnerIterator()->replace($word);
66
+	}
67 67
 
68
-    /**
69
-     * Возвращает предыдущее слово.
70
-     *
71
-     * @param int $shift Смещение
72
-     *
73
-     * @return Word|null
74
-     */
75
-    protected function getPreviousWord($shift = 1)
76
-    {
77
-        return $this->getText()->offsetGet($this->getText()->key() + $shift * -1);
78
-    }
68
+	/**
69
+	 * Возвращает предыдущее слово.
70
+	 *
71
+	 * @param int $shift Смещение
72
+	 *
73
+	 * @return Word|null
74
+	 */
75
+	protected function getPreviousWord($shift = 1)
76
+	{
77
+		return $this->getText()->offsetGet($this->getText()->key() + $shift * -1);
78
+	}
79 79
 
80
-    /**
81
-     * Возвращает следующее слово.
82
-     *
83
-     * @param int $shift Смещение
84
-     *
85
-     * @return Word|null
86
-     */
87
-    protected function getNextWord($shift = 1)
88
-    {
89
-        return $this->getText()->offsetGet($this->getText()->key() + $shift);
90
-    }
80
+	/**
81
+	 * Возвращает следующее слово.
82
+	 *
83
+	 * @param int $shift Смещение
84
+	 *
85
+	 * @return Word|null
86
+	 */
87
+	protected function getNextWord($shift = 1)
88
+	{
89
+		return $this->getText()->offsetGet($this->getText()->key() + $shift);
90
+	}
91 91
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
      */
75 75
     protected function getPreviousWord($shift = 1)
76 76
     {
77
-        return $this->getText()->offsetGet($this->getText()->key() + $shift * -1);
77
+        return $this->getText()->offsetGet($this->getText()->key()+$shift*-1);
78 78
     }
79 79
 
80 80
     /**
@@ -86,6 +86,6 @@  discard block
 block discarded – undo
86 86
      */
87 87
     protected function getNextWord($shift = 1)
88 88
     {
89
-        return $this->getText()->offsetGet($this->getText()->key() + $shift);
89
+        return $this->getText()->offsetGet($this->getText()->key()+$shift);
90 90
     }
91 91
 }
Please login to merge, or discard this patch.
AnalyzerText/Text.php 1 patch
Indentation   +74 added lines, -74 removed lines patch added patch discarded remove patch
@@ -18,85 +18,85 @@
 block discarded – undo
18 18
  */
19 19
 class Text extends \ArrayIterator
20 20
 {
21
-    /**
22
-     * Спиок всех слов в тексте в простой форме.
23
-     *
24
-     * @var array
25
-     */
26
-    protected $plains = array();
21
+	/**
22
+	 * Спиок всех слов в тексте в простой форме.
23
+	 *
24
+	 * @var array
25
+	 */
26
+	protected $plains = array();
27 27
 
28
-    /**
29
-     * @param string $text
30
-     */
31
-    public function __construct($text)
32
-    {
33
-        $words = array();
34
-        // слово не может начинаться с тире и не может содержать только его
35
-        if (preg_match_all('/[[:alnum:]]+(?:[-\'][[:alnum:]]+)*/u', trim(strip_tags($text)), $match)) {
36
-            $words = $match[0];
37
-            // получение списка слов в нижнем регистре
38
-            $this->plains = explode(' ', mb_strtolower(implode(' ', $words), 'utf8'));
39
-        }
40
-        parent::__construct($words);
41
-    }
28
+	/**
29
+	 * @param string $text
30
+	 */
31
+	public function __construct($text)
32
+	{
33
+		$words = array();
34
+		// слово не может начинаться с тире и не может содержать только его
35
+		if (preg_match_all('/[[:alnum:]]+(?:[-\'][[:alnum:]]+)*/u', trim(strip_tags($text)), $match)) {
36
+			$words = $match[0];
37
+			// получение списка слов в нижнем регистре
38
+			$this->plains = explode(' ', mb_strtolower(implode(' ', $words), 'utf8'));
39
+		}
40
+		parent::__construct($words);
41
+	}
42 42
 
43
-    /**
44
-     * Возвращает список слов.
45
-     *
46
-     * @return array
47
-     */
48
-    public function getWords()
49
-    {
50
-        return $this->getArrayCopy();
51
-    }
43
+	/**
44
+	 * Возвращает список слов.
45
+	 *
46
+	 * @return array
47
+	 */
48
+	public function getWords()
49
+	{
50
+		return $this->getArrayCopy();
51
+	}
52 52
 
53
-    /**
54
-     * Возвращает текущий элемент
55
-     *
56
-     * @return Word|null
57
-     */
58
-    public function current()
59
-    {
60
-        return parent::current() ? new Word(parent::current(), $this->plains[$this->key()]) : null;
61
-    }
53
+	/**
54
+	 * Возвращает текущий элемент
55
+	 *
56
+	 * @return Word|null
57
+	 */
58
+	public function current()
59
+	{
60
+		return parent::current() ? new Word(parent::current(), $this->plains[$this->key()]) : null;
61
+	}
62 62
 
63
-    /**
64
-     * @param int $index
65
-     *
66
-     * @return Word|null
67
-     */
68
-    public function offsetGet($index)
69
-    {
70
-        return $this->offsetExists($index) ? new Word(parent::offsetGet($index), $this->plains[$index]) : null;
71
-    }
63
+	/**
64
+	 * @param int $index
65
+	 *
66
+	 * @return Word|null
67
+	 */
68
+	public function offsetGet($index)
69
+	{
70
+		return $this->offsetExists($index) ? new Word(parent::offsetGet($index), $this->plains[$index]) : null;
71
+	}
72 72
 
73
-    /**
74
-     * Удаляет слово из текста.
75
-     */
76
-    public function remove()
77
-    {
78
-        $this->offsetUnset($this->key());
79
-        unset($this->plains[$this->key()]);
80
-    }
73
+	/**
74
+	 * Удаляет слово из текста.
75
+	 */
76
+	public function remove()
77
+	{
78
+		$this->offsetUnset($this->key());
79
+		unset($this->plains[$this->key()]);
80
+	}
81 81
 
82
-    /**
83
-     * Заменяет слово в тексте.
84
-     *
85
-     * @param Word $word
86
-     */
87
-    public function replace(Word $word)
88
-    {
89
-        $this->offsetSet($this->key(), $word->getWord());
90
-        $this->plains[$this->key()] = $word->getPlain();
91
-    }
82
+	/**
83
+	 * Заменяет слово в тексте.
84
+	 *
85
+	 * @param Word $word
86
+	 */
87
+	public function replace(Word $word)
88
+	{
89
+		$this->offsetSet($this->key(), $word->getWord());
90
+		$this->plains[$this->key()] = $word->getPlain();
91
+	}
92 92
 
93
-    /**
94
-     * Возвращает текст
95
-     *
96
-     * @return string
97
-     */
98
-    public function __toString()
99
-    {
100
-        return implode(' ', $this->getWords());
101
-    }
93
+	/**
94
+	 * Возвращает текст
95
+	 *
96
+	 * @return string
97
+	 */
98
+	public function __toString()
99
+	{
100
+		return implode(' ', $this->getWords());
101
+	}
102 102
 }
Please login to merge, or discard this patch.