0
# Sentence Processing
1
2
Single sentence analysis including grammatical transformations, pattern matching, and entity extraction. The Sentence class provides detailed analysis and manipulation of individual sentences.
3
4
## Capabilities
5
6
### Sentence Class
7
8
Core sentence object with term-level operations and grammatical analysis.
9
10
```javascript { .api }
11
/**
12
* Sentence class for single sentence analysis and transformation
13
*/
14
class Sentence {
15
/** Array of terms in the sentence */
16
terms: Term[];
17
/** Original sentence string */
18
str: string;
19
/** Contraction expansion/contraction methods */
20
contractions: Contractions;
21
}
22
```
23
24
### Sentence Output Methods
25
26
Methods for retrieving processed sentence text in different formats.
27
28
```javascript { .api }
29
/**
30
* Get formatted sentence text with original spacing
31
* @returns {string} Reconstructed sentence text
32
*/
33
text(): string;
34
35
/**
36
* Get normalized sentence with cleaned whitespace
37
* @returns {string} Normalized sentence text
38
*/
39
normal(): string;
40
41
/**
42
* Get root/lemmatized form of all words in sentence
43
* @returns {string} Root form sentence
44
*/
45
root(): string;
46
47
/**
48
* Get part-of-speech tags for all terms in sentence
49
* @returns {string[]} Array of POS tags
50
*/
51
tags(): string[];
52
53
/**
54
* Get the primary tag for the sentence (from main verb or subject)
55
* @returns {Term[]} Array of primary terms
56
*/
57
tag(): Term[];
58
```
59
60
**Usage Examples:**
61
62
```javascript
63
const nlp = require('nlp_compromise');
64
65
const sent = nlp.sentence('She walks quickly to school.');
66
67
console.log(sent.text()); // 'She walks quickly to school.'
68
console.log(sent.normal()); // 'she walks quickly to school'
69
console.log(sent.root()); // 'she walk quickly to school'
70
console.log(sent.tags()); // ['Pronoun', 'Verb', 'Adverb', 'Preposition', 'Noun']
71
```
72
73
### Sentence Analysis
74
75
Methods for analyzing sentence structure and properties.
76
77
```javascript { .api }
78
/**
79
* Determine the type of sentence
80
* @returns {string} Sentence type: 'declarative', 'interrogative', or 'exclamative'
81
*/
82
sentence_type(): string;
83
84
/**
85
* Get the sentence terminator punctuation
86
* @returns {string} Terminator: '.', '?', or '!'
87
*/
88
terminator(): string;
89
90
/**
91
* Check if sentence is in passive voice
92
* @returns {boolean} True if passive voice
93
*/
94
is_passive(): boolean;
95
```
96
97
**Usage Examples:**
98
99
```javascript
100
const stmt = nlp.sentence('She goes to school.');
101
console.log(stmt.sentence_type()); // 'declarative'
102
console.log(stmt.terminator()); // '.'
103
104
const quest = nlp.sentence('Where does she go?');
105
console.log(quest.sentence_type()); // 'interrogative'
106
console.log(quest.terminator()); // '?'
107
108
const passive = nlp.sentence('The ball was thrown by him.');
109
console.log(passive.is_passive()); // true
110
```
111
112
### Pattern Matching
113
114
Advanced pattern matching within the sentence with grammatical pattern support.
115
116
```javascript { .api }
117
/**
118
* Find patterns within the sentence
119
* @param {string} pattern - Search pattern (supports grammatical patterns)
120
* @param {object} [options] - Matching options
121
* @returns {Result[]} Array of matching results
122
*/
123
match(pattern, options): Result[];
124
125
/**
126
* Replace patterns within the sentence
127
* @param {string} pattern - Pattern to replace
128
* @param {string} replacement - Replacement text
129
* @param {object} [options] - Replacement options
130
* @returns {Sentence} Modified sentence object
131
*/
132
replace(pattern, replacement, options): Sentence;
133
```
134
135
**Usage Examples:**
136
137
```javascript
138
const sent = nlp.sentence('The big red car drove fast.');
139
140
// Find adjective patterns
141
const adjectives = sent.match('[Adjective]');
142
console.log(adjectives.map(a => a.text())); // ['big', 'red']
143
144
// Find adjective-noun patterns
145
const adjNoun = sent.match('[Adjective] [Noun]');
146
console.log(adjNoun[0].text()); // 'red car'
147
148
// Replace patterns
149
sent.replace('[Adjective] car', 'blue truck');
150
console.log(sent.text()); // 'The big blue truck drove fast.'
151
```
152
153
### Sentence Editing
154
155
Methods for programmatically modifying sentence structure.
156
157
```javascript { .api }
158
/**
159
* Insert a new term before the specified index
160
* @param {number} index - Position to insert before
161
* @param {string} str - Text to insert
162
*/
163
addBefore(index, str): void;
164
165
/**
166
* Insert a new term after the specified index
167
* @param {number} index - Position to insert after
168
* @param {string} str - Text to insert
169
*/
170
addAfter(index, str): void;
171
```
172
173
**Usage Examples:**
174
175
```javascript
176
const sent = nlp.sentence('She walks to school.');
177
178
// Add word before index 1 (before 'walks')
179
sent.addBefore(1, 'always');
180
console.log(sent.text()); // 'She always walks to school.'
181
182
// Add word after index 2 (after 'walks')
183
sent.addAfter(2, 'slowly');
184
console.log(sent.text()); // 'She always walks slowly to school.'
185
```
186
187
### Grammatical Transformations
188
189
Sentence-level transformations for tense and negation.
190
191
```javascript { .api }
192
/**
193
* Convert all verbs in sentence to past tense
194
* @returns {Sentence} Sentence with past tense verbs
195
*/
196
to_past(): Sentence;
197
198
/**
199
* Convert all verbs in sentence to present tense
200
* @returns {Sentence} Sentence with present tense verbs
201
*/
202
to_present(): Sentence;
203
204
/**
205
* Convert all verbs in sentence to future tense
206
* @returns {Sentence} Sentence with future tense verbs
207
*/
208
to_future(): Sentence;
209
210
/**
211
* Negate the sentence
212
* @returns {Sentence} Negated sentence
213
*/
214
negate(): Sentence;
215
```
216
217
**Usage Examples:**
218
219
```javascript
220
const sent = nlp.sentence('She walks to school.');
221
222
console.log(sent.to_past().text()); // 'She walked to school.'
223
console.log(sent.to_future().text()); // 'She will walk to school.'
224
console.log(sent.negate().text()); // 'She doesn\'t walk to school.'
225
226
// Chain transformations
227
console.log(sent.to_past().negate().text()); // 'She didn\'t walk to school.'
228
```
229
230
### Entity Extraction
231
232
Extract entities from the sentence with the same methods as Text class.
233
234
```javascript { .api }
235
/**
236
* Extract person entities from sentence
237
* @returns {Person[]} Array of Person objects
238
*/
239
people(): Person[];
240
241
/**
242
* Extract place entities from sentence
243
* @returns {Place[]} Array of Place objects
244
*/
245
places(): Place[];
246
247
/**
248
* Extract organization entities from sentence
249
* @returns {Organization[]} Array of Organization objects
250
*/
251
organizations(): Organization[];
252
253
/**
254
* Extract date entities from sentence
255
* @returns {Date[]} Array of Date objects
256
*/
257
dates(): Date[];
258
259
/**
260
* Extract numeric values from sentence
261
* @returns {Value[]} Array of Value objects
262
*/
263
values(): Value[];
264
265
/**
266
* Extract all nouns from sentence
267
* @returns {Noun[]} Array of Noun objects
268
*/
269
nouns(): Noun[];
270
271
/**
272
* Extract all adjectives from sentence
273
* @returns {Adjective[]} Array of Adjective objects
274
*/
275
adjectives(): Adjective[];
276
277
/**
278
* Extract all verbs from sentence
279
* @returns {Verb[]} Array of Verb objects
280
*/
281
verbs(): Verb[];
282
283
/**
284
* Extract all adverbs from sentence
285
* @returns {Adverb[]} Array of Adverb objects
286
*/
287
adverbs(): Adverb[];
288
289
/**
290
* Extract topics and keywords from sentence with frequency counts
291
* @returns {Topic[]} Array of Topic objects with counts
292
*/
293
topics(): Topic[];
294
```
295
296
**Usage Examples:**
297
298
```javascript
299
const sent = nlp.sentence('Tony Hawk visited Apple Inc. in Cupertino yesterday.');
300
301
const people = sent.people();
302
console.log(people[0].text); // 'Tony Hawk'
303
304
const places = sent.places();
305
console.log(places[0].text); // 'Cupertino'
306
307
const orgs = sent.organizations();
308
console.log(orgs[0].text); // 'Apple Inc.'
309
310
const nouns = sent.nouns();
311
console.log(nouns.map(n => n.text)); // ['Tony Hawk', 'Apple Inc.', 'Cupertino']
312
313
const topics = sent.topics();
314
console.log(topics.map(t => t.text)); // ['Tony Hawk', 'Apple Inc.', 'Cupertino']
315
```
316
317
### Conditional Processing
318
319
Methods for removing conditional clauses and extracting core sentence meaning.
320
321
```javascript { .api }
322
/**
323
* Remove conditional clauses from sentence
324
* @returns {Sentence} Sentence with conditions stripped
325
*/
326
strip_conditions(): Sentence;
327
```
328
329
**Usage Examples:**
330
331
```javascript
332
const sent = nlp.sentence('If it rains, she will stay home.');
333
console.log(sent.strip_conditions().text()); // 'she will stay home'
334
```
335
336
### Question Class
337
338
Extended sentence class specifically for interrogative sentences.
339
340
```javascript { .api }
341
/**
342
* Question class extending Sentence for interrogative analysis
343
*/
344
class Question extends Sentence {
345
/**
346
* Determine the type of question
347
* @returns {string} Question type: 'how', 'when', 'where', 'who', 'why', 'what', 'which', 'number', 'yesNo'
348
*/
349
from(): string;
350
}
351
```
352
353
**Usage Examples:**
354
355
```javascript
356
const question = nlp.question('Where did she go yesterday?');
357
console.log(question.from()); // 'where'
358
359
const yesNo = nlp.question('Did she go to school?');
360
console.log(yesNo.from()); // 'yesNo'
361
362
const what = nlp.question('What time is it?');
363
console.log(what.from()); // 'what'
364
```
365
366
### Statement Class
367
368
Extended sentence class specifically for declarative sentences.
369
370
```javascript { .api }
371
/**
372
* Statement class extending Sentence for declarative analysis
373
*/
374
class Statement extends Sentence {
375
// Inherits all Sentence methods with declarative-specific behavior
376
}
377
```