This project performs sentiment analysis on news articles related to US tariffs using Natural Language Processing (NLP) techniques. It fetches recent news articles from NewsAPI, analyzes their sentiment using VADER (Valence Aware Dictionary and sEntiment Reasoner), and creates word embeddings using Word2Vec.
- Fetches news articles about US tariffs from NewsAPI
- Performs sentiment analysis using VADER
- Creates word embeddings using Word2Vec
- Visualizes word embeddings using t-SNE
- Saves results and visualizations to output directory
Check out the web version of this project at News Friend!
- Python 3.8+
- NewsAPI key (get one for free at https://newsapi.org/)
- Clone this repository:
git clone <repository-url>
cd Sentiment_Analysis_About_US_Tariffs_News- Create a virtual environment (optional but recommended):
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install required packages:
pip install -r requirements.txt- Create a
.envfile in the project root and add your NewsAPI key:
NEWS_API_KEY=your_api_key_here
Run the script with default parameters:
python sentiment_analysis.py--api-key: NewsAPI key (optional if set in .env file)--query: Search query (default: "US tariffs")--days: Number of days to look back (default: 7)--output-dir: Output directory for results (default: "output")--picture-dir: Directory for saving visualizations (default: "picture")
Example with custom parameters:
python sentiment_analysis.py --query "US China trade" --days 14 --output-dir resultsThe script generates the following outputs in the specified output directory:
news_articles.csv: Contains the fetched articles with their sentiment scoresword_embeddings.png: Visualization of word embeddings using t-SNE
sentiment_analysis.py: Main script containing all functionalityrequirements.txt: List of required Python packages.env: Configuration file for API keys (not included in repository)output/: Directory containing generated resultspicture/: Directory containing visualizations
- requests: For making HTTP requests to NewsAPI
- pandas: For data manipulation
- nltk: For natural language processing
- gensim: For Word2Vec implementation
- scikit-learn: For t-SNE visualization
- matplotlib & seaborn: For data visualization
- python-dotenv: For loading environment variables
If you find this project useful, consider supporting me by buying me a coffee!
This project is licensed under the MIT License - see the LICENSE file for details.