我使用python中的Wordcloud包直接从文本文件生成词云。这是我从 stckoverflow 重新使用的代码:
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
def random_color_func(word=None, font_size=None, position=None, orientation=None, font_path=None, random_state=None):
h = int(360.0 * 45.0 / 255.0)
s = int(100.0 * 255.0 / 255.0)
l = int(100.0 * float(random_state.randint(60, 120)) / 255.0)
return "hsl({}, {}%, {}%)".format(h, s, l)
file_content=open ("xyz.txt").read()
wordcloud = WordCloud(font_path = r'C:\Windows\Fonts\Verdana.ttf',
stopwords = STOPWORDS,
background_color = 'white',
width = 1200,
height = 1000,
color_func = random_color_func
).generate(file_content)
plt.imshow(wordcloud,interpolation="bilinear")
plt.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud)
它给了我单个单词的 wordcloud。WordCloud() 函数中是否有任何参数可以在不格式化文本文件的情况下传递 n-gram。
我想要 bigram 的词云。或带有下划线的文字显示。喜欢:machine_learning(机器和学习是两个不同的词)