@@ -48,6 +48,11 @@ class Syllable {
48
48
private static $ cache_dir = null ;
49
49
private static $ language_dir = null ;
50
50
51
+ /**
52
+ * Create a new Syllable class, with defaults
53
+ * @param string $language
54
+ * @param string|Syllable_Hyphen_Interface $hyphen
55
+ */
51
56
public function __construct ($ language = 'en ' , $ hyphen = null ) {
52
57
if (!self ::$ cache_dir ) {
53
58
self ::$ cache_dir = __DIR__ .'/cache ' ;
@@ -67,21 +72,35 @@ public function __construct($language = 'en', $hyphen = null) {
67
72
$ this ->setHyphen ($ hyphen ? $ hyphen : new Syllable_Hyphen_Soft ());
68
73
}
69
74
75
+ /**
76
+ * Set the directory where compiled language files may be stored.
77
+ * Default to the `cache` subdirectory of the current directory.
78
+ * @param string $dir
79
+ */
70
80
public static function setCacheDir ($ dir ) {
71
81
self ::$ cache_dir = $ dir ;
72
82
}
73
-
83
+
84
+ /**
85
+ * Set the directory where language source files can be found.
86
+ * Default to the `languages` subdirectory of the current directory.
87
+ * @param string $dir
88
+ */
74
89
public static function setLanguageDir ($ dir ) {
75
90
self ::$ language_dir = $ dir ;
76
91
}
77
-
92
+
93
+ /**
94
+ * Set the language whose rules will be used for hyphenation.
95
+ * @param string $language
96
+ */
78
97
public function setLanguage ($ language ) {
79
98
$ this ->language = $ language ;
80
99
$ this ->setSource (new Syllable_Source_File ($ language , self ::$ language_dir ));
81
100
}
82
101
83
102
/**
84
- * Set the hyphen to use when hyphenating text
103
+ * Set the hyphen text or object to use as a hyphen marker.
85
104
* @param Mixed $hyphen either a Syllable_Hyphen_Interface or a string, which is turned into a Syllable_Hyphen_Text
86
105
*/
87
106
public function setHyphen ($ hyphen ) {
@@ -91,7 +110,7 @@ public function setHyphen($hyphen) {
91
110
}
92
111
93
112
/**
94
- *
113
+ * Get the current hyphen object.
95
114
* @return Syllable_Hyphen_Interface hyphen
96
115
*/
97
116
public function getHyphen () {
@@ -147,6 +166,11 @@ public function getSource() {
147
166
return $ this ->Source ;
148
167
}
149
168
169
+ /**
170
+ * Split a single word on where the hyphenation would go.
171
+ * @param string $text
172
+ * @return array
173
+ */
150
174
public function splitWord ($ word ) {
151
175
mb_internal_encoding ('UTF-8 ' ); //@todo upwards?
152
176
mb_regex_encoding ('UTF-8 ' ); //@todo upwards?
@@ -156,6 +180,11 @@ public function splitWord($word) {
156
180
return $ this ->parseWord ($ word );
157
181
}
158
182
183
+ /**
184
+ * Split a text on where the hyphenation would go.
185
+ * @param string $text
186
+ * @return array
187
+ */
159
188
public function splitText ($ text ) {
160
189
mb_internal_encoding ('UTF-8 ' ); //@todo upwards?
161
190
mb_regex_encoding ('UTF-8 ' ); //@todo upwards?
@@ -195,16 +224,32 @@ public function splitText($text) {
195
224
return $ parts ;
196
225
}
197
226
227
+ /**
228
+ * Hyphenate a single word.
229
+ * @param string $html
230
+ * @return string
231
+ */
198
232
public function hyphenateWord ($ word ) {
199
233
$ parts = $ this ->splitWord ($ word );
200
234
return $ this ->Hyphen ->joinText ($ parts );
201
235
}
202
236
237
+ /**
238
+ * Hyphenate all words in the plain text.
239
+ * @param string $html
240
+ * @return string
241
+ */
203
242
public function hyphenateText ($ text ) {
204
243
$ parts = $ this ->splitText ($ text );
205
244
return $ this ->Hyphen ->joinText ($ parts );
206
245
}
207
246
247
+ /**
248
+ * Hyphenate all readable text in the HTML, excluding HTML tags and
249
+ * attributes.
250
+ * @param string $html
251
+ * @return string
252
+ */
208
253
public function hyphenateHtml ($ html ) {
209
254
$ dom = new DOMDocument ();
210
255
$ dom ->resolveExternals = true ;
@@ -215,6 +260,10 @@ public function hyphenateHtml($html) {
215
260
return $ dom ->saveHTML ();
216
261
}
217
262
263
+ /**
264
+ * Add hyphenation to the DOM nodes.
265
+ * @param DOMNode $node
266
+ */
218
267
private function hyphenateHtmlDom (DOMNode $ node ) {
219
268
if ($ node ->hasChildNodes ()) {
220
269
foreach ($ node ->childNodes as $ child ) {
@@ -227,7 +276,14 @@ private function hyphenateHtmlDom(DOMNode $node) {
227
276
$ this ->Hyphen ->joinHtmlDom ($ parts , $ node );
228
277
}
229
278
}
230
-
279
+
280
+ /**
281
+ * Count the number of syllables in the text and return a map with
282
+ * syllable count as key and number of words for that syllable count as
283
+ * the value.
284
+ * @param string $text
285
+ * @return array
286
+ */
231
287
public function histogramText ($ text ) {
232
288
mb_internal_encoding ('UTF-8 ' ); //@todo upwards?
233
289
mb_regex_encoding ('UTF-8 ' ); //@todo upwards?
@@ -248,7 +304,12 @@ public function histogramText($text) {
248
304
249
305
return $ counts ;
250
306
}
251
-
307
+
308
+ /**
309
+ * Count the number of words in the text.
310
+ * @param string $text
311
+ * @return int
312
+ */
252
313
public function countWordsText ($ text ) {
253
314
mb_internal_encoding ('UTF-8 ' ); //@todo upwards?
254
315
mb_regex_encoding ('UTF-8 ' ); //@todo upwards?
@@ -264,7 +325,12 @@ public function countWordsText($text) {
264
325
265
326
return $ count ;
266
327
}
267
-
328
+
329
+ /**
330
+ * Count the number of polysyllables in the text.
331
+ * @param string $text
332
+ * @return int
333
+ */
268
334
public function countPolysyllablesText ($ text ) {
269
335
mb_internal_encoding ('UTF-8 ' ); //@todo upwards?
270
336
mb_regex_encoding ('UTF-8 ' ); //@todo upwards?
0 commit comments