Đám mây từ tích cực và tiêu cực trong python

Bất cứ khi nào bạn đề cập đến trực quan hóa trong Python, hầu hết mọi người sẽ bắt đầu nghĩ đến biểu đồ thanh, biểu đồ hình tròn, v.v. Nếu dữ liệu của bạn xoay quanh một tập hợp các mục, thì thực sự đây thường là những kiểu trực quan hóa mà bạn thường tạo. Nhưng nếu bạn đang làm việc với các từ và văn bản thì sao?

Câu trả lời là đám mây từ

Nói một cách đơn giản, đám mây từ là một hình ảnh chứa các từ được hiển thị ở các kích cỡ khác nhau. Kích thước của từ xác định tầm quan trọng của từ - từ càng to và đậm thì từ đó càng xuất hiện thường xuyên trong một khối văn bản nhất định

Cài đặt các mô-đun

Để cài đặt các mô-đun cần thiết để tạo các đám mây từ, hãy chạy lệnh sau trong Jupyter Notebook

!pip install wordcloud

Hãy tận dụng bộ dữ liệu IMDB có tại https. //www. kaggle. com/lakshmi25npathi/imdb-dataset-of-50k-movie-reviews/version/1

Sử dụng Pandas, tải tập dữ liệu dưới dạng DataFrame

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df

Khung dữ liệu có 2 cột và 50.000 hàng

Hãy lấy mẫu hàng đầu tiên và in ra nội dung

display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment

Cột đầu tiên của hàng chứa đánh giá về một bộ phim

"One of the other reviewers has mentioned that after watching just 1 Oz episode you'll be hooked. They are right, as this is exactly what happened with me.

The first thing that struck me about Oz was its brutality and unflinching scenes of violence, which set in right from the word GO. Trust me, this is not a show for the faint hearted or timid. This show pulls no punches with regards to drugs, sex or violence. Its is hardcore, in the classic use of the word.

It is called OZ as that is the nickname given to the Oswald Maximum Security State Penitentary. It focuses mainly on Emerald City, an experimental section of the prison where all the cells have glass fronts and face inwards, so privacy is not high on the agenda. Em City is home to many..Aryans, Muslims, gangstas, Latinos, Christians, Italians, Irish and more....so scuffles, death stares, dodgy dealings and shady agreements are never far away.

I would say the main appeal of the show is due to the fact that it goes where other shows wouldn't dare. Forget pretty pictures painted for mainstream audiences, forget charm, forget romance...OZ doesn't mess around. The first episode I ever saw struck me as so nasty it was surreal, I couldn't say I was ready for it, but as I watched more, I developed a taste for Oz, and got accustomed to the high levels of graphic violence. Not just violence, but injustice (crooked guards who'll be sold out for a nickel, inmates who'll kill on order and get away with it, well mannered, middle class inmates being turned into prison bitches due to their lack of street skills or prison experience) Watching Oz, you may become comfortable with what is uncomfortable viewing....thats if you can get in touch with your darker side."

Cột thứ hai thể hiện cảm xúc của bài đánh giá (cho dù đó là bài đánh giá tích cực hay tiêu cực)

'positive'
Vẽ sơ đồ đám mây từ

Mô-đun wordcloud mà chúng ta sẽ sử dụng sẽ tạo ra một đám mây từ dưới dạng hình ảnh. Để hiển thị hình ảnh, chúng ta có thể sử dụng matplotlib. Đối với điều này, hãy định nghĩa một hàm có tên là

display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
2

import matplotlib.pyplot as pltdef draw_wordcloud(wordcloud, size):
plt.figure(figsize = size)
plt.imshow(wordcloud)
plt.axis("off")

Hàm nhận hai đối số — đối số đầu tiên chứa từ hình ảnh đám mây và đối số thứ hai là kích thước (một bộ chứa chiều rộng và chiều cao) của hình ảnh cần vẽ

Tiếp theo, hãy nhập lớp

display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
3 và bộ
display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
4, cũng như mô-đun
display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
5

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
0

Từ dừng về cơ bản là những từ thường được sử dụng trong bất kỳ ngôn ngữ nào. Bộ

display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
4 chứa danh sách các từ thông dụng

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
2

Từ văn bản đánh giá mà chúng ta đã xem trước đó, có một số trường hợp của từ như “

display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
7”, “
display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
8” và
display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
9, vì vậy hãy thêm chúng vào biến
display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
4 của chúng ta

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
7

Chúng tôi cũng muốn chọn ngẫu nhiên một bài đánh giá từ khung dữ liệu của mình

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
8

Cuối cùng, tạo đám mây từ bằng cách tạo một thể hiện của lớp

display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
3 và sau đó gọi phương thức
"One of the other reviewers has mentioned that after watching just 1 Oz episode you'll be hooked. They are right, as this is exactly what happened with me.

The first thing that struck me about Oz was its brutality and unflinching scenes of violence, which set in right from the word GO. Trust me, this is not a show for the faint hearted or timid. This show pulls no punches with regards to drugs, sex or violence. Its is hardcore, in the classic use of the word.

It is called OZ as that is the nickname given to the Oswald Maximum Security State Penitentary. It focuses mainly on Emerald City, an experimental section of the prison where all the cells have glass fronts and face inwards, so privacy is not high on the agenda. Em City is home to many..Aryans, Muslims, gangstas, Latinos, Christians, Italians, Irish and more....so scuffles, death stares, dodgy dealings and shady agreements are never far away.

I would say the main appeal of the show is due to the fact that it goes where other shows wouldn't dare. Forget pretty pictures painted for mainstream audiences, forget charm, forget romance...OZ doesn't mess around. The first episode I ever saw struck me as so nasty it was surreal, I couldn't say I was ready for it, but as I watched more, I developed a taste for Oz, and got accustomed to the high levels of graphic violence. Not just violence, but injustice (crooked guards who'll be sold out for a nickel, inmates who'll kill on order and get away with it, well mannered, middle class inmates being turned into prison bitches due to their lack of street skills or prison experience) Watching Oz, you may become comfortable with what is uncomfortable viewing....thats if you can get in touch with your darker side."
2 của nó

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
0

Lưu ý các tham số khác nhau được cung cấp cho trình khởi tạo của lớp

display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
3

  • tham số
    "One of the other reviewers has mentioned that after watching just 1 Oz episode you'll be hooked. They are right, as this is exactly what happened with me.

    The first thing that struck me about Oz was its brutality and unflinching scenes of violence, which set in right from the word GO. Trust me, this is not a show for the faint hearted or timid. This show pulls no punches with regards to drugs, sex or violence. Its is hardcore, in the classic use of the word.

    It is called OZ as that is the nickname given to the Oswald Maximum Security State Penitentary. It focuses mainly on Emerald City, an experimental section of the prison where all the cells have glass fronts and face inwards, so privacy is not high on the agenda. Em City is home to many..Aryans, Muslims, gangstas, Latinos, Christians, Italians, Irish and more....so scuffles, death stares, dodgy dealings and shady agreements are never far away.

    I would say the main appeal of the show is due to the fact that it goes where other shows wouldn't dare. Forget pretty pictures painted for mainstream audiences, forget charm, forget romance...OZ doesn't mess around. The first episode I ever saw struck me as so nasty it was surreal, I couldn't say I was ready for it, but as I watched more, I developed a taste for Oz, and got accustomed to the high levels of graphic violence. Not just violence, but injustice (crooked guards who'll be sold out for a nickel, inmates who'll kill on order and get away with it, well mannered, middle class inmates being turned into prison bitches due to their lack of street skills or prison experience) Watching Oz, you may become comfortable with what is uncomfortable viewing....thats if you can get in touch with your darker side."
    4 và
    "One of the other reviewers has mentioned that after watching just 1 Oz episode you'll be hooked. They are right, as this is exactly what happened with me.

    The first thing that struck me about Oz was its brutality and unflinching scenes of violence, which set in right from the word GO. Trust me, this is not a show for the faint hearted or timid. This show pulls no punches with regards to drugs, sex or violence. Its is hardcore, in the classic use of the word.

    It is called OZ as that is the nickname given to the Oswald Maximum Security State Penitentary. It focuses mainly on Emerald City, an experimental section of the prison where all the cells have glass fronts and face inwards, so privacy is not high on the agenda. Em City is home to many..Aryans, Muslims, gangstas, Latinos, Christians, Italians, Irish and more....so scuffles, death stares, dodgy dealings and shady agreements are never far away.

    I would say the main appeal of the show is due to the fact that it goes where other shows wouldn't dare. Forget pretty pictures painted for mainstream audiences, forget charm, forget romance...OZ doesn't mess around. The first episode I ever saw struck me as so nasty it was surreal, I couldn't say I was ready for it, but as I watched more, I developed a taste for Oz, and got accustomed to the high levels of graphic violence. Not just violence, but injustice (crooked guards who'll be sold out for a nickel, inmates who'll kill on order and get away with it, well mannered, middle class inmates being turned into prison bitches due to their lack of street skills or prison experience) Watching Oz, you may become comfortable with what is uncomfortable viewing....thats if you can get in touch with your darker side."
    5 chỉ định kích thước của đám mây từ tính bằng pixel
  • tham số
    "One of the other reviewers has mentioned that after watching just 1 Oz episode you'll be hooked. They are right, as this is exactly what happened with me.

    The first thing that struck me about Oz was its brutality and unflinching scenes of violence, which set in right from the word GO. Trust me, this is not a show for the faint hearted or timid. This show pulls no punches with regards to drugs, sex or violence. Its is hardcore, in the classic use of the word.

    It is called OZ as that is the nickname given to the Oswald Maximum Security State Penitentary. It focuses mainly on Emerald City, an experimental section of the prison where all the cells have glass fronts and face inwards, so privacy is not high on the agenda. Em City is home to many..Aryans, Muslims, gangstas, Latinos, Christians, Italians, Irish and more....so scuffles, death stares, dodgy dealings and shady agreements are never far away.

    I would say the main appeal of the show is due to the fact that it goes where other shows wouldn't dare. Forget pretty pictures painted for mainstream audiences, forget charm, forget romance...OZ doesn't mess around. The first episode I ever saw struck me as so nasty it was surreal, I couldn't say I was ready for it, but as I watched more, I developed a taste for Oz, and got accustomed to the high levels of graphic violence. Not just violence, but injustice (crooked guards who'll be sold out for a nickel, inmates who'll kill on order and get away with it, well mannered, middle class inmates being turned into prison bitches due to their lack of street skills or prison experience) Watching Oz, you may become comfortable with what is uncomfortable viewing....thats if you can get in touch with your darker side."
    6 đảm bảo rằng đám mây từ bạn tạo với cùng một văn bản luôn giống nhau. Nếu tham số này không được đặt, bạn sẽ nhận được một đám mây từ khác mỗi khi bạn chạy mã
  • tham số
    "One of the other reviewers has mentioned that after watching just 1 Oz episode you'll be hooked. They are right, as this is exactly what happened with me.

    The first thing that struck me about Oz was its brutality and unflinching scenes of violence, which set in right from the word GO. Trust me, this is not a show for the faint hearted or timid. This show pulls no punches with regards to drugs, sex or violence. Its is hardcore, in the classic use of the word.

    It is called OZ as that is the nickname given to the Oswald Maximum Security State Penitentary. It focuses mainly on Emerald City, an experimental section of the prison where all the cells have glass fronts and face inwards, so privacy is not high on the agenda. Em City is home to many..Aryans, Muslims, gangstas, Latinos, Christians, Italians, Irish and more....so scuffles, death stares, dodgy dealings and shady agreements are never far away.

    I would say the main appeal of the show is due to the fact that it goes where other shows wouldn't dare. Forget pretty pictures painted for mainstream audiences, forget charm, forget romance...OZ doesn't mess around. The first episode I ever saw struck me as so nasty it was surreal, I couldn't say I was ready for it, but as I watched more, I developed a taste for Oz, and got accustomed to the high levels of graphic violence. Not just violence, but injustice (crooked guards who'll be sold out for a nickel, inmates who'll kill on order and get away with it, well mannered, middle class inmates being turned into prison bitches due to their lack of street skills or prison experience) Watching Oz, you may become comfortable with what is uncomfortable viewing....thats if you can get in touch with your darker side."
    7 được đặt thành
    "One of the other reviewers has mentioned that after watching just 1 Oz episode you'll be hooked. They are right, as this is exactly what happened with me.

    The first thing that struck me about Oz was its brutality and unflinching scenes of violence, which set in right from the word GO. Trust me, this is not a show for the faint hearted or timid. This show pulls no punches with regards to drugs, sex or violence. Its is hardcore, in the classic use of the word.

    It is called OZ as that is the nickname given to the Oswald Maximum Security State Penitentary. It focuses mainly on Emerald City, an experimental section of the prison where all the cells have glass fronts and face inwards, so privacy is not high on the agenda. Em City is home to many..Aryans, Muslims, gangstas, Latinos, Christians, Italians, Irish and more....so scuffles, death stares, dodgy dealings and shady agreements are never far away.

    I would say the main appeal of the show is due to the fact that it goes where other shows wouldn't dare. Forget pretty pictures painted for mainstream audiences, forget charm, forget romance...OZ doesn't mess around. The first episode I ever saw struck me as so nasty it was surreal, I couldn't say I was ready for it, but as I watched more, I developed a taste for Oz, and got accustomed to the high levels of graphic violence. Not just violence, but injustice (crooked guards who'll be sold out for a nickel, inmates who'll kill on order and get away with it, well mannered, middle class inmates being turned into prison bitches due to their lack of street skills or prison experience) Watching Oz, you may become comfortable with what is uncomfortable viewing....thats if you can get in touch with your darker side."
    8 để đám mây từ không chứa chữ lớn hoặc từ trùng lặp

Một bigram là một cặp đơn vị viết liên tiếp như chữ cái, âm tiết hoặc từ. e. g. “San Francisco”, “đi bộ chậm” và “đánh răng” là tất cả các ví dụ về bigram

  • tham số
    "One of the other reviewers has mentioned that after watching just 1 Oz episode you'll be hooked. They are right, as this is exactly what happened with me.

    The first thing that struck me about Oz was its brutality and unflinching scenes of violence, which set in right from the word GO. Trust me, this is not a show for the faint hearted or timid. This show pulls no punches with regards to drugs, sex or violence. Its is hardcore, in the classic use of the word.

    It is called OZ as that is the nickname given to the Oswald Maximum Security State Penitentary. It focuses mainly on Emerald City, an experimental section of the prison where all the cells have glass fronts and face inwards, so privacy is not high on the agenda. Em City is home to many..Aryans, Muslims, gangstas, Latinos, Christians, Italians, Irish and more....so scuffles, death stares, dodgy dealings and shady agreements are never far away.

    I would say the main appeal of the show is due to the fact that it goes where other shows wouldn't dare. Forget pretty pictures painted for mainstream audiences, forget charm, forget romance...OZ doesn't mess around. The first episode I ever saw struck me as so nasty it was surreal, I couldn't say I was ready for it, but as I watched more, I developed a taste for Oz, and got accustomed to the high levels of graphic violence. Not just violence, but injustice (crooked guards who'll be sold out for a nickel, inmates who'll kill on order and get away with it, well mannered, middle class inmates being turned into prison bitches due to their lack of street skills or prison experience) Watching Oz, you may become comfortable with what is uncomfortable viewing....thats if you can get in touch with your darker side."
    9 đặt màu nền của đám mây từ
  • tham số
    'positive'
    0 đặt bản đồ màu được sử dụng để hiển thị các từ trong đám mây từ. Tham khảo https. // matplotlib. org/ổn định/hướng dẫn/màu sắc/bản đồ màu. html để biết danh sách các bản đồ màu mà bạn có thể sử dụng
  • tham số
    'positive'
    1 đặt các từ dừng cho đám mây từ. Trong trường hợp này, đám mây từ được tạo sẽ loại trừ danh sách các từ dừng được cung cấp (
    display(df.iloc[0,0])   # review
    display(df.iloc[0,1]) # sentiment
    4). Xóa các từ dừng trong đám mây từ cho phép chúng tôi tập trung vào các từ quan trọng thay thế

Đây là một ví dụ về một bản đồ màu trong matplotlib

Nguồn. https. // matplotlib. org/ổn định/hướng dẫn/màu sắc/bản đồ màu. html

Khi đám mây từ được tạo, bạn cần hiển thị nó. Bạn sẽ sử dụng hàm

display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
2 mà bạn đã xác định trước đó

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
1

Từ đám mây trông như thế này

Bạn cũng có thể lưu đám mây từ vào một tệp bằng phương pháp

'positive'
4

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
2Tùy chỉnh Word Clouds dựa trên Đánh giá cảm tính

Hãy nhớ rằng mỗi bài đánh giá đều có một ý kiến ​​— tích cực hoặc tiêu cực. Hãy nhóm khung dữ liệu theo

'positive'
5, sau đó lấy mẫu hai bài đánh giá tích cực và hai bài đánh giá tiêu cực và vẽ một đám mây từ cho mỗi bài đánh giá. Để phân biệt giữa đánh giá tích cực và tiêu cực, chúng tôi sẽ làm như sau

  • đặt nền màu hồng cho đánh giá tiêu cực và màu đen cho đánh giá tích cực
  • sử dụng bản đồ màu
    'positive'
    6 để đánh giá tiêu cực và bản đồ màu
    'positive'
    7 để đánh giá tích cực

Đoạn mã sau tạo ra các đám mây từ cho bốn bài phê bình phim

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
3

Hai đám mây từ sau đây là những đánh giá tiêu cực

Hai đám mây từ tiếp theo là những đánh giá tích cực

Sử dụng Mặt nạ cho Word Cloud

Mô-đun wordcloud có một số tính năng thực sự gọn gàng mà bạn có thể sử dụng để làm cho đám mây từ của mình nổi bật — bạn có thể áp dụng mặt nạ để tạo đám mây từ theo các hình dạng tùy ý

Tốt nhất, bạn nên sử dụng mặt nạ có hình ảnh hoàn toàn đen trắng

Là một bồi dưỡng về những điều cơ bản của hình ảnh

  • Đối với một pixel màu đen, các giá trị RGB của nó đều bằng 0
  • Đối với một pixel màu trắng, các giá trị RGB của nó đều là 255

Tại sao điều này quan trọng để biết? . e. bất kỳ pixel nào không phải là (255,255,255) sẽ được sử dụng để vẽ đám mây từ

Hãy thử điều này bằng cách sử dụng hình ảnh này của logo Phương tiện

Nguồn. https. //Trung bình. com/blogging-guide/mediums-new-logo-2020-unfinished-ellipses-logomark-e24c059871e7

Đoạn mã sau tải hình ảnh Phương tiện dưới dạng một mảng NumPy và sau đó chuyển nó đến tham số

'positive'
8 của lớp
display(df.iloc[0,0])   # review
display(df.iloc[0,1]) # sentiment
3

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
4

Lưu ý rằng khi bạn sử dụng mặt nạ để tạo đám mây từ, các tham số

"One of the other reviewers has mentioned that after watching just 1 Oz episode you'll be hooked. They are right, as this is exactly what happened with me.

The first thing that struck me about Oz was its brutality and unflinching scenes of violence, which set in right from the word GO. Trust me, this is not a show for the faint hearted or timid. This show pulls no punches with regards to drugs, sex or violence. Its is hardcore, in the classic use of the word.

It is called OZ as that is the nickname given to the Oswald Maximum Security State Penitentary. It focuses mainly on Emerald City, an experimental section of the prison where all the cells have glass fronts and face inwards, so privacy is not high on the agenda. Em City is home to many..Aryans, Muslims, gangstas, Latinos, Christians, Italians, Irish and more....so scuffles, death stares, dodgy dealings and shady agreements are never far away.

I would say the main appeal of the show is due to the fact that it goes where other shows wouldn't dare. Forget pretty pictures painted for mainstream audiences, forget charm, forget romance...OZ doesn't mess around. The first episode I ever saw struck me as so nasty it was surreal, I couldn't say I was ready for it, but as I watched more, I developed a taste for Oz, and got accustomed to the high levels of graphic violence. Not just violence, but injustice (crooked guards who'll be sold out for a nickel, inmates who'll kill on order and get away with it, well mannered, middle class inmates being turned into prison bitches due to their lack of street skills or prison experience) Watching Oz, you may become comfortable with what is uncomfortable viewing....thats if you can get in touch with your darker side."
4 và
"One of the other reviewers has mentioned that after watching just 1 Oz episode you'll be hooked. They are right, as this is exactly what happened with me.

The first thing that struck me about Oz was its brutality and unflinching scenes of violence, which set in right from the word GO. Trust me, this is not a show for the faint hearted or timid. This show pulls no punches with regards to drugs, sex or violence. Its is hardcore, in the classic use of the word.

It is called OZ as that is the nickname given to the Oswald Maximum Security State Penitentary. It focuses mainly on Emerald City, an experimental section of the prison where all the cells have glass fronts and face inwards, so privacy is not high on the agenda. Em City is home to many..Aryans, Muslims, gangstas, Latinos, Christians, Italians, Irish and more....so scuffles, death stares, dodgy dealings and shady agreements are never far away.

I would say the main appeal of the show is due to the fact that it goes where other shows wouldn't dare. Forget pretty pictures painted for mainstream audiences, forget charm, forget romance...OZ doesn't mess around. The first episode I ever saw struck me as so nasty it was surreal, I couldn't say I was ready for it, but as I watched more, I developed a taste for Oz, and got accustomed to the high levels of graphic violence. Not just violence, but injustice (crooked guards who'll be sold out for a nickel, inmates who'll kill on order and get away with it, well mannered, middle class inmates being turned into prison bitches due to their lack of street skills or prison experience) Watching Oz, you may become comfortable with what is uncomfortable viewing....thats if you can get in touch with your darker side."
5 không còn cần thiết nữa vì kích thước của đám mây từ giờ đây sẽ dựa trên kích thước của mặt nạ. Bạn sẽ thấy đầu ra sau

Một ví dụ khác, hãy xem xét hình ảnh sau

nguồn hình ảnh. https. // vi. từ điển mở. tổ chức/wiki/quả địa cầu

Chúng tôi sẽ áp dụng hình ảnh này làm mặt nạ của đám mây từ được tạo

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
5

Đám mây từ sẽ trông như thế này

Tạo đám mây từ cho bài phát biểu chiến thắng của Obama và Trump

Tôi sẽ kết thúc bài viết này bằng cách tạo ra các đám mây từ cho các bài phát biểu Chiến thắng của Obama và Trump. Bài phát biểu của họ có thể được tìm thấy ở đây

  • Bài phát biểu của Barack Obama năm 2012 — https. //www. người giám hộ. com/world/2012/nov/07/barack-obama-speech-full-text
  • Bài phát biểu của Donald Trump năm 2016 — https. //phiên bản. cnn. com/2016/11/09/politics/donald-trump-victory-speech/index. html

Những gì tôi đã làm là trích xuất các bài phát biểu của họ và lưu chúng vào hai tệp văn bản riêng biệt

  • Bài phát biểu của Obama 2012. txt
  • Bài phát biểu của Trump 2016. txt

Hãy tạo đám mây từ cho bài phát biểu chiến thắng của Obama vào năm 2012

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
6

Đây là từ đám mây

Tiếp theo, tạo đám mây từ cho bài phát biểu chiến thắng của Donald Trump năm 2016

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
7

Đây là từ đám mây

Nếu bạn muốn danh sách các từ và tần suất tương đối của chúng (với các từ khác), hãy sử dụng thuộc tính

import matplotlib.pyplot as pltdef draw_wordcloud(wordcloud, size):
plt.figure(figsize = size)
plt.imshow(wordcloud)
plt.axis("off")
2 của đám mây từ được tạo

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
8

Bạn sẽ thấy một danh sách như thế này

import pandas as pd
import numpy as np
df = pd.read_csv('IMDB Dataset.csv')
df
9

Nếu bạn chỉ muốn đếm tần suất của các từ được sử dụng trong bài phát biểu, thì bạn thậm chí không cần tạo một đám mây từ;