0
# Text Analysis
1
2
Multi-sentence text processing with comprehensive entity extraction, pattern matching, and document-level transformations. The Text class is designed for analyzing documents, paragraphs, or any text containing multiple sentences.
3
4
## Capabilities
5
6
### Text Class
7
8
Container for multiple sentences with document-level operations and entity extraction.
9
10
```javascript { .api }
11
/**
12
* Text class for multi-sentence analysis and transformation
13
*/
14
class Text {
15
/** Array of parsed sentences */
16
sentences: Sentence[];
17
/** Original input text */
18
raw_text: string;
19
/** Contraction expansion/contraction methods */
20
contractions: Contractions;
21
}
22
```
23
24
### Text Output Methods
25
26
Methods for retrieving processed text in different formats.
27
28
```javascript { .api }
29
/**
30
* Get formatted text with original spacing and punctuation
31
* @returns {string} Reconstructed text
32
*/
33
text(): string;
34
35
/**
36
* Get normalized text with cleaned whitespace
37
* @returns {string} Normalized text
38
*/
39
normal(): string;
40
41
/**
42
* Get root/lemmatized form of all words
43
* @returns {string} Root form text
44
*/
45
root(): string;
46
```
47
48
**Usage Examples:**
49
50
```javascript
51
const nlp = require('nlp_compromise');
52
53
const doc = nlp.text('She walks quickly. He ran slowly.');
54
55
console.log(doc.text()); // 'She walks quickly. He ran slowly.'
56
console.log(doc.normal()); // 'she walks quickly he ran slowly'
57
console.log(doc.root()); // 'she walk quickly he run slowly'
58
```
59
60
### Term and Tag Access
61
62
Methods for accessing individual terms and their grammatical tags.
63
64
```javascript { .api }
65
/**
66
* Get all terms from all sentences
67
* @returns {Term[]} Array of all terms
68
*/
69
terms(): Term[];
70
71
/**
72
* Get part-of-speech tags for all sentences
73
* @returns {string[][]} Array of tag arrays for each sentence
74
*/
75
tags(): string[][];
76
```
77
78
**Usage Examples:**
79
80
```javascript
81
const doc = nlp.text('Dogs bark loudly.');
82
const terms = doc.terms();
83
console.log(terms[0].text); // 'Dogs'
84
console.log(terms[0].tag); // 'Plural'
85
86
const tags = doc.tags();
87
console.log(tags[0]); // ['Plural', 'Verb', 'Adverb']
88
```
89
90
### Pattern Matching
91
92
Advanced pattern matching across the entire text with support for grammatical patterns.
93
94
```javascript { .api }
95
/**
96
* Find patterns across all sentences in the text
97
* @param {string} pattern - Search pattern (supports grammatical patterns)
98
* @param {object} [options] - Matching options
99
* @returns {Result[]} Array of matching results
100
*/
101
match(pattern, options): Result[];
102
103
/**
104
* Replace patterns across all sentences
105
* @param {string} pattern - Pattern to replace
106
* @param {string} replacement - Replacement text
107
* @param {object} [options] - Replacement options
108
* @returns {Text} Modified text object
109
*/
110
replace(pattern, replacement, options): Text;
111
```
112
113
**Usage Examples:**
114
115
```javascript
116
const doc = nlp.text('The big dog barked. A small cat meowed.');
117
118
// Find adjective-noun patterns
119
const matches = doc.match('[Adjective] [Noun]');
120
console.log(matches[0].text()); // 'big dog'
121
console.log(matches[1].text()); // 'small cat'
122
123
// Replace patterns
124
doc.replace('[Adjective] dog', 'tiny puppy');
125
console.log(doc.text()); // 'The tiny puppy barked. A small cat meowed.'
126
```
127
128
### Grammatical Transformations
129
130
Document-level transformations that apply to all verbs in the text.
131
132
```javascript { .api }
133
/**
134
* Convert all verbs to past tense
135
* @returns {Text} Text with past tense verbs
136
*/
137
to_past(): Text;
138
139
/**
140
* Convert all verbs to present tense
141
* @returns {Text} Text with present tense verbs
142
*/
143
to_present(): Text;
144
145
/**
146
* Convert all verbs to future tense
147
* @returns {Text} Text with future tense verbs
148
*/
149
to_future(): Text;
150
151
/**
152
* Negate all sentences in the text
153
* @returns {Text} Text with negated sentences
154
*/
155
negate(): Text;
156
```
157
158
**Usage Examples:**
159
160
```javascript
161
const doc = nlp.text('She walks to school. He runs home.');
162
163
console.log(doc.to_past().text()); // 'She walked to school. He ran home.'
164
console.log(doc.to_future().text()); // 'She will walk to school. He will run home.'
165
console.log(doc.negate().text()); // 'She doesn\'t walk to school. He doesn\'t run home.'
166
```
167
168
### Entity Extraction
169
170
Comprehensive entity recognition and extraction across the entire document.
171
172
```javascript { .api }
173
/**
174
* Extract all person entities from text
175
* @returns {Person[]} Array of Person objects
176
*/
177
people(): Person[];
178
179
/**
180
* Extract all place entities from text
181
* @returns {Place[]} Array of Place objects
182
*/
183
places(): Place[];
184
185
/**
186
* Extract all organization entities from text
187
* @returns {Organization[]} Array of Organization objects
188
*/
189
organizations(): Organization[];
190
191
/**
192
* Extract all date entities from text
193
* @returns {Date[]} Array of Date objects
194
*/
195
dates(): Date[];
196
197
/**
198
* Extract all numeric values and measurements
199
* @returns {Value[]} Array of Value objects
200
*/
201
values(): Value[];
202
203
/**
204
* Extract topics and keywords with frequency counts
205
* @returns {Topic[]} Array of Topic objects with counts
206
*/
207
topics(): Topic[];
208
```
209
210
**Usage Examples:**
211
212
```javascript
213
const doc = nlp.text(`
214
Tony Hawk visited Apple Inc. in Cupertino on March 15th.
215
He spent $500 on new skateboard equipment.
216
`);
217
218
const people = doc.people();
219
console.log(people[0].text); // 'Tony Hawk'
220
console.log(people[0].firstName); // 'Tony'
221
222
const places = doc.places();
223
console.log(places[0].text); // 'Cupertino'
224
225
const orgs = doc.organizations();
226
console.log(orgs[0].text); // 'Apple Inc.'
227
228
const dates = doc.dates();
229
console.log(dates[0].text); // 'March 15th'
230
231
const values = doc.values();
232
console.log(values[0].number); // 500
233
console.log(values[0].unit); // 'dollar'
234
```
235
236
### Part-of-Speech Extraction
237
238
Extract all words of specific grammatical categories from the text.
239
240
```javascript { .api }
241
/**
242
* Extract all nouns from text
243
* @returns {Noun[]} Array of Noun objects
244
*/
245
nouns(): Noun[];
246
247
/**
248
* Extract all adjectives from text
249
* @returns {Adjective[]} Array of Adjective objects
250
*/
251
adjectives(): Adjective[];
252
253
/**
254
* Extract all verbs from text
255
* @returns {Verb[]} Array of Verb objects
256
*/
257
verbs(): Verb[];
258
259
/**
260
* Extract all adverbs from text
261
* @returns {Adverb[]} Array of Adverb objects
262
*/
263
adverbs(): Adverb[];
264
```
265
266
**Usage Examples:**
267
268
```javascript
269
const doc = nlp.text('The quick brown fox jumps quickly over the lazy dog.');
270
271
const nouns = doc.nouns();
272
console.log(nouns.map(n => n.text)); // ['fox', 'dog']
273
274
const adjectives = doc.adjectives();
275
console.log(adjectives.map(a => a.text)); // ['quick', 'brown', 'lazy']
276
277
const verbs = doc.verbs();
278
console.log(verbs.map(v => v.text)); // ['jumps']
279
280
const adverbs = doc.adverbs();
281
console.log(adverbs.map(a => a.text)); // ['quickly']
282
```
283
284
### Contractions
285
286
Methods for expanding and contracting text-wide contractions.
287
288
```javascript { .api }
289
/**
290
* Contractions object for expansion/contraction operations
291
*/
292
interface Contractions {
293
/**
294
* Expand all contractions in text (he'd → he would)
295
* @returns {Text} Text with expanded contractions
296
*/
297
expand(): Text;
298
299
/**
300
* Contract appropriate phrases (he would → he'd)
301
* @returns {Text} Text with contracted phrases
302
*/
303
contract(): Text;
304
}
305
```
306
307
**Usage Examples:**
308
309
```javascript
310
const doc = nlp.text("He'd like to go. She's very smart.");
311
312
console.log(doc.contractions.expand().text());
313
// "He would like to go. She is very smart."
314
315
const expanded = nlp.text("He would like to go. She is very smart.");
316
console.log(expanded.contractions.contract().text());
317
// "He'd like to go. She's very smart."
318
```