defsynonym_replacement(text): words = text.split() new_words = words.copy() for i inrange(len(words)): synonyms = wordnet.synsets(words[i]) if synonyms: new_words[i] = synonyms[0].lemmas()[0].name() return' '.join(new_words)
defrandom_deletion(text, p=0.1): words = text.split() iflen(words) == 1: return text new_words = [word for word in words if random.uniform(0, 1) > p] return' '.join(new_words)
数据可视化:文本数据可视化
词频统计:使用词云(Word Cloud)展示高频词。
1 2 3 4 5 6 7 8 9
from wordcloud import WordCloud import matplotlib.pyplot as plt