A modular pipeline for text classification using large language models, featuring label extraction, clustering, hierarchical taxonomy mapping, and sentiment analysis.
This implementation is based on the methodology described in:
Wei, M., Zhang, P., Chen, C. et al. Mapping AI ethics narratives: evidence from Twitter discourse between 2015 and 2022. Humanit Soc Sci Commun 12, 212 (2025). https://doi.org/10.1057/s41599-025-04469-9
This pipeline automates the process of extracting, organizing, and analyzing thematic structures from large-scale text corpora. It combines multiple state-of-the-art NLP models to:
This pipeline integrates multiple pre-trained transformer models:
bloomberg/KeyBARTall-MiniLM-L6-v2roberta-large-mnlicardiffnlp/twitter-roberta-base-sentiment-latestFor enhanced performance, you can optionally use OpenAI's GPT models:
gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-3.5-turbollm_classification/
├── src/
│ ├── core/
│ │ ├── clustering.py # Label clustering and alignment
│ │ ├── pipeline.py # Main pipeline orchestrator
│ │ ├── statistics.py # Statistics calculation
│ │ └── taxonomy.py # Taxonomy mapping
│ ├── models/
│ │ ├── label_extractor.py # KeyBART label extraction
│ │ └── sentiment_analyzer.py # RoBERTa sentiment analysis
│ └── utils/
│ ├── data_io.py # Data I/O utilities
│ ├── device.py # Device management
│ └── text_processing.py # Text preprocessing
├── config/ # Configuration files
├── tem_file/ # Cache directory
├── main.py # Main CLI entry point
├── extract_labels.py # Label extraction script
├── analyze_sentiment.py # Sentiment analysis script
└── cluster_labels.py # Label clustering script
pip install -r requirements.txt
Or manually install:
pip install torch transformers sentence-transformers scikit-learn scipy pandas nltk tqdm openpyxl
All models download automatically on first use! No manual download needed.
The pipeline uses models from Hugging Face Hub (~3.6 GB total):
First run: Models download automatically (5-30 minutes) Subsequent runs: Uses cached models (instant, works offline)
Pre-download all models (optional):
python download_models.py
For China users (if Hugging Face is slow):
export HF_ENDPOINT=https://hf-mirror.com
python main.py --input data.jsonl
Model cache location: ~/.cache/huggingface/
The pipeline will automatically download required NLTK resources on first run:
To use GPT-based features:
pip install openai>=1.12.0
Set your API key:
export OPENAI_API_KEY='your-api-key-here'
Use free, open-source models (recommended for most cases):
python main.py \
--input data.jsonl \
--column content \
--taxonomy taxonomy.json \
--processes 4 \
--sentiment
Use OpenAI GPT models for enhanced performance:
# Set API key first
export OPENAI_API_KEY='your-api-key-here'
# Run GPT pipeline
python main_gpt.py \
--input data.jsonl \
--column content \
--model gpt-4o-mini \
--generate-taxonomy \
--batch-size 50
GPT Pipeline Options:
--model: Choose GPT model (gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-3.5-turbo)--generate-taxonomy: Auto-generate taxonomy from labels--taxonomy: Use existing taxonomy file--api-key: Pass API key directly (or use env var)--batch-size: Labels per GPT request (default: 50)--max-labels: Max labels for taxonomy generation (default: 500)Extract Labels:
python extract_labels.py \
--file_path data.jsonl \
--col_name content \
--processes_num 3
Cluster Labels:
python cluster_labels.py \
--file_path data_labels.jsonl \
--min_samples 3
Analyze Sentiment:
python analyze_sentiment.py \
--file_path data.jsonl \
--col_name content \
--processes_num 5
--input: Input file (.jsonl or .xlsx)--column: Text content column name (default: content)--taxonomy: Path to taxonomy JSON file--processes: Number of parallel processes (default: 3)--min-samples: Minimum samples for DBSCAN (default: 3)--max-length: Max output length for labels (default: 100)--num-beams: Number of beams for generation (default: 10)--threshold: Taxonomy mapping threshold (default: 0.9)--sentiment: Include sentiment analysis--skip-extraction: Skip label extraction step--overwrite: Overwrite cached resultsJSONL format with one JSON object per line:
{"content": "Your text here", "other_field": "value"}
{input}_labels.jsonl: Extracted labels{input}_sentiment.jsonl: Sentiment scores{input}_hierarchy_labels.jsonl: Mapped hierarchical labelstem_file/sorted_word_list_*.json: Sorted unique labelstem_file/final_mapping_dict*.json: Label alignment mappingtem_file/all_num_key*.json: Category frequency statisticsThe taxonomy should be a nested JSON structure representing hierarchical categories:
{
"Category1": {
"Subcategory1": ["label1", "label2"],
"Subcategory2": ["label3", "label4"]
},
"Category2": {
"Subcategory3": ["label5", "label6"]
}
}
Example taxonomy from the AI Ethics research paper:
{
"Technology": {
"AI Systems": ["artificial intelligence", "machine learning", "algorithm"],
"Data": ["data privacy", "personal data", "data protection"]
},
"Ethics": {
"Bias and Fairness": ["bias", "discrimination", "fairness"],
"Transparency": ["explainability", "transparency", "accountability"]
},
"Society": {
"Impact": ["social impact", "employment", "inequality"],
"Regulation": ["regulation", "policy", "governance"]
}
}
Input Text (JSONL/Excel)
↓
[1] Label Extraction (KeyBART)
→ Extracts keywords/labels from each document
↓
[2] Label Cleaning & Clustering (NLTK + Sentence-BERT + DBSCAN)
→ Lemmatizes words
→ Generates semantic embeddings
→ Clusters similar labels
→ Aligns label variants
↓
[3] Taxonomy Mapping (RoBERTa-MNLI)
→ Maps labels to hierarchical categories
→ Uses zero-shot classification
↓
[4] Statistics Calculation
→ Counts label frequencies
→ Aggregates parent category counts
↓
[5] Sentiment Analysis (Twitter-RoBERTa) [Optional]
→ Analyzes sentiment polarity
↓
Output: Labeled & Classified Documents + Statistics
Input Text (JSONL/Excel)
↓
[1] Label Extraction (GPT-4o/GPT-4o-mini)
→ Uses GPT to extract contextual labels
↓
[2] Label Cleaning & Clustering (NLTK + Sentence-BERT + DBSCAN)
→ Same as standard pipeline
↓
[3A] Taxonomy Generation (GPT-4o) [Optional]
→ Auto-generate hierarchical taxonomy
OR
[3B] Taxonomy Mapping (GPT-4o-mini)
→ Map labels to provided taxonomy
↓
[4] Statistics Calculation
→ Same as standard pipeline
↓
Output: Labeled & Classified Documents + Statistics
If you use this pipeline in your research, please cite:
@article{wei2025mapping,
title={Mapping AI ethics narratives: evidence from Twitter discourse between 2015 and 2022},
author={Wei, M. and Zhang, P. and Chen, C. and others},
journal={Humanities and Social Sciences Communications},
volume={12},
pages={212},
year={2025},
publisher={Palgrave Macmillan},
doi={10.1057/s41599-025-04469-9}
}
A modular pipeline for text classification using large language models, featuring label extraction, clustering, hierarchical taxonomy mapping, and sentiment analysis.
This implementation is based on the methodology described in:
Wei, M., Zhang, P., Chen, C. et al. Mapping AI ethics narratives: evidence from Twitter discourse between 2015 and 2022. Humanit Soc Sci Commun 12, 212 (2025). https://doi.org/10.1057/s41599-025-04469-9
This pipeline automates the process of extracting, organizing, and analyzing thematic structures from large-scale text corpora. It combines multiple state-of-the-art NLP models to:
This pipeline integrates multiple pre-trained transformer models:
bloomberg/KeyBARTall-MiniLM-L6-v2roberta-large-mnlicardiffnlp/twitter-roberta-base-sentiment-latestFor enhanced performance, you can optionally use OpenAI's GPT models:
gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-3.5-turbollm_classification/
├── src/
│ ├── core/
│ │ ├── clustering.py # Label clustering and alignment
│ │ ├── pipeline.py # Main pipeline orchestrator
│ │ ├── statistics.py # Statistics calculation
│ │ └── taxonomy.py # Taxonomy mapping
│ ├── models/
│ │ ├── label_extractor.py # KeyBART label extraction
│ │ └── sentiment_analyzer.py # RoBERTa sentiment analysis
│ └── utils/
│ ├── data_io.py # Data I/O utilities
│ ├── device.py # Device management
│ └── text_processing.py # Text preprocessing
├── config/ # Configuration files
├── tem_file/ # Cache directory
├── main.py # Main CLI entry point
├── extract_labels.py # Label extraction script
├── analyze_sentiment.py # Sentiment analysis script
└── cluster_labels.py # Label clustering script
pip install -r requirements.txt
Or manually install:
pip install torch transformers sentence-transformers scikit-learn scipy pandas nltk tqdm openpyxl
All models download automatically on first use! No manual download needed.
The pipeline uses models from Hugging Face Hub (~3.6 GB total):
First run: Models download automatically (5-30 minutes) Subsequent runs: Uses cached models (instant, works offline)
Pre-download all models (optional):
python download_models.py
For China users (if Hugging Face is slow):
export HF_ENDPOINT=https://hf-mirror.com
python main.py --input data.jsonl
Model cache location: ~/.cache/huggingface/
The pipeline will automatically download required NLTK resources on first run:
To use GPT-based features:
pip install openai>=1.12.0
Set your API key:
export OPENAI_API_KEY='your-api-key-here'
Use free, open-source models (recommended for most cases):
python main.py \
--input data.jsonl \
--column content \
--taxonomy taxonomy.json \
--processes 4 \
--sentiment
Use OpenAI GPT models for enhanced performance:
# Set API key first
export OPENAI_API_KEY='your-api-key-here'
# Run GPT pipeline
python main_gpt.py \
--input data.jsonl \
--column content \
--model gpt-4o-mini \
--generate-taxonomy \
--batch-size 50
GPT Pipeline Options:
--model: Choose GPT model (gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-3.5-turbo)--generate-taxonomy: Auto-generate taxonomy from labels--taxonomy: Use existing taxonomy file--api-key: Pass API key directly (or use env var)--batch-size: Labels per GPT request (default: 50)--max-labels: Max labels for taxonomy generation (default: 500)Extract Labels:
python extract_labels.py \
--file_path data.jsonl \
--col_name content \
--processes_num 3
Cluster Labels:
python cluster_labels.py \
--file_path data_labels.jsonl \
--min_samples 3
Analyze Sentiment:
python analyze_sentiment.py \
--file_path data.jsonl \
--col_name content \
--processes_num 5
--input: Input file (.jsonl or .xlsx)--column: Text content column name (default: content)--taxonomy: Path to taxonomy JSON file--processes: Number of parallel processes (default: 3)--min-samples: Minimum samples for DBSCAN (default: 3)--max-length: Max output length for labels (default: 100)--num-beams: Number of beams for generation (default: 10)--threshold: Taxonomy mapping threshold (default: 0.9)--sentiment: Include sentiment analysis--skip-extraction: Skip label extraction step--overwrite: Overwrite cached resultsJSONL format with one JSON object per line:
{"content": "Your text here", "other_field": "value"}
{input}_labels.jsonl: Extracted labels{input}_sentiment.jsonl: Sentiment scores{input}_hierarchy_labels.jsonl: Mapped hierarchical labelstem_file/sorted_word_list_*.json: Sorted unique labelstem_file/final_mapping_dict*.json: Label alignment mappingtem_file/all_num_key*.json: Category frequency statisticsThe taxonomy should be a nested JSON structure representing hierarchical categories:
{
"Category1": {
"Subcategory1": ["label1", "label2"],
"Subcategory2": ["label3", "label4"]
},
"Category2": {
"Subcategory3": ["label5", "label6"]
}
}
Example taxonomy from the AI Ethics research paper:
{
"Technology": {
"AI Systems": ["artificial intelligence", "machine learning", "algorithm"],
"Data": ["data privacy", "personal data", "data protection"]
},
"Ethics": {
"Bias and Fairness": ["bias", "discrimination", "fairness"],
"Transparency": ["explainability", "transparency", "accountability"]
},
"Society": {
"Impact": ["social impact", "employment", "inequality"],
"Regulation": ["regulation", "policy", "governance"]
}
}
Input Text (JSONL/Excel)
↓
[1] Label Extraction (KeyBART)
→ Extracts keywords/labels from each document
↓
[2] Label Cleaning & Clustering (NLTK + Sentence-BERT + DBSCAN)
→ Lemmatizes words
→ Generates semantic embeddings
→ Clusters similar labels
→ Aligns label variants
↓
[3] Taxonomy Mapping (RoBERTa-MNLI)
→ Maps labels to hierarchical categories
→ Uses zero-shot classification
↓
[4] Statistics Calculation
→ Counts label frequencies
→ Aggregates parent category counts
↓
[5] Sentiment Analysis (Twitter-RoBERTa) [Optional]
→ Analyzes sentiment polarity
↓
Output: Labeled & Classified Documents + Statistics
Input Text (JSONL/Excel)
↓
[1] Label Extraction (GPT-4o/GPT-4o-mini)
→ Uses GPT to extract contextual labels
↓
[2] Label Cleaning & Clustering (NLTK + Sentence-BERT + DBSCAN)
→ Same as standard pipeline
↓
[3A] Taxonomy Generation (GPT-4o) [Optional]
→ Auto-generate hierarchical taxonomy
OR
[3B] Taxonomy Mapping (GPT-4o-mini)
→ Map labels to provided taxonomy
↓
[4] Statistics Calculation
→ Same as standard pipeline
↓
Output: Labeled & Classified Documents + Statistics
If you use this pipeline in your research, please cite:
@article{wei2025mapping,
title={Mapping AI ethics narratives: evidence from Twitter discourse between 2015 and 2022},
author={Wei, M. and Zhang, P. and Chen, C. and others},
journal={Humanities and Social Sciences Communications},
volume={12},
pages={212},
year={2025},
publisher={Palgrave Macmillan},
doi={10.1057/s41599-025-04469-9}
}