From 2a12864640ecb259a1c3e34c943c6954cc4e4278 Mon Sep 17 00:00:00 2001 From: gaiborjosue Date: Fri, 15 Nov 2024 14:28:32 -0500 Subject: [PATCH 1/3] feat: Added overall profesor rating based on name and school --- score/main.py | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 score/main.py diff --git a/score/main.py b/score/main.py new file mode 100644 index 0000000..dcf1c6f --- /dev/null +++ b/score/main.py @@ -0,0 +1,95 @@ +import ratemyprofessor +from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer + +class ProfessorAnalyzer: + def __init__(self): + self.sentiment_analyzer = SentimentIntensityAnalyzer() + + def get_professor(self, name, school_name): + school = ratemyprofessor.get_school_by_name(school_name) + professors = ratemyprofessor.get_professors_by_school_and_name(school, name) + + for prof in professors: + if prof.name == name: + return prof + return None + + def analyze_comments(self, professor): + ratings = professor.get_ratings() + weighted_compound = 0 + total_weight = 0 + + for rating in ratings: + # Base weight of 1, increased by positive feedback + weight = 1 + if rating.thumbs_up > 0 or rating.thumbs_down > 0: + weight += (rating.thumbs_up - rating.thumbs_down) / max( + sum(r.thumbs_up + r.thumbs_down for r in ratings), 1) + + sentiment = self.sentiment_analyzer.polarity_scores(rating.comment) + weighted_compound += sentiment['compound'] * weight + total_weight += weight + + avg_sentiment = weighted_compound / total_weight if total_weight > 0 else 0 + + return { + 'compound': avg_sentiment, + 'pos': max(0, avg_sentiment), + 'neg': max(0, -avg_sentiment), + 'neu': 1 - abs(avg_sentiment) + } + + def calculate_overall_score(self, professor, sentiment): + weights = { + 'rating': 0.25, + 'sentiment': 0.25, + 'difficulty_inverse': 0.2, + 'would_take_again': 0.3 + } + + normalized_scores = { + 'rating': professor.rating / 5.0, + 'sentiment': (sentiment['compound'] + 1) / 2, + 'difficulty_inverse': (5.0 - professor.difficulty) / 5.0, + 'would_take_again': professor.would_take_again / 100 if professor.would_take_again else 0.5 + } + + overall_score = sum(weights[key] * normalized_scores[key] for key in weights) + return overall_score * 100 + +def main(): + analyzer = ProfessorAnalyzer() + + # Example: Daniel Haehn, University of Massachusetts - Boston + name = input("Enter professor's name: ") + school = input("Enter school name: ") + + professor = analyzer.get_professor(name, school) + + if professor is None: + print("Professor not found.") + return + + print(f"\nProfessor: {professor.name}") + print(f"Department: {professor.department}") + print(f"School: {professor.school.name}") + print(f"Rating: {professor.rating}/5.0") + print(f"Difficulty: {professor.difficulty}/5.0") + print(f"Total Ratings: {professor.num_ratings}") + if professor.would_take_again: + print(f"Would Take Again: {round(professor.would_take_again, 1)}%") + else: + print("Would Take Again: N/A") + + sentiment = analyzer.analyze_comments(professor) + print("\nWeighted Comment Sentiment Analysis:") + print(f"Positive: {sentiment['pos']:.2f}") + print(f"Neutral: {sentiment['neu']:.2f}") + print(f"Negative: {sentiment['neg']:.2f}") + print(f"Compound: {sentiment['compound']:.2f}") + + overall_score = analyzer.calculate_overall_score(professor, sentiment) + print(f"\nOverall Professor Score: {overall_score:.1f}%") + +if __name__ == "__main__": + main() \ No newline at end of file From 9bf7b53737da0a32b10491f529e309f567d28aeb Mon Sep 17 00:00:00 2001 From: gaiborjosue Date: Fri, 15 Nov 2024 15:17:41 -0500 Subject: [PATCH 2/3] Add type hints and improve professor analysis --- score/main.py | 78 +++++++++++++++++++++++++++++++++--------- score/requirements.txt | 2 ++ 2 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 score/requirements.txt diff --git a/score/main.py b/score/main.py index dcf1c6f..3cf7337 100644 --- a/score/main.py +++ b/score/main.py @@ -1,11 +1,12 @@ import ratemyprofessor from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer +from typing import List, Dict, Optional class ProfessorAnalyzer: def __init__(self): self.sentiment_analyzer = SentimentIntensityAnalyzer() - def get_professor(self, name, school_name): + def get_professor(self, name: str, school_name: str) -> Optional[ratemyprofessor.Professor]: school = ratemyprofessor.get_school_by_name(school_name) professors = ratemyprofessor.get_professors_by_school_and_name(school, name) @@ -14,7 +15,7 @@ def get_professor(self, name, school_name): return prof return None - def analyze_comments(self, professor): + def analyze_comments(self, professor: ratemyprofessor.Professor) -> Dict[str, float]: ratings = professor.get_ratings() weighted_compound = 0 total_weight = 0 @@ -39,7 +40,7 @@ def analyze_comments(self, professor): 'neu': 1 - abs(avg_sentiment) } - def calculate_overall_score(self, professor, sentiment): + def calculate_overall_score(self, professor: ratemyprofessor.Professor, sentiment: Dict[str, float]) -> float: weights = { 'rating': 0.25, 'sentiment': 0.25, @@ -57,18 +58,26 @@ def calculate_overall_score(self, professor, sentiment): overall_score = sum(weights[key] * normalized_scores[key] for key in weights) return overall_score * 100 -def main(): - analyzer = ProfessorAnalyzer() - - # Example: Daniel Haehn, University of Massachusetts - Boston - name = input("Enter professor's name: ") - school = input("Enter school name: ") - - professor = analyzer.get_professor(name, school) - - if professor is None: - print("Professor not found.") - return + def analyze_professor(self, name: str, school_name: str) -> Optional[Dict]: + professor = self.get_professor(name, school_name) + + if professor is None: + print(f"Professor {name} not found.") + return None + + sentiment = self.analyze_comments(professor) + overall_score = self.calculate_overall_score(professor, sentiment) + + return { + 'professor': professor, + 'sentiment': sentiment, + 'overall_score': overall_score + } + +def print_professor_details(analysis: Dict) -> None: + professor = analysis['professor'] + sentiment = analysis['sentiment'] + overall_score = analysis['overall_score'] print(f"\nProfessor: {professor.name}") print(f"Department: {professor.department}") @@ -81,15 +90,50 @@ def main(): else: print("Would Take Again: N/A") - sentiment = analyzer.analyze_comments(professor) print("\nWeighted Comment Sentiment Analysis:") print(f"Positive: {sentiment['pos']:.2f}") print(f"Neutral: {sentiment['neu']:.2f}") print(f"Negative: {sentiment['neg']:.2f}") print(f"Compound: {sentiment['compound']:.2f}") - overall_score = analyzer.calculate_overall_score(professor, sentiment) print(f"\nOverall Professor Score: {overall_score:.1f}%") +def main(): + analyzer = ProfessorAnalyzer() + + # Get input for school and professors + school = input("Enter school name: ") + print("Enter professor names (one per line, press Enter twice when done):") + + professor_names = [] + while True: + name = input() + if not name: + break + professor_names.append(name) + + # Analyze all professors + professor_analyses = [] + for name in professor_names: + analysis = analyzer.analyze_professor(name, school) + if analysis: + professor_analyses.append(analysis) + + # Sort professors by overall score + professor_analyses.sort(key=lambda x: x['overall_score'], reverse=True) + + # Print individual details + print("\n=== Detailed Professor Analyses ===") + for analysis in professor_analyses: + print_professor_details(analysis) + print("\n" + "="*40) + + # Print rankings + print("\n=== Top 3 Professors ===") + for i, analysis in enumerate(professor_analyses[:3], 1): + prof = analysis['professor'] + score = analysis['overall_score'] + print(f"{i}. {prof.name} ({prof.department}) - Score: {score:.1f}%") + if __name__ == "__main__": main() \ No newline at end of file diff --git a/score/requirements.txt b/score/requirements.txt new file mode 100644 index 0000000..42b789d --- /dev/null +++ b/score/requirements.txt @@ -0,0 +1,2 @@ +ratemyprofessor +vaderSentiment \ No newline at end of file From fbf3a7126345885a785de6f1dd2a3a0d2f892c41 Mon Sep 17 00:00:00 2001 From: gaiborjosue Date: Fri, 15 Nov 2024 18:35:58 -0500 Subject: [PATCH 3/3] fix: Pip lib versions --- score/requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/score/requirements.txt b/score/requirements.txt index 42b789d..bc95654 100644 --- a/score/requirements.txt +++ b/score/requirements.txt @@ -1,2 +1,2 @@ -ratemyprofessor -vaderSentiment \ No newline at end of file +RateMyProfessorAPI==1.3.6 +vaderSentiment==3.3.2 \ No newline at end of file