forked from DaveChild/Text-Statistics
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathText.php
More file actions
274 lines (238 loc) · 8.95 KB
/
Text.php
File metadata and controls
274 lines (238 loc) · 8.95 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
namespace DaveChild\TextStatistics;
class Text
{
/**
* @var array $clean Efficiency: Store strings once processed.
*/
protected static $clean = array();
/**
* Trims, removes line breaks, multiple spaces and generally cleans text
* before processing.
* @param string|boolean $strText Text to be transformed
* @return string
*/
public static function cleanText($strText)
{
// Check for boolean before processing as string
if (is_bool($strText) || is_null($strText)) {
return '';
}
// Check to see if we already processed this text. If we did, don't
// re-process it.
$key = sha1($strText);
if (isset(self::$clean[$key])) {
return self::$clean[$key];
}
$strText = mb_convert_encoding($strText, 'UTF-8', 'ISO-8859-1');
// Curly quotes etc
$strText = str_replace(
array(
"\xe2\x80\x98",
"\xe2\x80\x99",
"\xe2\x80\x9c",
"\xe2\x80\x9d",
"\xe2\x80\x93",
"\xe2\x80\x94",
"\xe2\x80\xa6"
),
array(
"'",
"'",
'"',
'"',
'-',
'--',
'...'
),
$strText
);
$strText = str_replace(
array(
chr(145),
chr(146),
chr(147),
chr(148),
chr(150),
chr(151),
chr(133)
),
array(
"'",
"'",
'"',
'"',
'-',
'--',
'...'
),
$strText
);
// Replace periods within numbers
$strText = preg_replace('`([^0-9][0-9]+)\.([0-9]+[^0-9])`mis', '${1}0$2', $strText);
// Handle HTML. Treat block level elements as sentence terminators and
// remove all other tags.
$strText = preg_replace('`<script(.*?)>(.*?)</script>`is', '', $strText);
$strText = preg_replace('`\</?(address|blockquote|center|dir|div|dl|dd|dt|fieldset|form|h1|h2|h3|h4|h5|h6|menu|noscript|ol|p|pre|table|ul|li)[^>]*>`is', '.', $strText);
$strText = html_entity_decode($strText);
$strText = strip_tags($strText);
// Assume blank lines (i.e., paragraph breaks) end sentences (useful
// for titles in plain text documents) and replace remaining new
// lines with spaces
$strText = preg_replace('`(\r\n|\n\r)`is', "\n", $strText);
$strText = preg_replace('`(\r|\n){2,}`is', ".\n\n", $strText);
$strText = preg_replace('`[ ]*(\n|\r\n|\r)[ ]*`', ' ', $strText);
// Replace commas, hyphens, quotes etc (count as spaces)
$strText = preg_replace('`[",:;()/\`-]`', ' ', $strText);
// Unify terminators and spaces
$strText = trim($strText, '. ') . '.'; // Add final terminator.
$strText = preg_replace('`[\.!?]`', '.', $strText); // Unify terminators
$strText = preg_replace('`([\.\s]*\.[\.\s]*)`mis', '. ', $strText); // Merge terminators separated by whitespace.
$strText = preg_replace('`[ ]+`', ' ', $strText); // Remove multiple spaces
$strText = preg_replace('`([\.])[\. ]+`', '$1', $strText); // Check for duplicated terminators
$strText = trim(preg_replace('`[ ]*([\.])`', '$1 ', $strText)); // Pad sentence terminators
// Lower case all words following terminators (for gunning fog score)
$strText = preg_replace_callback('`\. [^\. ]`', function($matches) { return strtolower($matches[0]); }, $strText);
$strText = trim($strText);
// Cache it and return
self::$clean[$key] = $strText;
return $strText;
}
/**
* Converts string to lower case. Tries mb_strtolower and if that fails uses regular strtolower.
* @param string $strText Text to be transformed
* @param string $strEncoding Encoding of text
* @return string
*/
public static function lowerCase($strText, $strEncoding = '')
{
if ($strEncoding == '') {
$strLowerCaseText = mb_strtolower($strText);
} else {
$strLowerCaseText = mb_strtolower($strText, $strEncoding);
}
return $strLowerCaseText;
}
/**
* Converts string to upper case. Tries mb_strtoupper and if that fails uses regular strtoupper.
* @param string $strText Text to be transformed
* @param string $strEncoding Encoding of text
* @return string
*/
public static function upperCase($strText, $strEncoding = '')
{
if ($strEncoding == '') {
$strUpperCaseText = mb_strtoupper($strText);
} else {
$strUpperCaseText = mb_strtoupper($strText, $strEncoding);
}
return $strUpperCaseText;
}
/**
* Gets portion of string. Tries mb_substr and if that fails uses regular substr.
* @param string $strText Text to be cut up
* @param int $intStart Start character
* @param int $intLength Length
* @param string $strEncoding Encoding of text
* @return string
*/
public static function substring($strText, $intStart, $intLength, $strEncoding = '')
{
if ($strEncoding == '') {
$strSubstring = mb_substr($strText, $intStart, $intLength);
} else {
$strSubstring = mb_substr($strText, $intStart, $intLength, $strEncoding);
}
return $strSubstring;
}
/**
* Gives string length. Tries mb_strlen and if that fails uses regular strlen.
* @param string $strText Text to be measured
* @param string $strEncoding Encoding of text
* @return int
*/
public static function textLength($strText, $strEncoding = '')
{
if ($strEncoding == '') {
$intTextLength = mb_strlen($strText);
} else {
$intTextLength = mb_strlen($strText, $strEncoding);
}
return $intTextLength;
}
/**
* Alias for textLength, as "letterCount", "wordCount" etc also used
* @param string $strText Text to be measured
* @param string $strEncoding Encoding of text
* @return int
*/
public static function characterCount($strText, $strEncoding = '')
{
return self::textLength($strText, $strEncoding);
}
/**
* Gives letter count (ignores all non-letters). Tries mb_strlen and if
* that fails uses regular strlen.
* @param string $strText Text to be measured
* @param string $strEncoding Encoding of text
* @return int
*/
public static function letterCount($strText, $strEncoding = '')
{
if (strlen(trim($strText)) == 0) {
return 0;
}
$strText = self::cleanText($strText); // To clear out newlines etc
$intTextLength = 0;
$strText = preg_replace('`[^A-Za-z]+`', '', $strText);
if ($strEncoding == '') {
$intTextLength = mb_strlen($strText);
} else {
$intTextLength = mb_strlen($strText, $strEncoding);
}
return $intTextLength;
}
/**
* Returns word count for text.
* @param string $strText Text to be measured
* @param string $strEncoding Encoding of text
* @return int
*/
public static function wordCount($strText, $strEncoding = '')
{
if (strlen(trim($strText)) == 0) {
return 0;
}
// Will be tripped by em dashes with spaces either side, among other similar characters
$intWords = 1 + self::textLength(preg_replace('`[^ ]`', '', preg_replace('`\s+`', ' ', $strText)), $strEncoding); // Space count + 1 is word count
return $intWords;
}
/**
* Returns sentence count for text.
* @param string $strText Text to be measured
* @param string $strEncoding Encoding of text
* @return int
*/
public static function sentenceCount($strText, $strEncoding = '')
{
if (strlen(trim($strText)) == 0) {
return 0;
}
// Will be tripped up by "Mr." or "U.K.". Not a major concern at this point.
$intSentences = max(1, self::textLength(preg_replace('`[^\.!?]`', '', $strText), $strEncoding));
return $intSentences;
}
/**
* Returns average words per sentence for text.
* @param string $strText Text to be measured
* @param string $strEncoding Encoding of text
* @return int|float
*/
public static function averageWordsPerSentence($strText, $strEncoding = '')
{
$intSentenceCount = self::sentenceCount($strText, $strEncoding);
$intWordCount = self::wordCount($strText, $strEncoding);
$averageWords = (Maths::bcCalc($intWordCount, '/', $intSentenceCount));
return $averageWords;
}
}