You could achieve this with a nested for loop
, however this isn't a great solution to a simple problem:
for posWord in posWords:
for test in readFile:
if i == test:
counter +=1
print i
print(counter)
This isn't an effective approach towards analysing sentiment, rather you are just checking if a no-context positive word exists in the text or not which doesn't tell you much. The way you are approaching this task ignores common semantics that make their way into every day language such as double negatives, palindromes, and so on. Also, it doesn't look like you are filtering out stop words from the text or stemming words. See Stemming Algorithms.
Sentiment Analysis should be the product of a statistic. Structured based approaches do not tend to be as useful as semantic implementations - however, this is up for debate (probably). Further, a supervised learning approach to [binary or multiclass]
classifying text into predefined categories such as positive or negative. A typical approach to sentiment analysis is implementing the Naive Bayes framework, although more effective / powerful methods have been proposed (SVM, Hidden Markov Models, and so on). See notable resource 2.
Final Notes
Although I don't really work with sentiment analysis unless I'm trying to make my life easier or compliment something I'm already doing, I do research a couple of topics in Natural Language Processing. I strongly believe that the academic domain has far surpassed the efforts of that in the commercial arena, in fact, some of the results / conclusions / prices companies are generating is hysterical - I'm still to come across a decent implementation. I recommend if you would like to learn more about this area you read academic journals published in IEEE & ACM.
Notable Resources:
- Python NLTK - Natural Language Tool Kit
- Twitter Sentiment Analysis using Python & NLTK
- Sentiment Analysis & Opinion Mining