txt2phrases 1.0.1__tar.gz
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.
- txt2phrases-1.0.1/PKG-INFO +223 -0
- txt2phrases-1.0.1/README.md +182 -0
- txt2phrases-1.0.1/dist/txt2phrases-0.2.0-py3-none-any.whl +0 -0
- txt2phrases-1.0.1/dist/txt2phrases-0.2.0.tar.gz +0 -0
- txt2phrases-1.0.1/pyproject.toml +3 -0
- txt2phrases-1.0.1/setup.cfg +4 -0
- txt2phrases-1.0.1/setup.py +47 -0
- txt2phrases-1.0.1/txt2phrases/__init__.py +32 -0
- txt2phrases-1.0.1/txt2phrases/classify_specific.py +143 -0
- txt2phrases-1.0.1/txt2phrases/cli.py +106 -0
- txt2phrases-1.0.1/txt2phrases/html2txt.py +70 -0
- txt2phrases-1.0.1/txt2phrases/keyword.py +139 -0
- txt2phrases-1.0.1/txt2phrases/pdf2txt.py +71 -0
- txt2phrases-1.0.1/txt2phrases/pygetpaper.py +183 -0
- txt2phrases-1.0.1/txt2phrases.egg-info/PKG-INFO +223 -0
- txt2phrases-1.0.1/txt2phrases.egg-info/SOURCES.txt +18 -0
- txt2phrases-1.0.1/txt2phrases.egg-info/dependency_links.txt +1 -0
- txt2phrases-1.0.1/txt2phrases.egg-info/entry_points.txt +2 -0
- txt2phrases-1.0.1/txt2phrases.egg-info/requires.txt +6 -0
- txt2phrases-1.0.1/txt2phrases.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: txt2phrases
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: A comprehensive library for text processing, keyword extraction, and classification from PDF and HTML documents
|
|
5
|
+
Home-page: https://github.com/semanticClimate/encyclopedia/tree/main/txt2phrases
|
|
6
|
+
Author: Udita Agarwal
|
|
7
|
+
Author-email: udita20agarwal@gmail.com
|
|
8
|
+
Keywords: text-processing,keyword-extraction,pdf,html,nlp,tf-idf,classification
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Topic :: Text Processing
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Requires-Python: >=3.7
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
Requires-Dist: beautifulsoup4>=4.9.0
|
|
26
|
+
Requires-Dist: transformers>=4.0.0
|
|
27
|
+
Requires-Dist: pandas>=1.0.0
|
|
28
|
+
Requires-Dist: tqdm>=4.50.0
|
|
29
|
+
Requires-Dist: PyPDF2>=2.0.0
|
|
30
|
+
Requires-Dist: scikit-learn>=1.0.0
|
|
31
|
+
Dynamic: author
|
|
32
|
+
Dynamic: author-email
|
|
33
|
+
Dynamic: classifier
|
|
34
|
+
Dynamic: description
|
|
35
|
+
Dynamic: description-content-type
|
|
36
|
+
Dynamic: home-page
|
|
37
|
+
Dynamic: keywords
|
|
38
|
+
Dynamic: requires-dist
|
|
39
|
+
Dynamic: requires-python
|
|
40
|
+
Dynamic: summary
|
|
41
|
+
|
|
42
|
+
# txt2phrases
|
|
43
|
+
|
|
44
|
+
`txt2phrases` is a Python library and CLI tool designed for processing and analyzing text data.
|
|
45
|
+
It provides a streamlined pipeline for converting documents (HTML, PDF) into plain text, extracting keywords using AI models, and classifying keywords into specific and general categories using TF-IDF.
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## ✨ Features
|
|
50
|
+
|
|
51
|
+
### 1. **PDF to Text Conversion**
|
|
52
|
+
- Extract plain text from PDF files for further processing.
|
|
53
|
+
|
|
54
|
+
### 2. **HTML to Text Conversion**
|
|
55
|
+
- Convert HTML documents into clean, plain text.
|
|
56
|
+
|
|
57
|
+
### 3. **AI-Powered Keyword Extraction**
|
|
58
|
+
- Use advanced NLP models (e.g., Hugging Face Transformers) to extract and rank the most important keywords from text files.
|
|
59
|
+
|
|
60
|
+
### 4. **Automated Pipeline**
|
|
61
|
+
- Run the entire pipeline (PDF/HTML → TXT → Keywords) with a single command.
|
|
62
|
+
|
|
63
|
+
### 5. **Batch Processing**
|
|
64
|
+
- Process single files or entire directories efficiently.
|
|
65
|
+
|
|
66
|
+
### 6. **Configurable Parameters**
|
|
67
|
+
- Customize thresholds, batch sizes, and output formats to suit your needs.
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## 🧩 Installation
|
|
72
|
+
|
|
73
|
+
Install `txt2phrases` directly from PyPI:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install txt2phrases
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## 🚀 Quick Start
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
# Convert PDF to text
|
|
85
|
+
txt2phrases pdf2txt -i document.pdf -o output_folder
|
|
86
|
+
|
|
87
|
+
# Convert HTML to text
|
|
88
|
+
txt2phrases html2txt -i webpage.html -o output_folder
|
|
89
|
+
|
|
90
|
+
# Extract keywords from text files
|
|
91
|
+
txt2phrases keyphrases -i text_files/ -o keywords/ -n 500
|
|
92
|
+
|
|
93
|
+
# Run complete pipeline
|
|
94
|
+
txt2phrases auto -i pygetpapers_output/ -o results/ -n 100
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## 🐍 Python API
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from txt2phrases import (
|
|
103
|
+
convert_pdf_to_text,
|
|
104
|
+
convert_html_to_text,
|
|
105
|
+
KeywordExtraction,
|
|
106
|
+
classify_keywords_split_files
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
# Convert PDF to text
|
|
110
|
+
txt_path = convert_pdf_to_text("document.pdf", "output_folder")
|
|
111
|
+
|
|
112
|
+
# Extract keywords
|
|
113
|
+
extractor = KeywordExtraction(
|
|
114
|
+
input_path="text_files/",
|
|
115
|
+
output_folder="keywords/",
|
|
116
|
+
top_n=1000
|
|
117
|
+
)
|
|
118
|
+
extractor.extract()
|
|
119
|
+
```
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## 🧠 CLI Commands
|
|
123
|
+
|
|
124
|
+
### 📄 `pdf2txt`
|
|
125
|
+
Convert PDF files to text format.
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
txt2phrases pdf2txt -i input.pdf -o output_folder
|
|
129
|
+
txt2phrases pdf2txt -i pdfs_directory/ -o text_output/
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
### 🌐 `html2txt`
|
|
135
|
+
Convert HTML files to clean text format.
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
txt2phrases html2txt -i webpage.html -o output_folder
|
|
139
|
+
txt2phrases html2txt -i html_directory/ -o text_output/
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
### 🔑 `keyphrases`
|
|
145
|
+
Extract keyphrases from text files using advanced NLP models.
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
txt2phrases keyphrases -i text.txt -o keywords/ -n 500
|
|
149
|
+
txt2phrases keyphrases -i text_directory/ -o keywords/ -n 1000
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
### ⚙️ `auto`
|
|
155
|
+
Complete processing pipeline for PyGetPapers output or PDF directories.
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
txt2phrases auto -i pygetpapers_output/ -o results/ -n 200
|
|
159
|
+
txt2phrases auto -i pdf_collection/ -o results/ -n 100
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## 🔍 Advanced Features
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
### 2. **Complete Research Pipeline**
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
# Download papers with PyGetPapers
|
|
171
|
+
pygetpapers -q "machine learning" -o papers/ -k 100
|
|
172
|
+
|
|
173
|
+
# Process and analyze
|
|
174
|
+
txt2phrases auto -i papers/ -o analysis/ -n 200
|
|
175
|
+
|
|
176
|
+
# Classify results
|
|
177
|
+
python -c "
|
|
178
|
+
from txt2phrases import classify_keywords_split_files
|
|
179
|
+
classify_keywords_split_files('analysis/', 'classified/', threshold=0.7)
|
|
180
|
+
"
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## 📦 Output Formats
|
|
186
|
+
|
|
187
|
+
- **Text Conversion:** `.txt` files with extracted text
|
|
188
|
+
- **Keyword Extraction:** `.csv` files containing keyword and count columns
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## 🧱 Requirements
|
|
193
|
+
|
|
194
|
+
To use `txt2phrases`, ensure you have the following installed:
|
|
195
|
+
|
|
196
|
+
- **Python 3.8+**
|
|
197
|
+
- **Dependencies:**
|
|
198
|
+
- `argparse`: For CLI argument parsing
|
|
199
|
+
- `beautifulsoup4`: For HTML parsing
|
|
200
|
+
- `pandas`: For data manipulation and CSV export
|
|
201
|
+
- `tqdm`: For progress bars during batch processing
|
|
202
|
+
- `transformers`: For AI-powered keyword extraction
|
|
203
|
+
- `scikit-learn`: For TF-IDF-based keyword classification
|
|
204
|
+
- `torch`: For running NLP models
|
|
205
|
+
|
|
206
|
+
Install dependencies with:
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
pip install -r requirements.txt
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
## 📚 Documentation
|
|
215
|
+
|
|
216
|
+
For full documentation and examples, visit the [GitHub repository](https://github.com/semanticClimate/encyclopedia/tree/main/txt2phrases).
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## 📄 License
|
|
221
|
+
|
|
222
|
+
This project is licensed under the **MIT License** — see the `LICENSE` file for details.
|
|
223
|
+
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# txt2phrases
|
|
2
|
+
|
|
3
|
+
`txt2phrases` is a Python library and CLI tool designed for processing and analyzing text data.
|
|
4
|
+
It provides a streamlined pipeline for converting documents (HTML, PDF) into plain text, extracting keywords using AI models, and classifying keywords into specific and general categories using TF-IDF.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## ✨ Features
|
|
9
|
+
|
|
10
|
+
### 1. **PDF to Text Conversion**
|
|
11
|
+
- Extract plain text from PDF files for further processing.
|
|
12
|
+
|
|
13
|
+
### 2. **HTML to Text Conversion**
|
|
14
|
+
- Convert HTML documents into clean, plain text.
|
|
15
|
+
|
|
16
|
+
### 3. **AI-Powered Keyword Extraction**
|
|
17
|
+
- Use advanced NLP models (e.g., Hugging Face Transformers) to extract and rank the most important keywords from text files.
|
|
18
|
+
|
|
19
|
+
### 4. **Automated Pipeline**
|
|
20
|
+
- Run the entire pipeline (PDF/HTML → TXT → Keywords) with a single command.
|
|
21
|
+
|
|
22
|
+
### 5. **Batch Processing**
|
|
23
|
+
- Process single files or entire directories efficiently.
|
|
24
|
+
|
|
25
|
+
### 6. **Configurable Parameters**
|
|
26
|
+
- Customize thresholds, batch sizes, and output formats to suit your needs.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 🧩 Installation
|
|
31
|
+
|
|
32
|
+
Install `txt2phrases` directly from PyPI:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install txt2phrases
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## 🚀 Quick Start
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# Convert PDF to text
|
|
44
|
+
txt2phrases pdf2txt -i document.pdf -o output_folder
|
|
45
|
+
|
|
46
|
+
# Convert HTML to text
|
|
47
|
+
txt2phrases html2txt -i webpage.html -o output_folder
|
|
48
|
+
|
|
49
|
+
# Extract keywords from text files
|
|
50
|
+
txt2phrases keyphrases -i text_files/ -o keywords/ -n 500
|
|
51
|
+
|
|
52
|
+
# Run complete pipeline
|
|
53
|
+
txt2phrases auto -i pygetpapers_output/ -o results/ -n 100
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 🐍 Python API
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from txt2phrases import (
|
|
62
|
+
convert_pdf_to_text,
|
|
63
|
+
convert_html_to_text,
|
|
64
|
+
KeywordExtraction,
|
|
65
|
+
classify_keywords_split_files
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# Convert PDF to text
|
|
69
|
+
txt_path = convert_pdf_to_text("document.pdf", "output_folder")
|
|
70
|
+
|
|
71
|
+
# Extract keywords
|
|
72
|
+
extractor = KeywordExtraction(
|
|
73
|
+
input_path="text_files/",
|
|
74
|
+
output_folder="keywords/",
|
|
75
|
+
top_n=1000
|
|
76
|
+
)
|
|
77
|
+
extractor.extract()
|
|
78
|
+
```
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## 🧠 CLI Commands
|
|
82
|
+
|
|
83
|
+
### 📄 `pdf2txt`
|
|
84
|
+
Convert PDF files to text format.
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
txt2phrases pdf2txt -i input.pdf -o output_folder
|
|
88
|
+
txt2phrases pdf2txt -i pdfs_directory/ -o text_output/
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
### 🌐 `html2txt`
|
|
94
|
+
Convert HTML files to clean text format.
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
txt2phrases html2txt -i webpage.html -o output_folder
|
|
98
|
+
txt2phrases html2txt -i html_directory/ -o text_output/
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
### 🔑 `keyphrases`
|
|
104
|
+
Extract keyphrases from text files using advanced NLP models.
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
txt2phrases keyphrases -i text.txt -o keywords/ -n 500
|
|
108
|
+
txt2phrases keyphrases -i text_directory/ -o keywords/ -n 1000
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
### ⚙️ `auto`
|
|
114
|
+
Complete processing pipeline for PyGetPapers output or PDF directories.
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
txt2phrases auto -i pygetpapers_output/ -o results/ -n 200
|
|
118
|
+
txt2phrases auto -i pdf_collection/ -o results/ -n 100
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## 🔍 Advanced Features
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
### 2. **Complete Research Pipeline**
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
# Download papers with PyGetPapers
|
|
130
|
+
pygetpapers -q "machine learning" -o papers/ -k 100
|
|
131
|
+
|
|
132
|
+
# Process and analyze
|
|
133
|
+
txt2phrases auto -i papers/ -o analysis/ -n 200
|
|
134
|
+
|
|
135
|
+
# Classify results
|
|
136
|
+
python -c "
|
|
137
|
+
from txt2phrases import classify_keywords_split_files
|
|
138
|
+
classify_keywords_split_files('analysis/', 'classified/', threshold=0.7)
|
|
139
|
+
"
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## 📦 Output Formats
|
|
145
|
+
|
|
146
|
+
- **Text Conversion:** `.txt` files with extracted text
|
|
147
|
+
- **Keyword Extraction:** `.csv` files containing keyword and count columns
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## 🧱 Requirements
|
|
152
|
+
|
|
153
|
+
To use `txt2phrases`, ensure you have the following installed:
|
|
154
|
+
|
|
155
|
+
- **Python 3.8+**
|
|
156
|
+
- **Dependencies:**
|
|
157
|
+
- `argparse`: For CLI argument parsing
|
|
158
|
+
- `beautifulsoup4`: For HTML parsing
|
|
159
|
+
- `pandas`: For data manipulation and CSV export
|
|
160
|
+
- `tqdm`: For progress bars during batch processing
|
|
161
|
+
- `transformers`: For AI-powered keyword extraction
|
|
162
|
+
- `scikit-learn`: For TF-IDF-based keyword classification
|
|
163
|
+
- `torch`: For running NLP models
|
|
164
|
+
|
|
165
|
+
Install dependencies with:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
pip install -r requirements.txt
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## 📚 Documentation
|
|
174
|
+
|
|
175
|
+
For full documentation and examples, visit the [GitHub repository](https://github.com/semanticClimate/encyclopedia/tree/main/txt2phrases).
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## 📄 License
|
|
180
|
+
|
|
181
|
+
This project is licensed under the **MIT License** — see the `LICENSE` file for details.
|
|
182
|
+
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
with open("README.md", "r", encoding="utf-8") as fh:
|
|
4
|
+
long_description = fh.read()
|
|
5
|
+
|
|
6
|
+
setup(
|
|
7
|
+
name="txt2phrases",
|
|
8
|
+
version="1.0.1",
|
|
9
|
+
author="Udita Agarwal",
|
|
10
|
+
author_email="udita20agarwal@gmail.com",
|
|
11
|
+
description="A comprehensive library for text processing, keyword extraction, and classification from PDF and HTML documents",
|
|
12
|
+
long_description=long_description,
|
|
13
|
+
long_description_content_type="text/markdown",
|
|
14
|
+
url="https://github.com/semanticClimate/encyclopedia/tree/main/txt2phrases",
|
|
15
|
+
packages=find_packages(),
|
|
16
|
+
classifiers=[
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Science/Research",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.7",
|
|
24
|
+
"Programming Language :: Python :: 3.8",
|
|
25
|
+
"Programming Language :: Python :: 3.9",
|
|
26
|
+
"Programming Language :: Python :: 3.10",
|
|
27
|
+
"Programming Language :: Python :: 3.11",
|
|
28
|
+
"Topic :: Text Processing",
|
|
29
|
+
"Topic :: Scientific/Engineering :: Information Analysis",
|
|
30
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
31
|
+
],
|
|
32
|
+
python_requires=">=3.7",
|
|
33
|
+
install_requires=[
|
|
34
|
+
"beautifulsoup4>=4.9.0",
|
|
35
|
+
"transformers>=4.0.0",
|
|
36
|
+
"pandas>=1.0.0",
|
|
37
|
+
"tqdm>=4.50.0",
|
|
38
|
+
"PyPDF2>=2.0.0",
|
|
39
|
+
"scikit-learn>=1.0.0",
|
|
40
|
+
],
|
|
41
|
+
entry_points={
|
|
42
|
+
"console_scripts": [
|
|
43
|
+
"txt2phrases=txt2phrases.cli:main",
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
keywords="text-processing, keyword-extraction, pdf, html, nlp, tf-idf, classification",
|
|
47
|
+
)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# __init__.py
|
|
2
|
+
"""
|
|
3
|
+
txt2phrases: A library for text processing, keyword extraction, and classification.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
__version__ = "1.0.0"
|
|
7
|
+
__author__ = "Your Name"
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
from .html2txt import convert_html_to_text
|
|
11
|
+
from .pdf2txt import convert_pdf_to_text
|
|
12
|
+
from .keyword import KeywordExtraction
|
|
13
|
+
from .classify_specific import classify_keywords_split_files
|
|
14
|
+
from .pygetpaper import main as pygetpaper_main
|
|
15
|
+
except ImportError:
|
|
16
|
+
# Handle the case when running as standalone scripts
|
|
17
|
+
from html2txt import convert_html_to_text
|
|
18
|
+
from pdf2txt import convert_pdf_to_text
|
|
19
|
+
from keyword import KeywordExtraction
|
|
20
|
+
from classify_specific import classify_keywords_split_files
|
|
21
|
+
from pygetpaper import main as pygetpaper_main
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"convert_html_to_text",
|
|
25
|
+
"convert_pdf_to_text",
|
|
26
|
+
"KeywordExtraction",
|
|
27
|
+
"classify_keywords_split_files",
|
|
28
|
+
"pygetpaper_main"
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import pandas as pd
|
|
3
|
+
from collections import defaultdict
|
|
4
|
+
from argparse import ArgumentParser
|
|
5
|
+
from sklearn.feature_extraction.text import TfidfTransformer
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def classify_keywords_split_files(input_dir, output_dir, threshold=0.6, min_freq=5):
|
|
9
|
+
"""
|
|
10
|
+
For each chapter, create a separate CSV listing keywords that are 'specific' to it
|
|
11
|
+
according to TF-IDF >= threshold. Also, create a single CSV with general/specific chapters.
|
|
12
|
+
|
|
13
|
+
Input: multiple chapter CSVs in `input_dir`, each with columns: keyword, count
|
|
14
|
+
Output:
|
|
15
|
+
- <chapter>_specific_keywords.csv in `output_dir`
|
|
16
|
+
- general_specific_keywords.csv in `output_dir`
|
|
17
|
+
"""
|
|
18
|
+
os.makedirs(output_dir, exist_ok=True)
|
|
19
|
+
|
|
20
|
+
# --- Load per-chapter frequencies (with min_freq filtering) ---
|
|
21
|
+
chapter_files = [f for f in os.listdir(input_dir) if f.endswith(".csv")]
|
|
22
|
+
if not chapter_files:
|
|
23
|
+
print(f"No CSV files found in {input_dir}")
|
|
24
|
+
return
|
|
25
|
+
|
|
26
|
+
keyword_chapter_freq = defaultdict(dict) # {keyword: {chapter: count}}
|
|
27
|
+
chapters = []
|
|
28
|
+
|
|
29
|
+
for file in chapter_files:
|
|
30
|
+
chapter = os.path.splitext(file)[0]
|
|
31
|
+
chapters.append(chapter)
|
|
32
|
+
df = pd.read_csv(os.path.join(input_dir, file))
|
|
33
|
+
if not {"keyword", "count"}.issubset(df.columns):
|
|
34
|
+
raise ValueError(f"{file} must contain 'keyword' and 'count' columns")
|
|
35
|
+
df = df[df["count"] >= min_freq]
|
|
36
|
+
for _, row in df.iterrows():
|
|
37
|
+
keyword_chapter_freq[str(row["keyword"])][chapter] = int(row["count"])
|
|
38
|
+
|
|
39
|
+
chapters = sorted(chapters)
|
|
40
|
+
keywords = list(keyword_chapter_freq.keys())
|
|
41
|
+
|
|
42
|
+
if not keywords:
|
|
43
|
+
# create empty files if no keywords pass min_freq
|
|
44
|
+
for chapter in chapters:
|
|
45
|
+
pd.DataFrame(columns=["keyword", "tfidf", "count"]).to_csv(
|
|
46
|
+
os.path.join(output_dir, f"{chapter}_specific_keywords.csv"), index=False
|
|
47
|
+
)
|
|
48
|
+
pd.DataFrame(columns=["keyword", "General", "Specific"]).to_csv(
|
|
49
|
+
os.path.join(output_dir, "general_specific_keywords.csv"), index=False
|
|
50
|
+
)
|
|
51
|
+
print("No keywords passed min_freq. Empty files created.")
|
|
52
|
+
return
|
|
53
|
+
|
|
54
|
+
# --- Build keyword x chapter matrix ---
|
|
55
|
+
data = []
|
|
56
|
+
for kw in keywords:
|
|
57
|
+
row = [keyword_chapter_freq[kw].get(ch, 0) for ch in chapters]
|
|
58
|
+
data.append(row)
|
|
59
|
+
df_matrix = pd.DataFrame(data, index=keywords, columns=chapters)
|
|
60
|
+
|
|
61
|
+
# --- TF-IDF over (keywords x chapters) ---
|
|
62
|
+
transformer = TfidfTransformer()
|
|
63
|
+
tfidf_matrix = transformer.fit_transform(df_matrix) # sparse
|
|
64
|
+
|
|
65
|
+
# --- For each chapter, collect specific keywords ---
|
|
66
|
+
for j, chapter in enumerate(chapters):
|
|
67
|
+
rows = []
|
|
68
|
+
for i, keyword in enumerate(df_matrix.index):
|
|
69
|
+
score = tfidf_matrix[i, j]
|
|
70
|
+
freq_in_chapter = int(df_matrix.iloc[i, j])
|
|
71
|
+
if score >= threshold and freq_in_chapter > 0:
|
|
72
|
+
rows.append((keyword, float(score), freq_in_chapter))
|
|
73
|
+
rows.sort(key=lambda x: (x[1], x[2]), reverse=True)
|
|
74
|
+
pd.DataFrame(rows, columns=["keyword", "tfidf", "count"]).to_csv(
|
|
75
|
+
os.path.join(output_dir, f"{chapter}_specific_keywords.csv"), index=False
|
|
76
|
+
)
|
|
77
|
+
print(f"Saved {chapter}_specific_keywords.csv ({len(rows)} keywords)")
|
|
78
|
+
|
|
79
|
+
# --- Generate general/specific keywords file ---
|
|
80
|
+
general_specific_rows = []
|
|
81
|
+
for i, keyword in enumerate(df_matrix.index):
|
|
82
|
+
specific_chapters = []
|
|
83
|
+
general_chapters = []
|
|
84
|
+
for j, chapter in enumerate(chapters):
|
|
85
|
+
score = tfidf_matrix[i, j]
|
|
86
|
+
if score >= threshold:
|
|
87
|
+
specific_chapters.append(chapter)
|
|
88
|
+
else:
|
|
89
|
+
general_chapters.append(chapter)
|
|
90
|
+
general_specific_rows.append((
|
|
91
|
+
keyword,
|
|
92
|
+
" ".join(general_chapters),
|
|
93
|
+
" ".join(specific_chapters)
|
|
94
|
+
))
|
|
95
|
+
|
|
96
|
+
out_df = pd.DataFrame(general_specific_rows, columns=["keyword", "General", "Specific"])
|
|
97
|
+
out_path = os.path.join(output_dir, "general_specific_keywords.csv")
|
|
98
|
+
out_df.to_csv(out_path, index=False)
|
|
99
|
+
print(f"Saved general_specific_keywords.csv ({len(out_df)} keywords)")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def main():
|
|
103
|
+
"""
|
|
104
|
+
Command-line interface for generating per-chapter specific keywords
|
|
105
|
+
and a general/specific keywords CSV.
|
|
106
|
+
"""
|
|
107
|
+
import argparse
|
|
108
|
+
from .classify_specific import classify_keywords_split_files
|
|
109
|
+
import os
|
|
110
|
+
parser = argparse.ArgumentParser(
|
|
111
|
+
description="Create per-chapter files of TF-IDF-specific keywords "
|
|
112
|
+
"and a general/specific keywords CSV"
|
|
113
|
+
)
|
|
114
|
+
parser.add_argument(
|
|
115
|
+
"-i", "--input_dir", required=True,
|
|
116
|
+
help="Path to folder with chapter CSVs (keyword,count)"
|
|
117
|
+
)
|
|
118
|
+
parser.add_argument(
|
|
119
|
+
"-o", "--output_dir", required=True,
|
|
120
|
+
help="Path to save per-chapter specific and general/specific keyword files"
|
|
121
|
+
)
|
|
122
|
+
parser.add_argument(
|
|
123
|
+
"-t", "--threshold", type=float, default=0.6,
|
|
124
|
+
help="TF-IDF threshold for specificity (default: 0.6)"
|
|
125
|
+
)
|
|
126
|
+
parser.add_argument(
|
|
127
|
+
"-f", "--min_freq", type=int, default=5,
|
|
128
|
+
help="Minimum frequency of keywords to consider (default: 5)"
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
args = parser.parse_args()
|
|
132
|
+
|
|
133
|
+
classify_keywords_split_files(
|
|
134
|
+
input_dir=args.input_dir,
|
|
135
|
+
output_dir=args.output_dir,
|
|
136
|
+
threshold=args.threshold,
|
|
137
|
+
min_freq=args.min_freq
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
if __name__ == "__main__":
|
|
141
|
+
main()
|
|
142
|
+
|
|
143
|
+
|