generated from github/codespaces-react
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Integrate teenager protection tools to social media analyzer. #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from .heuristics import ( | ||
| CYBERBULLYING_KEYWORDS, | ||
| INAPPROPRIATE_CONTENT_KEYWORDS, | ||
| PRIVACY_RISK_KEYWORDS, | ||
| HEURISTIC_WEIGHTS | ||
| ) | ||
|
|
||
| def analyze_text_for_teen_risks(text, analysis_type): | ||
| """ | ||
| Analyzes text for a specific type of risk to teenagers. | ||
|
|
||
| :param text: The text content to analyze. | ||
| :param analysis_type: The type of analysis to perform ('cyberbullying', | ||
| 'inappropriate_content', 'privacy_risk'). | ||
| :return: A dictionary with the score and indicators found. | ||
| """ | ||
| if not text: | ||
| return {"score": 0.0, "indicators_found": []} | ||
|
|
||
| text_lower = text.lower() | ||
| score = 0.0 | ||
| indicators_found = [] | ||
|
|
||
| keyword_map = { | ||
| 'cyberbullying': ('CYBERBULLYING', CYBERBULLYING_KEYWORDS), | ||
| 'inappropriate_content': ('INAPPROPRIATE_CONTENT', INAPPROPRIATE_CONTENT_KEYWORDS), | ||
| 'privacy_risk': ('PRIVACY_RISK', PRIVACY_RISK_KEYWORDS), | ||
| } | ||
|
|
||
| if analysis_type not in keyword_map: | ||
| return {"error": "Invalid analysis type specified."} | ||
|
|
||
| category, keywords = keyword_map[analysis_type] | ||
| weight = HEURISTIC_WEIGHTS.get(category.upper(), 1.0) | ||
|
|
||
| for keyword in keywords: | ||
| if keyword in text_lower: | ||
| message = f"Detected potential {category.replace('_', ' ').lower()} keyword: '{keyword}'" | ||
| if message not in indicators_found: | ||
| indicators_found.append(message) | ||
| score += weight | ||
|
|
||
| return { | ||
| "score": round(score, 2), | ||
| "indicators_found": indicators_found | ||
| } | ||
|
|
||
| def analyze_for_cyberbullying(text): | ||
| """Analyzes text for signs of cyberbullying.""" | ||
| return analyze_text_for_teen_risks(text, 'cyberbullying') | ||
|
|
||
| def analyze_for_inappropriate_content(text): | ||
| """Analyzes text for inappropriate content.""" | ||
| return analyze_text_for_teen_risks(text, 'inappropriate_content') | ||
|
|
||
| def analyze_for_privacy_risks(text): | ||
| """Analyzes text for privacy risks (oversharing).""" | ||
| return analyze_text_for_teen_risks(text, 'privacy_risk') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import unittest | ||
| from .teen_protection import ( | ||
| analyze_for_cyberbullying, | ||
| analyze_for_inappropriate_content, | ||
| analyze_for_privacy_risks | ||
| ) | ||
|
|
||
| class TestTeenProtection(unittest.TestCase): | ||
|
|
||
| def test_cyberbullying(self): | ||
| """Test the cyberbullying detection.""" | ||
| # Test case with bullying keywords | ||
| text1 = "You are such a loser and an idiot." | ||
| result1 = analyze_for_cyberbullying(text1) | ||
| self.assertGreater(result1['score'], 0) | ||
| self.assertIn("Detected potential cyberbullying keyword: 'loser'", result1['indicators_found']) | ||
| self.assertIn("Detected potential cyberbullying keyword: 'idiot'", result1['indicators_found']) | ||
|
|
||
| # Test case with no bullying keywords | ||
| text2 = "Have a great day!" | ||
| result2 = analyze_for_cyberbullying(text2) | ||
| self.assertEqual(result2['score'], 0) | ||
| self.assertEqual(len(result2['indicators_found']), 0) | ||
|
|
||
| def test_inappropriate_content(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Missing tests for multiple keyword occurrences in a single text. Add a test where a keyword appears multiple times to ensure correct score accumulation and indicator handling. |
||
| """Test the inappropriate content detection.""" | ||
| # Test case with inappropriate keywords | ||
| text1 = "Don't send nudes or talk about drugs." | ||
| result1 = analyze_for_inappropriate_content(text1) | ||
| self.assertGreater(result1['score'], 0) | ||
| self.assertIn("Detected potential inappropriate content keyword: 'send nudes'", result1['indicators_found']) | ||
| self.assertIn("Detected potential inappropriate content keyword: 'drugs'", result1['indicators_found']) | ||
|
|
||
| # Test case with no inappropriate keywords | ||
| text2 = "This is a perfectly normal conversation." | ||
| result2 = analyze_for_inappropriate_content(text2) | ||
| self.assertEqual(result2['score'], 0) | ||
| self.assertEqual(len(result2['indicators_found']), 0) | ||
|
|
||
| def test_privacy_risks(self): | ||
| """Test the privacy risk detection.""" | ||
| # Test case with privacy risk keywords | ||
| text1 = "My address is 123 Main St and my phone number is 555-1234." | ||
| result1 = analyze_for_privacy_risks(text1) | ||
| self.assertGreater(result1['score'], 0) | ||
| self.assertIn("Detected potential privacy risk keyword: 'my address is'", result1['indicators_found']) | ||
| self.assertIn("Detected potential privacy risk keyword: 'my phone number is'", result1['indicators_found']) | ||
|
|
||
| # Test case with no privacy risk keywords | ||
| text2 = "I like to talk about my hobbies." | ||
| result2 = analyze_for_privacy_risks(text2) | ||
| self.assertEqual(result2['score'], 0) | ||
| self.assertEqual(len(result2['indicators_found']), 0) | ||
|
|
||
| def test_empty_input(self): | ||
| """Test empty input for all analysis types.""" | ||
| result_cb = analyze_for_cyberbullying("") | ||
| self.assertEqual(result_cb['score'], 0) | ||
| self.assertEqual(len(result_cb['indicators_found']), 0) | ||
|
|
||
| result_ic = analyze_for_inappropriate_content("") | ||
| self.assertEqual(result_ic['score'], 0) | ||
| self.assertEqual(len(result_ic['indicators_found']), 0) | ||
|
|
||
| result_pr = analyze_for_privacy_risks("") | ||
| self.assertEqual(result_pr['score'], 0) | ||
| self.assertEqual(len(result_pr['indicators_found']), 0) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Heuristic weights for teen protection categories may need calibration.
These weights appear arbitrary; please validate them against expert input or data to ensure appropriate risk prioritization.