nltk-chunking 0.0.0-stage → 1.0.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/README.md +32 -2
- package/cli.js +136 -0
- package/package.json +23 -4
- package/program.py +26 -0
package/README.md
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
-
#
|
|
1
|
+
# nltk-chunking
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**NLTK Noun-Phrase Chunking**
|
|
4
|
+
|
|
5
|
+
POS-tag `input.txt` and print noun phrases found by an NLTK RegexpParser grammar.
|
|
6
|
+
|
|
7
|
+
## Run it
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npx nltk-chunking
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
No install step. The first run creates a small Python environment in
|
|
14
|
+
`~/.nltk-cli` and installs `nltk` automatically; later runs start instantly.
|
|
15
|
+
|
|
16
|
+
> **Note**
|
|
17
|
+
>
|
|
18
|
+
> This program reads `input.txt` from the folder where you run it. Create that file first.
|
|
19
|
+
|
|
20
|
+
## Requirements
|
|
21
|
+
|
|
22
|
+
- Node.js 18 or newer (only used to launch the Python program)
|
|
23
|
+
- Python 3.9 or newer on your `PATH`
|
|
24
|
+
- Internet access on the first run (to install `nltk` and download NLTK data)
|
|
25
|
+
|
|
26
|
+
## About
|
|
27
|
+
|
|
28
|
+
This package wraps a single Python/NLTK program so it can be run with `npx`.
|
|
29
|
+
The original Python source is included as `program.py`.
|
|
30
|
+
|
|
31
|
+
## License
|
|
32
|
+
|
|
33
|
+
MIT
|
package/cli.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Shared launcher for the bundled Python program.
|
|
6
|
+
*
|
|
7
|
+
* Why this exists: the program is written in Python (NLTK), but `npx` ships
|
|
8
|
+
* Node.js. This launcher finds a Python 3 interpreter, creates a small cached
|
|
9
|
+
* virtual environment on first use, installs `nltk` into it, and then runs the
|
|
10
|
+
* bundled program.py with stdin/stdout wired straight to your terminal.
|
|
11
|
+
*
|
|
12
|
+
* Everything is cached in ~/.nltk-cli so later runs start instantly.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const { spawn, spawnSync } = require('node:child_process');
|
|
16
|
+
const fs = require('node:fs');
|
|
17
|
+
const os = require('node:os');
|
|
18
|
+
const path = require('node:path');
|
|
19
|
+
|
|
20
|
+
const PROGRAM = path.join(__dirname, 'program.py');
|
|
21
|
+
const CACHE_DIR = process.env.NLTK_CLI_HOME || path.join(os.homedir(), '.nltk-cli');
|
|
22
|
+
const NLTK_DATA = path.join(CACHE_DIR, 'nltk_data');
|
|
23
|
+
const VENV_DIR = path.join(CACHE_DIR, 'venv');
|
|
24
|
+
const IS_WIN = process.platform === 'win32';
|
|
25
|
+
|
|
26
|
+
function info(message) {
|
|
27
|
+
process.stderr.write(`${message}\n`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function fail(message) {
|
|
31
|
+
process.stderr.write(`\nError: ${message}\n`);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function detectPython(cmd, prefixArgs) {
|
|
36
|
+
try {
|
|
37
|
+
const result = spawnSync(
|
|
38
|
+
cmd,
|
|
39
|
+
[...prefixArgs, '-c', 'import sys; print(sys.version_info[0])'],
|
|
40
|
+
{ encoding: 'utf8', windowsHide: true }
|
|
41
|
+
);
|
|
42
|
+
return result.status === 0 && String(result.stdout).trim() === '3';
|
|
43
|
+
} catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function findPython() {
|
|
49
|
+
const candidates = IS_WIN
|
|
50
|
+
? [['python', []], ['py', ['-3']], ['python3', []]]
|
|
51
|
+
: [['python3', []], ['python', []]];
|
|
52
|
+
|
|
53
|
+
for (const [cmd, args] of candidates) {
|
|
54
|
+
if (detectPython(cmd, args)) return { cmd, args };
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function venvPython() {
|
|
60
|
+
return IS_WIN
|
|
61
|
+
? path.join(VENV_DIR, 'Scripts', 'python.exe')
|
|
62
|
+
: path.join(VENV_DIR, 'bin', 'python');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function hasNltk(pythonPath) {
|
|
66
|
+
const result = spawnSync(pythonPath, ['-c', 'import nltk'], {
|
|
67
|
+
stdio: 'ignore',
|
|
68
|
+
windowsHide: true,
|
|
69
|
+
});
|
|
70
|
+
return result.status === 0;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function ensureEnvironment() {
|
|
74
|
+
if (!fs.existsSync(VENV_DIR) || !fs.existsSync(venvPython())) {
|
|
75
|
+
const python = findPython();
|
|
76
|
+
if (!python) {
|
|
77
|
+
fail(
|
|
78
|
+
'Python 3 was not found on this machine.\n' +
|
|
79
|
+
'Install it from https://www.python.org/downloads/ and try again.'
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
info('[setup] Creating a Python environment (first run only)...');
|
|
84
|
+
fs.mkdirSync(CACHE_DIR, { recursive: true });
|
|
85
|
+
|
|
86
|
+
const created = spawnSync(
|
|
87
|
+
python.cmd,
|
|
88
|
+
[...python.args, '-m', 'venv', VENV_DIR],
|
|
89
|
+
{ stdio: 'inherit', windowsHide: true }
|
|
90
|
+
);
|
|
91
|
+
if (created.status !== 0) {
|
|
92
|
+
fail('Could not create the Python virtual environment.');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const pythonPath = venvPython();
|
|
97
|
+
|
|
98
|
+
if (!hasNltk(pythonPath)) {
|
|
99
|
+
info('[setup] Installing nltk (first run only, this can take a minute)...');
|
|
100
|
+
const installed = spawnSync(
|
|
101
|
+
pythonPath,
|
|
102
|
+
['-m', 'pip', 'install', '--quiet', '--disable-pip-version-check', 'nltk'],
|
|
103
|
+
{ stdio: 'inherit', windowsHide: true }
|
|
104
|
+
);
|
|
105
|
+
if (installed.status !== 0) {
|
|
106
|
+
fail(
|
|
107
|
+
'Could not install nltk. Check your internet connection, then run the command again.'
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
fs.mkdirSync(NLTK_DATA, { recursive: true });
|
|
113
|
+
return pythonPath;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const pythonPath = ensureEnvironment();
|
|
117
|
+
|
|
118
|
+
const child = spawn(pythonPath, [PROGRAM], {
|
|
119
|
+
stdio: 'inherit',
|
|
120
|
+
env: {
|
|
121
|
+
...process.env,
|
|
122
|
+
NLTK_DATA,
|
|
123
|
+
PYTHONIOENCODING: 'utf-8',
|
|
124
|
+
PYTHONUTF8: '1',
|
|
125
|
+
},
|
|
126
|
+
windowsHide: false,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
child.on('error', (error) => {
|
|
130
|
+
fail(`Could not start Python: ${error.message}`);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
child.on('exit', (code, signal) => {
|
|
134
|
+
if (signal) process.kill(process.pid, signal);
|
|
135
|
+
process.exit(code === null ? 1 : code);
|
|
136
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nltk-chunking",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "POS-tag `input.txt` and print noun phrases found by an NLTK RegexpParser grammar.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"nltk",
|
|
7
|
+
"nlp",
|
|
8
|
+
"python",
|
|
9
|
+
"cli",
|
|
10
|
+
"tokenize"
|
|
11
|
+
],
|
|
12
|
+
"author": "Rohith",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"bin": {
|
|
15
|
+
"nltk-chunking": "cli.js"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"cli.js",
|
|
19
|
+
"program.py",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/program.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import nltk
|
|
2
|
+
nltk.download('punkt')
|
|
3
|
+
nltk.download('punkt_tab')
|
|
4
|
+
nltk.download('averaged_perceptron_tagger_eng')
|
|
5
|
+
|
|
6
|
+
from nltk.tokenize import word_tokenize
|
|
7
|
+
from nltk import pos_tag, RegexpParser
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
with open("input.txt", "r") as f:
|
|
11
|
+
text = f.read()
|
|
12
|
+
except FileNotFoundError:
|
|
13
|
+
print("Error: input.txt file not found! Create input.txt in the current folder.")
|
|
14
|
+
exit()
|
|
15
|
+
|
|
16
|
+
words = word_tokenize(text)
|
|
17
|
+
tags = pos_tag(words)
|
|
18
|
+
|
|
19
|
+
grammar = "NP: {<DT>?<JJ>*<NN.*>+}"
|
|
20
|
+
|
|
21
|
+
parser = RegexpParser(grammar)
|
|
22
|
+
tree = parser.parse(tags)
|
|
23
|
+
|
|
24
|
+
for np in tree.subtrees():
|
|
25
|
+
if np.label() == 'NP':
|
|
26
|
+
print(" ".join(w for w, t in np.leaves()))
|