I have the following code that works well, but I can’t trim and store data in a data file:
import nltk tweets = [ (['love', 'this', 'car']), (['this', 'view', 'amazing']), (['not', 'looking', 'forward', 'the', 'concert']) ] def get_words_in_tweets(tweets): all_words = [] for (words) in tweets: all_words.extend(words) return all_words def get_word_features(wordlist): wordlist = nltk.FreqDist(wordlist) word_features = wordlist.keys() return word_features output = open('wordFeatures.csv','w') word_features = get_word_features(get_words_in_tweets(tweets)) print (word_features) output.write(word_features)
What he does, he checks to see if the words are double or triple, etc. and adds only one word to the list. The result is as follows:
['this', 'amazing', 'car', 'concert', 'forward', 'looking', 'love', 'not', 'the', 'view']
Now, as you can see, I tried to save this data in a text file, but I get
TypeError: expected a character buffer object
I need data from an array in a text file in the following format:
1:this 2:amazing 3:car 4:concert 5:forward ...
therefore, one line for each word with an increasing integer.
Does anyone know how to save my data this way?