or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

classification.mddistance.mdindex.mdngrams-tfidf.mdphonetics.mdpos-tagging.mdsentiment.mdtext-processing.mdtransliterators.mdutilities.mdwordnet.md

utilities.mddocs/

0

# Utilities

1

2

Additional utility classes and functions for specialized data structures, storage, and graph algorithms.

3

4

## Capabilities

5

6

### Data Structures

7

8

#### Trie

9

10

A trie data structure for efficient string storage and retrieval.

11

12

```javascript { .api }

13

/**

14

* Trie data structure for string storage

15

*/

16

class Trie {

17

constructor();

18

19

/** Add a string to the trie */

20

addString(string: string): void;

21

22

/** Check if string exists in trie */

23

contains(string: string): boolean;

24

25

/** Find all strings with given prefix */

26

findPrefix(prefix: string): string[];

27

28

/** Get all strings in trie */

29

allPrefixes(): string[];

30

}

31

```

32

33

### Graph Algorithms

34

35

#### Shortest Path Tree

36

37

Implementation of shortest path tree algorithms for graph traversal.

38

39

```javascript { .api }

40

/**

41

* Shortest path tree implementation

42

*/

43

class ShortestPathTree {

44

constructor(graph: EdgeWeightedDigraph, source: number);

45

46

/** Get distance to vertex */

47

distTo(vertex: number): number;

48

49

/** Check if path exists to vertex */

50

hasPathTo(vertex: number): boolean;

51

52

/** Get path to vertex */

53

pathTo(vertex: number): DirectedEdge[];

54

}

55

```

56

57

#### Longest Path Tree

58

59

Implementation of longest path algorithms for DAGs.

60

61

```javascript { .api }

62

/**

63

* Longest path tree implementation

64

*/

65

class LongestPathTree {

66

constructor(graph: EdgeWeightedDigraph, source: number);

67

68

/** Get distance to vertex */

69

distTo(vertex: number): number;

70

71

/** Check if path exists to vertex */

72

hasPathTo(vertex: number): boolean;

73

74

/** Get path to vertex */

75

pathTo(vertex: number): DirectedEdge[];

76

}

77

```

78

79

#### Graph Data Structures

80

81

Supporting classes for graph algorithms.

82

83

```javascript { .api }

84

/**

85

* Directed edge with weight

86

*/

87

class DirectedEdge {

88

constructor(from: number, to: number, weight: number);

89

90

/** Get source vertex */

91

from(): number;

92

93

/** Get destination vertex */

94

to(): number;

95

96

/** Get edge weight */

97

weight(): number;

98

}

99

100

/**

101

* Edge-weighted directed graph

102

*/

103

class EdgeWeightedDigraph {

104

constructor(vertices: number);

105

106

/** Add directed edge */

107

addEdge(edge: DirectedEdge): void;

108

109

/** Get edges from vertex */

110

adj(vertex: number): DirectedEdge[];

111

112

/** Get all edges */

113

edges(): DirectedEdge[];

114

115

/** Get vertex count */

116

V(): number;

117

118

/** Get edge count */

119

E(): number;

120

}

121

122

/**

123

* Topological sort for DAGs

124

*/

125

class Topological {

126

constructor(graph: EdgeWeightedDigraph);

127

128

/** Get topological order */

129

order(): number[];

130

131

/** Check if graph is DAG */

132

isDAG(): boolean;

133

134

/** Check if vertex comes before another in topological order */

135

rank(vertex: number): number;

136

}

137

```

138

139

### Storage Backend

140

141

Generic storage interface for classifiers and other persistent data.

142

143

```javascript { .api }

144

/**

145

* Storage backend interface

146

*/

147

class StorageBackend {

148

constructor();

149

150

/** Store data with key */

151

put(key: string, data: any): void;

152

153

/** Retrieve data by key */

154

get(key: string): any;

155

156

/** Check if key exists */

157

exists(key: string): boolean;

158

159

/** Delete data by key */

160

delete(key: string): void;

161

162

/** Get all keys */

163

keys(): string[];

164

}

165

```

166

167

### Text Resources

168

169

Common text processing resources and utilities.

170

171

```javascript { .api }

172

/**

173

* English stopwords list

174

*/

175

const stopwords: string[];

176

177

/**

178

* Known abbreviations for sentence tokenization

179

*/

180

const abbreviations: string[];

181

```

182

183

### Spell Checking

184

185

Basic spell checking functionality.

186

187

```javascript { .api }

188

/**

189

* Spell checker with correction suggestions

190

*/

191

class Spellcheck {

192

constructor();

193

194

/** Check if word is spelled correctly */

195

isCorrect(word: string): boolean;

196

197

/** Get spelling suggestions for word */

198

getCorrections(word: string): string[];

199

}

200

```

201

202

## Usage Examples

203

204

### Trie Operations

205

206

```javascript

207

const { Trie } = require('natural');

208

209

const trie = new Trie();

210

211

// Add words

212

trie.addString('hello');

213

trie.addString('help');

214

trie.addString('helicopter');

215

216

// Check existence

217

console.log(trie.contains('hello')); // true

218

console.log(trie.contains('world')); // false

219

220

// Find with prefix

221

const words = trie.findPrefix('hel');

222

console.log(words); // ['hello', 'help', 'helicopter']

223

```

224

225

### Graph Algorithms

226

227

```javascript

228

const { EdgeWeightedDigraph, DirectedEdge, ShortestPathTree } = require('natural');

229

230

// Create graph

231

const graph = new EdgeWeightedDigraph(5);

232

233

// Add edges

234

graph.addEdge(new DirectedEdge(0, 1, 2.0));

235

graph.addEdge(new DirectedEdge(0, 2, 1.0));

236

graph.addEdge(new DirectedEdge(1, 3, 3.0));

237

graph.addEdge(new DirectedEdge(2, 3, 1.0));

238

239

// Find shortest paths

240

const spt = new ShortestPathTree(graph, 0);

241

console.log(spt.distTo(3)); // Shortest distance to vertex 3

242

console.log(spt.pathTo(3)); // Path to vertex 3

243

```

244

245

### Storage Backend

246

247

```javascript

248

const { StorageBackend } = require('natural');

249

250

const storage = new StorageBackend();

251

252

// Store data

253

storage.put('classifier_data', {

254

vocabulary: ['word1', 'word2'],

255

labels: ['positive', 'negative']

256

});

257

258

// Retrieve data

259

const data = storage.get('classifier_data');

260

console.log(data.vocabulary);

261

262

// Check existence

263

if (storage.exists('classifier_data')) {

264

console.log('Data exists');

265

}

266

```

267

268

### Spell Checking

269

270

```javascript

271

const { Spellcheck } = require('natural');

272

273

const spellcheck = new Spellcheck();

274

275

// Check spelling

276

console.log(spellcheck.isCorrect('hello')); // true

277

console.log(spellcheck.isCorrect('helo')); // false

278

279

// Get corrections

280

const corrections = spellcheck.getCorrections('helo');

281

console.log(corrections); // ['hello', 'help', ...]

282

```