Modifying the function in Python ( NLP)

Viewed 41
final_list = [ ]
words = ['good', 'bad', 'excellent','delivery', 'quality','upset','better','poor','refund','fake','cheat','quick','long','scam','cheaper','aluminium']

def func(words, list1):
  cnt = 0
  final_list = [ ]
  for i in list1:
    no_of_words = len(i.split())
    # print(no_of_words)
    # print(i)
    if no_of_words>2:
      for word in i.split():              # Only printing the records where occurence of words >2
        # print(word)
        if word in words:
          cnt+=1
      if cnt>2:
        final_list.append(i)
        cnt = 0
  return final_list
final_list = func(words, )
print( *final_list, sep = ' \n\n')

The above given code prints the row elements of the column 'list1' in which row elements contain words of the list 'words' where words > 2.

For eg. I am a good delivery person, but still customers cheat me sometimes.

Consider this to be one of the row elements. If this row is given it will get printed because (words>2) condition is satisfied i,e good,delivery, cheat are present in my list 'words'.

But I want to modify the function. Along with the above condition we also want to check if individual words>2 in the row element

for e.g.. I am a good delivery boy, I do good things to people, I don't cheat anyone, yet people are not good to me and cheat me often.

This above given example will not get printed. Because:

  1. Words>2 -> True //good, delivery , cheat are present ( words>2)
  2. Individual words>2 -> False // good- 3 times, delivery - 1 time , cheat - 2 times ( Doesn't satisfy the condition of individual items > 2, hence won't get printed)

Kindly help me modifying my function.

1 Answers

you can split the each element in a list and match with your list words and check if the duplicates of elements is greater than 2.

from collections import Counter
list1 = ['good good delivery good cheat delivery cheat delivery cheat', 'good 
cheat', 'fake good good fake']
words = ['good', 'bad', 'excellent', 'delivery', 'quality', 'upset', 'better', 
'poor', 'refund', 'fake', 'cheat','quick', 'long', 'scam', 'cheaper', 'aluminium']
repeated_words = []
for word in list1:
    data = word.split()
    match_data = [i for i in data if any((j in i) for j in words)]
    count_data = [k for k, v in Counter(match_data).items() if v > 2]
    if count_data:
        repeated_words.append(word)
print(repeated_words)

>>>> ['good good delivery good cheat delivery cheat delivery cheat']
Related