943,106 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 372
  • Python RSS
Feb 8th, 2010
0

Computing percentages with lists?

Expand Post »
Hello. I'm new here, and a student taking a course in Python. I'm completely lost as to what I should put in the numerator for this part:
	def compactness(self):
		# TODO: compute the percentage of filler words to total words
		# lower percentage is more compact
		# students to fill this in; not provided
		fillerwords = ['for', 'is', 'at', 'my', 'I', 'in', 'on', 'and', 'of', 'the', 'a', 'or', 'but', 'and', 'um']
		for x in
		this part/len(self.genTokens(t))
		return
I've tried so many things but I have not found a successful way to put t's fillerwords in the numerator.
here's the rest of my code

Python Syntax (Toggle Plain Text)
  1. from string import maketrans
  2. def stripPunctuation(s):
  3. transtab = maketrans('@#.!=/[];:"&*,?()-',' ')
  4. return s.translate(transtab)
  5.  
  6. class Tweet:
  7. """ A class that keeps track of a single tweet. Instance variables for:
  8. --text: (string) text of the tweet
  9. --url: (string) url for accessing the tweet
  10. --author: (string) author's name
  11. --tokens: (list) list of tokens generated by splitting text"""
  12.  
  13. def genTokens(self):
  14. """ This method accesses the text instance variable, strips punctuation,
  15. converts to lower case, and splits the string.
  16. Returns a list of words/tokens """
  17. return stripPunctuation(self.text.lower()).split()
  18.  
  19. def __str__(self):
  20. """Useful for debugging. Let's you see what's in the Tweet object
  21. with a print statement: print <obj>"""
  22. s = ''
  23. s += 'self.text: ' + self.text + '\n'
  24. s += 'self.url: ' + self.url + '\n'
  25. s += 'self.author: ' + self.author + '\n'
  26. s += 'self.tokens: ' + str(self.tokens)
  27. return s
  28.  
  29. def __init__(self,text,url,author):
  30. """ TODO:
  31. Constructor for the class. Sets the instance variables from passed in
  32. parameters, then calls genTokens to set the tokens instance variable. """
  33. self.text = text
  34. self.url = url
  35. self.author = author
  36. self.tokens = self.genTokens()
  37. return
  38.  
  39. def len(self):
  40. return len(self.text)
  41.  
  42. def compactness(self):
  43. # TODO: compute the percentage of filler words to total words
  44. # lower percentage is more compact
  45. # students to fill this in; not provided
  46. fillerwords = ['for', 'is', 'at', 'my', 'I', 'in', 'on', 'and', 'of', 'the', 'a', 'or', 'but', 'and', 'um']
  47. for x in
  48. /len(self.genTokens(t))
  49. return
  50.  
  51. def printHowCompact(self):
  52. """TODO: Pretty prints the compactness and the text of a Tweet"""
  53. # uncomment the line below and replace 0 and "empty" with appropriate values
  54. print "%.2f%% filler words in text: %s" % (20.0 , t)
  55. return
  56.  
  57. def main():
  58. # The lines below will let you test whether you've implemented the constructor and the compactness() method correctly
  59. t = Tweet("Hi to all in #si182","http://www.si.umich.edu","tester")
  60. print t
  61. print t.len()
  62. print t.compactness()
  63.  
  64. fname = raw_input("Please enter a file name: ")
  65. f = open(fname,"r")
  66. s = f.read()
  67.  
  68. #1 create tweets from reading the file
  69. # a: Provided. Use BeautifulStoneSoup to extract the text, url, and author
  70. # from each.
  71. # b: TODO: call the Tweet class constructor to generate a new Tweet object
  72. # c: TODO: append the resulting tweet to your list of tweets
  73.  
  74. from BeautifulSoup import BeautifulStoneSoup
  75. soup = BeautifulStoneSoup(s,selfClosingTags=["link"])
  76. tweets = []
  77. for entry in soup.findAll("entry"):
  78. # we need to typecast because beautifulsoup
  79. # will return unicode objects
  80. text = str(entry.find('title').contents[0])
  81. author = str(entry.find('name').contents[0])
  82. url = str(entry.find('link')['href'])
  83. #b and c
  84. x = Tweet(self,text,url,author)
  85. tweets.append(x)
  86.  
  87. #2 print the tweets
  88. print tweets
  89. print len(tweets), "tweets total"
  90. # TODO: invoke the printHowCompact method on each tweet
  91. for tweet in tweets:
  92. print printHowCompact()
  93. main()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
GoBlue is offline Offline
1 posts
since Feb 2010
Feb 10th, 2010
0
Re: Computing percentages with lists?
Your comment sais:
# TODO: compute the percentage of filler words to total words
# lower percentage is more compact
# students to fill this in; not provided

So you get out the punctuation from the text, you make the tokens (words) and store it in the variable "tokens".

You want to calculate the following:
number of tokens that are in fillerwords/number of all the words
Python Syntax (Toggle Plain Text)
  1. def compactness(self):
  2. fillerwords = set('for', 'is', 'at', 'my', 'I', 'in', 'on', 'and', 'of', 'the', 'a', 'or', 'but', 'and', 'um')
  3. return sum(1 for t in self.tokens if t in fillerwords)/len(self.tokens)
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Get my computer with wxPython
Next Thread in Python Forum Timeline: How to add contents in a tabbed view in PyQt4





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC