Guest User

model

a guest
Jun 11th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. class Content(models.Model):
  2.     description = models.TextField()
  3.     author = models.ForeignKey(Author, on_delete=models.CASCADE)
  4.  
  5.     def __str__(self):
  6.         return self.description
  7.  
  8.     def get_most_used_words(self, count):
  9.         words = {}
  10.         stop_words = set(stopwords.words('english'))
  11.         description = self.description.split()
  12.         for element in description:
  13.             if element not in stop_words:
  14.                 if element in words:
  15.                     words[element] += 1
  16.                 else:
  17.                     words[element] = 1
  18.         top_10_words = sorted(words.items(), key=lambda x: -x[1])[:count]
  19.  
  20.         return top_10_words
Advertisement
Add Comment
Please, Sign In to add comment