Hướng dẫn word cloud python nltk - từ đám mây python nltk

Xử lý ngôn ngữ tự nhiên trong Python: Khám phá tần số từ với NLTK

Hướng dẫn từng bước để trực quan hóa các từ xuất hiện trong một văn bản.

Hình ảnh của tác giả (Word Cloud được tạo từ bài viết Wikipedia NLP)

Xử lý ngôn ngữ tự nhiên (NLP) được định nghĩa rộng rãi là sự thao túng ngôn ngữ của con người bằng phần mềm. Nó có nguồn gốc từ ngôn ngữ học nhưng đã phát triển để bao gồm khoa học máy tính và trí tuệ nhân tạo, với nghiên cứu NLP phần lớn dành cho các máy tính lập trình cho

Từ việc tạo một tập hợp các từ từ một kho văn bản trong R, người trả lời có thể dễ dàng chuyển đổi term-document matrix thành một đám mây từ một cách dễ dàng.

Có một chức năng tương tự từ các thư viện Python lấy một từ văn bản thô hoặc NLTK Corpus hoặc Gensim mmcorpus vào một đám mây từ?

Kết quả sẽ trông giống như thế này:

Hướng dẫn word cloud python nltk - từ đám mây python nltk

Đã hỏi ngày 20 tháng 5 năm 2013 lúc 8:51May 20, 2013 at 8:51

Hướng dẫn word cloud python nltk - từ đám mây python nltk

1

from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
stopwords = set(STOPWORDS)

def show_wordcloud(data, title = None):
    wordcloud = WordCloud(
        background_color='white',
        stopwords=stopwords,
        max_words=200,
        max_font_size=40, 
        scale=3,
        random_state=1 # chosen at random by flipping a coin; it was heads
    ).generate(str(data))

    fig = plt.figure(1, figsize=(12, 12))
    plt.axis('off')
    if title: 
        fig.suptitle(title, fontsize=20)
        fig.subplots_adjust(top=2.3)

    plt.imshow(wordcloud)
    plt.show()

show_wordcloud(Samsung_Reviews_Negative['Reviews'])
show_wordcloud(Samsung_Reviews_positive['Reviews'])

Hướng dẫn word cloud python nltk - từ đám mây python nltk

Kristada673

3.2905 Huy hiệu vàng33 Huy hiệu bạc83 Huy hiệu Đồng5 gold badges33 silver badges83 bronze badges

Đã trả lời ngày 12 tháng 2 năm 2018 lúc 16:20Feb 12, 2018 at 16:20

Hướng dẫn word cloud python nltk - từ đám mây python nltk

HeadandtailHeadandtailHeadAndTail

7948 Huy hiệu bạc9 Huy hiệu đồng8 silver badges9 bronze badges

1

Trong trường hợp bạn yêu cầu các đám mây từ này hiển thị chúng trong trang web hoặc ứng dụng web, bạn có thể chuyển đổi dữ liệu của mình thành định dạng JSON hoặc CSV và tải nó sang thư viện trực quan JavaScript như D3.Từ mây trên d3

Nếu không, câu trả lời của Marcin là một cách tốt để làm những gì bạn mô tả.

Đã trả lời ngày 28 tháng 5 năm 2013 lúc 19:26May 28, 2013 at 19:26

Hướng dẫn word cloud python nltk - từ đám mây python nltk

Valentinosvalentinosvalentinos

4282 Huy hiệu bạc11 Huy hiệu đồng2 silver badges11 bronze badges

Ví dụ về mã của Amueller đang hoạt động

Trong dòng lệnh / thiết bị đầu cuối:

sudo pip install wordcloud

Sau đó chạy tập lệnh Python:

## Simple WordCloud
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS 

text = 'all your base are belong to us all of your base base base'

def generate_wordcloud(text): # optionally add: stopwords=STOPWORDS and change the arg below
    wordcloud = WordCloud(font_path='/Library/Fonts/Verdana.ttf',
                          width=800, height=400,
                          relative_scaling = 1.0,
                          stopwords = {'to', 'of'} # set or space-separated string
                          ).generate(text)
    
    fig = plt.figure(1, figsize=(8, 4))
    plt.axis('off')
    plt.imshow(wordcloud)
    plt.axis("off")
    ## Pick One:
    # plt.show()
    plt.savefig("WordCloud.png")

generate_wordcloud(text)

Hướng dẫn word cloud python nltk - từ đám mây python nltk

Hướng dẫn word cloud python nltk - từ đám mây python nltk

Mruanova

5.4455 Huy hiệu vàng34 Huy hiệu bạc52 Huy hiệu Đồng5 gold badges34 silver badges52 bronze badges

Đã trả lời ngày 18 tháng 1 năm 2017 lúc 19:25Jan 18, 2017 at 19:25

MyopicvisagemyopicvisageMyopicVisage

1.25317 Huy hiệu bạc33 Huy hiệu đồng17 silver badges33 bronze badges

6

Đây là mã ngắn

#make wordcoud

from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
stopwords = set(STOPWORDS)

def show_wordcloud(data, title = None):
    wordcloud = WordCloud(
        background_color='white',
        stopwords=stopwords,
        max_words=200,
        max_font_size=40, 
        scale=3,
        random_state=1 # chosen at random by flipping a coin; it was heads
    ).generate(str(data))

    fig = plt.figure(1, figsize=(12, 12))
    plt.axis('off')
    if title: 
        fig.suptitle(title, fontsize=20)
        fig.subplots_adjust(top=2.3)

    plt.imshow(wordcloud)
    plt.show()


if __name__ == '__main__':

    show_wordcloud(text_str)   

Đã trả lời ngày 24 tháng 3 năm 2018 lúc 13:18Mar 24, 2018 at 13:18

Hướng dẫn word cloud python nltk - từ đám mây python nltk

cv = CountVectorizer()
cvData = cv.fit_transform(DF["W"]).toarray()
cvDF = pd.DataFrame(data=cvData,          columns=cv.get_feature_names())
cvDF["target"] = DF["T"]

def w_count(tar):
    MO = cvDF[cvDF["target"] == tar].drop("target",axis=1)
    x=[]
    y=[]
    for i in range(MO.shape[0]):
        for j in cvDF.drop("target",axis=1):
             if MO.iloc[i][j]>4:
                x.append(j)
                y.append(MO.iloc[i][j])
    return x,y

for i in cvDF["target"]:
    x,y = w_count(i)
    plt.figure(figsize=(10, 6))
    plt.title(i)
    plt.xticks(rotation="vertical")
    plt.bar(x,y)
    plt.show()

for c in range(len(DF)):
    w=[]
    for i,j in zip(cvDF.T[c].index, cvDF.T[c].values):
        a=[]
        if j > 1:
            a.append(i)
            a.append(j)
            w.append(a)
    pd.DataFrame(w)
    data = dict(w)
    wc = WordCloud(width=800, height=400, max_words=200).generate_from_frequencies(data)
    plt.figure(figsize=(10, 10))
    plt.imshow(wc, interpolation='bilinear')
    plt.axis('off')
    plt.title(DF['T'][c])
    plt.show()

Đã trả lời ngày 2 tháng 3 năm 2021 lúc 17:09Mar 2, 2021 at 17:09

1