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
[I][U][B]this part[/B][/U][/I]/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
from string import maketrans
def stripPunctuation(s):
transtab = maketrans('@#.!=/[];:"&*,?()-',' ')
return s.translate(transtab)
class Tweet:
""" A class that keeps track of a single tweet. Instance variables for:
--text: (string) text of the tweet
--url: (string) url for accessing the tweet
--author: (string) author's name
--tokens: (list) list of tokens generated by splitting text"""
def genTokens(self):
""" This method accesses the text instance variable, strips punctuation,
converts to lower case, and splits the string.
Returns a list of words/tokens """
return stripPunctuation(self.text.lower()).split()
def __str__(self):
"""Useful for debugging. Let's you see what's in the Tweet object
with a print statement: print <obj>"""
s = ''
s += 'self.text: ' + self.text + '\n'
s += 'self.url: ' + self.url + '\n'
s += 'self.author: ' + self.author + '\n'
s += 'self.tokens: ' + str(self.tokens)
return s
def __init__(self,text,url,author):
""" TODO:
Constructor for the class. Sets the instance variables from passed in
parameters, then calls genTokens to set the tokens instance variable. """
self.text = text
self.url = url
self.author = author
self.tokens = self.genTokens()
return
def len(self):
return len(self.text)
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
/len(self.genTokens(t))
return
def printHowCompact(self):
"""TODO: Pretty prints the compactness and the text of a Tweet"""
# uncomment the line below and replace 0 and "empty" with appropriate values
print "%.2f%% filler words in text: %s" % (20.0 , t)
return
def main():
# The lines below will let you test whether you've implemented the constructor and the compactness() method correctly
t = Tweet("Hi to all in #si182","http://www.si.umich.edu","tester")
print t
print t.len()
print t.compactness()
fname = raw_input("Please enter a file name: ")
f = open(fname,"r")
s = f.read()
#1 create tweets from reading the file
# a: Provided. Use BeautifulStoneSoup to extract the text, url, and author
# from each.
# b: TODO: call the Tweet class constructor to generate a new Tweet object
# c: TODO: append the resulting tweet to your list of tweets
from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(s,selfClosingTags=["link"])
tweets = []
for entry in soup.findAll("entry"):
# we need to typecast because beautifulsoup
# will return unicode objects
text = str(entry.find('title').contents[0])
author = str(entry.find('name').contents[0])
url = str(entry.find('link')['href'])
#b and c
x = Tweet(self,text,url,author)
tweets.append(x)
#2 print the tweets
print tweets
print len(tweets), "tweets total"
# TODO: invoke the printHowCompact method on each tweet
for tweet in tweets:
print printHowCompact()
main()