Search code examples
pythonmongodbclassificationwords

Classification based on List of Words Python


I would like to classify text: Positive, Negative or Neutral. I have a positive and negative list of words. I want to do it in Python. Here I describe in this kind of code

if word in text found in positive_words then counterpos = pos_count[i] += 1

if word in text found in negative_words then counterneg = neg_count[i] += 1

totalcount = pos_count + neg_count

if(len(totalcount) > 0): store in positive database elif (len(totalcount) < 0): store in negative database else:

store in neutral database

This is the general idea, my coding hability is null. i am storing in mongodb so that part i have no problem storing. Still i cant do the classification. Can someone please help me.


Solution

  • In addition to string comparisons and control flow statements, a list comprehension will be useful here.

    text = "seeking help on possible homework task"
    raw_words = text.split(" ")
    
    positive_words = ['seeking','help']
    negative_words = ['homework']
    
    positive_score = len([word for word in raw_words if word in positive_words])
    negative_score = len([word for word in raw_words if word in negative_words])
    
    total_score = positive_score - negative_score
    

    This would result in total_score having a value of 1.