natural 6.11.1 → 6.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.env CHANGED
@@ -1,12 +1,12 @@
1
1
  # Storage backend you want to use
2
2
  NATURAL_STORAGE=PG
3
3
  # Settings for Postgres
4
- PG_USER=user
5
- PG_PASSWORD=password
6
- PG_DATABASE=naturaldb
7
- PG_HOST=localhost
8
- PG_PORT=5432
9
- PG_TABLE=naturaltable
4
+ POSTGRES_USER=user
5
+ POSTGRES_PASSWORD=password
6
+ POSTGRES_DB=naturaldb
7
+ POSTGRES_HOST=localhost
8
+ POSTGRES_PORT=5432
9
+ POSTGRES_TABLE=naturaltable
10
10
  # Settings for Redis
11
11
  REDIS_HOST=localhost
12
12
  REDIS_PORT=6379
@@ -18,5 +18,6 @@ MEM_EXPIRES = 3600
18
18
  MONGO_HOST=localhost
19
19
  MONGO_PORT=27017
20
20
  MONGO_DATABASE=naturaldb
21
+ MONGO_OBJECTMODEL=NaturalObjectModel
21
22
  # Path to the folder writing files
22
23
  FS_PATH=.
@@ -1,35 +1,76 @@
1
- # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2
- # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3
-
1
+ # This workflow will do a clean install of node dependencies,
2
+ # build the source code and run tests across different versions of node
3
+ # For more information see:
4
+ # https://help.github.com/actions/language-and-framework-guides/
5
+ # using-nodejs-with-github-actions
6
+ ---
4
7
  name: Node.js CI
5
8
 
6
9
  on:
7
10
  push:
8
- branches: [ master ]
11
+ branches: [master]
9
12
  pull_request:
10
- branches: [ master ]
13
+ branches: [master]
11
14
 
12
15
  jobs:
13
16
  build:
14
17
 
15
18
  runs-on: ubuntu-latest
16
19
 
20
+ services:
21
+ redis:
22
+ image: redis:latest
23
+ ports:
24
+ - 6379:6379
25
+ postgres:
26
+ image: postgres:latest
27
+ env:
28
+ POSTGRES_USER: user
29
+ POSTGRES_PASSWORD: password
30
+ POSTGRES_DB: naturaldb
31
+ POSTGRES_HOST: localhost
32
+ POSTGRES_PORT: 5432
33
+ POSTGRES_TABLE: naturaltable
34
+ ports:
35
+ - 5432:5432
36
+ memcached:
37
+ image: memcached:latest
38
+ ports:
39
+ - 11211:11211
40
+ mongodb:
41
+ image: mongo:latest
42
+ ports:
43
+ - 27017:27017
44
+
17
45
  strategy:
18
46
  matrix:
19
47
  node-version: [16.x, 18.x, 20.x]
20
- # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
48
+ # See supported Node.js release schedule at
49
+ #https://nodejs.org/en/about/releases/
21
50
 
22
51
  steps:
23
- - uses: actions/checkout@v2
24
- - name: Use Node.js ${{ matrix.node-version }}
25
- uses: actions/setup-node@v4
26
- with:
27
- node-version: ${{ matrix.node-version }}
28
- - run: npm ci
29
- - run: npm run build --if-present
30
- - run: npm run coverage
31
- # Save coverage report in Coveralls
32
- - name: Coveralls
33
- uses: coverallsapp/github-action@master
34
- with:
35
- github-token: ${{ secrets.GITHUB_TOKEN }}
52
+ - name: Checkout code
53
+ uses: actions/checkout@v2
54
+
55
+ - name: Use Node.js ${{ matrix.node-version }}
56
+ uses: actions/setup-node@v4
57
+ with:
58
+ node-version: ${{ matrix.node-version }}
59
+
60
+ - name: Clean install
61
+ run: npm ci
62
+
63
+ - name: Run tests
64
+ run: npm run test
65
+
66
+ - name: Run IO tests
67
+ run: npm run test_io
68
+
69
+ - name: Run Istanbul coverage
70
+ run: npm run coverage
71
+
72
+ # Save coverage report in Coveralls
73
+ - name: Coveralls
74
+ uses: coverallsapp/github-action@master
75
+ with:
76
+ github-token: ${{ secrets.GITHUB_TOKEN }}
@@ -52,6 +52,11 @@ class BayesClassifier extends Classifier {
52
52
  }
53
53
  })
54
54
  }
55
+
56
+ static async loadFrom (key, stemmer, storageBackend) {
57
+ const classifier = await Classifier.loadFrom(key, storageBackend)
58
+ return BayesClassifier.restore(classifier, stemmer)
59
+ }
55
60
  }
56
61
 
57
62
  module.exports = BayesClassifier
@@ -164,6 +164,16 @@ class Classifier extends EventEmitter {
164
164
  })
165
165
  }
166
166
 
167
+ async saveTo (storageBackend) {
168
+ return await storageBackend.store(this)
169
+ }
170
+
171
+ static async loadFrom (key, storageBackend) {
172
+ const obj = await storageBackend.retrieve(key)
173
+ Object.setPrototypeOf(obj, Classifier.prototype)
174
+ return obj
175
+ }
176
+
167
177
  setOptions (options) {
168
178
  this.keepStops = !!(options.keepStops)
169
179
  }
@@ -42,15 +42,20 @@ class LogisticRegressionClassifier extends Classifier {
42
42
  }
43
43
 
44
44
  static load (filename, stemmer, callback) {
45
- Classifier.load(filename, function (err, classifier) {
45
+ Classifier.load(filename, (err, classifier) => {
46
46
  if (err) {
47
47
  callback(err)
48
48
  } else {
49
- callback(err, this.restore(classifier, stemmer))
49
+ callback(err, LogisticRegressionClassifier.restore(classifier, stemmer))
50
50
  }
51
51
  })
52
52
  }
53
53
 
54
+ static async loadFrom (key, stemmer, storageBackend) {
55
+ const classifier = await Classifier.loadFrom(key, storageBackend)
56
+ return LogisticRegressionClassifier.restore(classifier, stemmer)
57
+ }
58
+
54
59
  train () {
55
60
  // we need to reset the training state because logistic regression
56
61
  // needs its matricies to have their widths synced, etc.
@@ -1,3 +1,25 @@
1
+ /*
2
+ Copyright (c) 2024, Hugo W.L. ter Doest
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+ */
22
+
1
23
  require('dotenv').config()
2
24
  const { v4: uuidv4 } = require('uuid')
3
25
  const fs = require('fs').promises
@@ -1,3 +1,25 @@
1
+ /*
2
+ Copyright (c) 2024, Hugo W.L. ter Doest
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+ */
22
+
1
23
  const { v4: uuidv4 } = require('uuid')
2
24
  require('dotenv').config()
3
25
  const memjs = require('memjs')
@@ -1,14 +1,42 @@
1
+ /*
2
+ Copyright (c) 2024, Hugo W.L. ter Doest
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+ */
22
+
1
23
  require('dotenv').config()
2
24
  const mongoose = require('mongoose')
3
25
  const mongoDBConnectionString = 'mongodb://' + process.env.MONGO_HOST + ':' +
4
26
  process.env.MONGO_PORT + '/' + process.env.MONGO_DATABASE
27
+ const objectModelName = process.env.MONGO_OBJECTMODEL
5
28
 
6
29
  class MongoDBPlugin {
7
30
  constructor () {
8
31
  mongoose.connect(mongoDBConnectionString, {})
9
- // We do not enforce a schema with MongoDB
10
- this.ObjectModel = mongoose.model('ObjectModel', new mongoose.Schema({},
11
- { strict: false, versionKey: false }))
32
+
33
+ if (!mongoose.models[objectModelName]) {
34
+ // We do not enforce a schema with MongoDB
35
+ this.ObjectModel = mongoose.model(objectModelName, new mongoose.Schema({},
36
+ { strict: false, versionKey: false }))
37
+ } else {
38
+ this.ObjectModel = mongoose.models[objectModelName]
39
+ }
12
40
  }
13
41
 
14
42
  async store (object, options) {
@@ -1,54 +1,71 @@
1
+ /*
2
+ Copyright (c) 2024, Hugo W.L. ter Doest
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+ */
22
+
1
23
  const postgres = require('pg')
2
24
  require('dotenv').config()
3
25
 
4
26
  class PostgresPlugin {
5
27
  constructor () {
6
- this.configPostgres()
28
+ this.postgresClient = null
7
29
  }
8
30
 
9
31
  async configPostgres () {
10
32
  // Initialize connection to Postgres
11
33
  const client = new postgres.Client({
12
- user: process.env.PG_USER,
13
- host: process.env.PG_HOST,
14
- database: process.env.PG_DATABASE,
15
- password: process.env.PG_PASSWORD,
16
- port: process.env.PG_PORT
34
+ user: process.env.POSTGRES_USER,
35
+ host: process.env.POSTGRES_HOST,
36
+ database: process.env.POSTGRES_DB,
37
+ password: process.env.POSTGRES_PASSWORD,
38
+ port: process.env.POSTGRES_PORT
17
39
  })
18
40
  this.postgresClient = client
19
- this.postgresTableName = process.env.PG_TABLE
41
+ this.postgresTableName = process.env.POSTGRES_TABLE
42
+
20
43
  // Connect to the database
21
- return new Promise((resolve, reject) => {
22
- client.connect()
23
- .then(() => {
24
- // Call the function to create the table
25
- createTable(this.postgresTableName)
26
- resolve()
27
- })
28
- .catch(err => {
29
- console.error('Error connecting to the database:', err)
30
- reject(err)
31
- })
32
- })
44
+ await client.connect()
45
+
46
+ // Call the function to create the table
47
+ await this.createTableIfNotExists(client, this.postgresTableName)
48
+
49
+ // Don't forget to close the connection when done
50
+ // await client.end()
51
+ }
52
+
53
+ // Function to create the table
54
+ async createTableIfNotExists (client, tableName) {
55
+ // Check if the table exists
56
+ const query = `SELECT to_regclass('${tableName}')`
57
+ const result = await client.query(query)
33
58
 
34
- // Function to create the table
35
- function createTable (tableName) {
36
- // We use JSONB (binary JSON format of Postgres) to store objects
59
+ if (result.rows[0].to_regclass === null) {
60
+ // Table does not exist, create it
37
61
  const createTableQuery = `
38
- CREATE TABLE IF NOT EXISTS ${tableName} (
62
+ CREATE TABLE ${tableName} (
39
63
  id SERIAL PRIMARY KEY,
40
64
  data JSONB
41
65
  );
42
66
  `
43
- return new Promise((resolve, reject) => {
44
- client.query(createTableQuery)
45
- .then(() => {
46
- resolve()
47
- })
48
- .catch(error => {
49
- reject(error)
50
- })
51
- })
67
+ await client.query(createTableQuery)
68
+ console.log('table created')
52
69
  }
53
70
  }
54
71
 
@@ -1,3 +1,25 @@
1
+ /*
2
+ Copyright (c) 2024, Hugo W.L. ter Doest
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+ */
22
+
1
23
  const { v4: uuidv4 } = require('uuid')
2
24
  require('dotenv').config()
3
25
  const redis = require('redis')
@@ -1,3 +1,25 @@
1
+ /*
2
+ Copyright (c) 2024, Hugo W.L. ter Doest
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+ */
22
+
1
23
  const PostgresPlugin = require('./Postgres')
2
24
  const MongoDBPlugin = require('./MongoDB')
3
25
  const RedisPlugin = require('./Redis')
@@ -38,6 +60,7 @@ class StorageBackend {
38
60
  switch (storageType) {
39
61
  case STORAGE_TYPES.POSTGRES:
40
62
  this.client = new PostgresPlugin()
63
+ await this.client.configPostgres()
41
64
  break
42
65
  case STORAGE_TYPES.REDIS:
43
66
  this.client = new RedisPlugin()
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "natural",
3
3
  "description": "General natural language (tokenizing, stemming (English, Russian, Spanish), part-of-speech tagging, sentiment analysis, classification, inflection, phonetics, tfidf, WordNet, jaro-winkler, Levenshtein distance, Dice's Coefficient) facilities for node.",
4
- "version": "6.11.1",
4
+ "version": "6.12.0",
5
5
  "homepage": "https://github.com/NaturalNode/natural",
6
6
  "repository": {
7
7
  "type": "git",
@@ -64,8 +64,8 @@
64
64
  "scripts": {
65
65
  "benchmark": "node benchmarks",
66
66
  "clean": "rimraf *~ #* *classifier.json",
67
- "test": "NODE_PATH=. node ./node_modules/jasmine/bin/jasmine.js io_spec/*_spec.js spec/*_spec.js",
68
- "coverage": "nyc --reporter=lcov npm test",
67
+ "test": "NODE_PATH=. node ./node_modules/jasmine/bin/jasmine.js --random=false spec/*_spec.js",
68
+ "coverage": "nyc --reporter=lcov npm run test && nyc npm run test_io",
69
69
  "test_io": "NODE_PATH=. node ./node_modules/jasmine/bin/jasmine.js --random=false io_spec/*_spec.js",
70
70
  "test_browser": "NODE_PATH=. node ./node_modules/gulp/bin/gulp.js",
71
71
  "lint": "eslint . --ext .ts",
package/tsconfig.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "include": [
18
18
  "lib/natural/**/*",
19
19
  "spec/*"
20
- ],
20
+ , "io_spec/StorageBackend_spec.js" ],
21
21
  "exclude": [
22
22
  "./node_modules"
23
23
  ]
@@ -1 +0,0 @@
1
- {"attr1":"val1","attr2":"val2"}