Python Forum
How do you compute tf-idf from a list without using the counter class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you compute tf-idf from a list without using the counter class
#1
I have a bit of code that works well in calculating term frequency using the counter class import.


from collections import Counter

terms=['the', 'fox', 'the', 'quick', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

tf = Counter(terms)

print(tf)
I am wondering can this be done without the use of the counter class in a simple def.
Reply
#2
It can. I have done it but I wouldn't if at that moment I knew for collections.Counter. Big Grin
A hint... See defaultdict
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
I want to do it without dict
Reply
#4
Hm! It's more work but... Just start codding.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Nov-30-2017, 10:41 PM)syntaxkiller Wrote: I want to do it without dict

in any case use need to end-up with some sort of data structure with term,count pairs - either dict, list/tuple of 2-element list/tuples, etc. and in this case dict is the best option.
This looks liek homework, so I move the thread to the appropriate sub-section
Reply
#6
The tuple are immutable type so you have to use list of list pairs [character, counter]
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(Dec-01-2017, 09:34 AM)wavic Wrote: The tuple are immutable type so you have to use list of list pairs [character, counter]
That is why I said "end up" - e.g if implement his own counter function, that will iterate over the list (eventually converted to set) and return tuple (term, count) for each term in the input list/set
Reply
#8
Sure, I miss my morning coffee.  Smile
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
(Nov-30-2017, 10:41 PM)syntaxkiller Wrote: I want to do it without dict

Cool.  If you have any trouble, feel free to share what you've tried, and we'll help.

But we won't do it for you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Maths and python: Compute stress level cheerful 1 2,706 Oct-20-2021, 10:05 AM
Last Post: Larz60+
  Compute complex solutions in quadratic equations liam 1 1,878 Feb-09-2020, 04:18 PM
Last Post: Gribouillis
  How to compute conditional unigram probabilities? jbond 2 2,504 Jan-25-2020, 02:58 PM
Last Post: jbond
  Child Class, Loop list bjornnotborg 2 2,390 Aug-28-2019, 12:31 PM
Last Post: bjornnotborg
  To extract a specific column from csv file and compute the average vicson 2 8,090 Oct-20-2018, 03:18 AM
Last Post: vicson
  Python Counter - Convert to a Class jill1978 1 2,096 Oct-16-2018, 10:52 AM
Last Post: Larz60+
  Write a program to compute the sum of the terms of the series: 4 - 8 + 12 - 16 + 20 - chewey777 0 2,801 Mar-24-2018, 12:39 AM
Last Post: chewey777
  compute gross pay jamesuzo 1 10,308 Sep-07-2017, 01:47 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020