import sysimport twython print 'Arguments:',sys.argv nounsstring = open("/Users/ranmo/Desktop/Nouns.txt", "r").read() adjectivesstring = open("/Users/ranmo/Desktop/Adjectives.txt", "r").read() nouns = list() adjectives = list() newNouns = list() number =0 nouns = nounsstring.split("\r\n") adjectives = adjectivesstring.split("\n") results = dict() name = sys.argv api_key = "cbQOYIm4QKOZFfU0udeecg" api_secret = "0xfLYKgOpLDfmYqLOujfWdeNBaG4K7AxJKm8e1huM" access_token = "441793842-SG93JvCP4426PpU9nSRt8yef8cliRY6gRFnMZmjE" token_secret = "BURRQAN5wZqzfQu9ly7TFiuE5412QtjSTb0G0c5LnPwoE" twitter = twython.Twython(api_key, api_secret, access_token, token_secret) response = twitter.get_user_timeline(screen_name=name, count=200) class tweet(object): def __init__(self,result): self.results = result self.tweet = tweet print "hello" def isNoun(self, word): if word == "": return False if word.lower() in nouns: return True else: return False print "noun" def isAdjective(self, word): if word == "": return False if word.lower() in adjectives: return True else: return False def newtweet(self): self.results = dict() for tweet in response: mynouns = list() myadjectives = list() if tweet['retweeted'] is False and tweet['text'][0:2] != "RT": words = tweet['text'].split(" ") for word in words: if self.isNoun(word): mynouns.append(word) elif self.isAdjective(word): myadjectives.append(word) for noun in mynouns: nounindex = words.index(noun) for adjective in myadjectives: adjectiveindex = words.index(adjective) if adjectiveindex < nounindex: self.results[noun] = adjective return self.results a = tweet(results) results = a.newtweet() for key in results: print results[key] + "\t\t" + key
Basically, what I did is using the idea which we've learned in the last RWET class to transform my mid-term project into a "class" in python. Also I did a little revise based on the former structure to refine to code as well.
By following the course note, the work was not that tough and complicated. However, because what I have to do was replant the former structure to the "class" logical structure, so the most errors I got were indentation issues which cost me a lot of time.
The interesting thing was after I finish all the debug work until there was no error, I barely could get more than 1 "result", which confused me a lot. I tried a lot. (The "count" in "response" is "count = 10")I printed stuff from the top to bottom of the code and found everything is fine. The turning point is when I check my friend's twitter, I found a tweet: "You. Are." I suddenly realize that this tweet would be neglect by my program because "You" is a noun, but there is no adjective at all. So, I tried to turn up the "count" number...