我的數據是來自webforum的對話線索。我創建了一個函數來清理停用詞,標點符號等數據。然後我創建了一個循環來清理我的csv文件中的所有帖子,並將它們放入列表中。然後我做了數字。我的問題是列表包含unicode短語而不是單個單詞。我怎樣才能分開這些短語,所以它們是我可以數的單個詞。這裏是我下面的代碼:如何將短語列表分成單詞,以便我可以使用計數器?
def post_to_words(raw_post):
HTML_text = BeautifulSoup(raw_post).get_text()
letters_only = re.sub("[^a-zA-Z]", " ", HTML_text)
words = letters_only.lower().split()
stops = set(stopwords.words("english"))
meaningful_words = [w for w in words if not w in stops]
return(" ".join(meaningful_words))
clean_Post_Text = post_to_words(fiance_forum["Post_Text"][0])
clean_Post_Text_split = clean_Post_Text.lower().split()
num_Post_Text = fiance_forum["Post_Text"].size
clean_posts_list = []
for i in range(0, num_Post_Text):
clean_posts_list.append(post_to_words(fiance_forum["Post_Text"][i]))
from collections import Counter
counts = Counter(clean_posts_list)
print(counts)
我的輸出是這樣的:u'please按照指示通知移動接收器「:1 我希望它看起來像這樣:
請:1
如下:1
指令:1
等等......非常感謝!
':1'是什麼? – Coder256
@ Coder256,Gina的代碼顯示unicode字符串的一個實例,而不是計算字符串中每個單詞的一個實例。 –
如果你想單獨列出單詞,你爲什麼要用'str.join'? –