| Conditions | 7 |
| Total Lines | 28 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | from textblob import TextBlob |
||
| 17 | def SentimentAnalysis(_arg1, library="nltk"): |
||
| 18 | """ |
||
| 19 | Sentiment Analysis is a procedure that assigns a score from -1 to 1 |
||
| 20 | for a piece of text with -1 being negative and 1 being positive. For |
||
| 21 | more information on the function and how to use it please refer to |
||
| 22 | tabpy-tools.md |
||
| 23 | """ |
||
| 24 | if not (isinstance(_arg1[0], str)): |
||
| 25 | raise TypeError |
||
| 26 | |||
| 27 | supportedLibraries = {"nltk", "textblob"} |
||
| 28 | |||
| 29 | library = library.lower() |
||
| 30 | if library not in supportedLibraries: |
||
| 31 | raise ValueError |
||
| 32 | |||
| 33 | scores = [] |
||
| 34 | if library == "nltk": |
||
| 35 | sid = SentimentIntensityAnalyzer() |
||
| 36 | for text in _arg1: |
||
| 37 | sentimentResults = sid.polarity_scores(text) |
||
| 38 | score = sentimentResults["compound"] |
||
| 39 | scores.append(score) |
||
| 40 | elif library == "textblob": |
||
| 41 | for text in _arg1: |
||
| 42 | currScore = TextBlob(text) |
||
| 43 | scores.append(currScore.sentiment.polarity) |
||
| 44 | return scores |
||
| 45 | |||
| 53 |