0

I am building a Django app that works with a text-heavy database, and it counts some predefined phrases within the text. It does not count the number of records that contain the phrases, but it counts the frequency within one record. And here is where i am stuck:

Django does count and find phrases if i add the string directly:

frequency = 'Search this text for me, please'.count('this text')

When i use this logic with a model Manager, "count" does not work, and gives the error:

Typeerror: count() takes 1 positional argument but 2 were given

This is my code in model.py

class MyModelManager(models.Manager):
    def get_queryset(self):
        qs = super(MyModelManager, self).get_queryset().values_list('mytext').count('this text')
        return qs

class MyModel(models.Model):
    mytext = models.TextField()
    ...
    objects = MyModelManager() 

Can you please suggest me how should i continue? Am i on the right track or should i apply a totally different approach to make this working? Thanks!

Helga
  • 37
  • 6

1 Answers1

1

Django docs .count() has another purpose - this is only the length of QuerySet, it does not provide any additional counting and does not take any arguments. In your case looks like you have to use some text-parsing instruments, like NLTK or something else, can't give advice for that.

Chiefir
  • 2,561
  • 1
  • 27
  • 46
  • Yes, i am aware of that, and i was afraid that the successful string-search was just a lucky work-around in a small scale, and is not really a feature i should use for a database with millions of words to go through. I gave it a try, though. Thanks for taking the time on this! – Helga May 03 '18 at 15:55
  • Google for 'Full Text Search' - that is special class of search. – Chiefir May 03 '18 at 18:18
  • Thanks a lot, Chiefir! I checked out NLP before, which was a bit too much for a first glance, but not "full text search". Also, i just found that i can perform the search on the database before i pull it to django. – Helga May 04 '18 at 06:13
  • And also you might want to have a look on Haystack + Whoosh and Django GIN index :P – Chiefir May 04 '18 at 07:39
  • Thanks :) it looks like now that NLTK actually can sort my issue out and i can get on with the development. – Helga May 04 '18 at 08:51