
v0のpythonで形態素解析&ワードクラウド表示!ーGoogleサイトで作るグループウェア(199)ー
🙇🏻いつも、Googleサイトで作るグループウェアを見ていただき、ありがとうございます!
この記事を読んで欲しい方
企業DXや学校DXの進め方に悩んでいる方
クラウドアプリの導入に悩んでいる方
自分だけのGoogleサイトを作ってみたい方
AIの利用に悩んでいる方
①v0のpythonネタ切れ?
みなさんこんにちは。
v0使ってますか?
少し前、v0でpythonが使えるようになったことをお伝えしましたが、noteやXでも、その後のpythonネタが出てきておらず、元?データサイエンティストとしては、つまらない日々を送っています。
また、pythonネタは専門的すぎて、AIにぎわし系の方々には、いいねや再生回数のアップにはつながらないためか、敬遠されがちです。
だがしかし、そこは昔取った杵柄で、v0でpythonネタを投下したいと思います。
実は、以下の投稿をたまたま見かけたので、v0のpythonでやってみたところうまくいきました。
②プロンプト公開
まず、形態素解析ですが、v0のpythonでは、日本語テキストはなかなか敷居が高い(python実行環境のメモリが少なくライブラリのインストールでパンクする?)ので、今回は英語テキストで実行します。
以下がプロンプトです。
「英語のサンプルテキストをつくり、形態素解析にかけて、ワードクラウドを表示してくれますか?必要な外部ライブラリは、pipでコード内でインストールしてください。」
超簡単ですよね。
流れを説明すると、
英語のテキスト>形態素解析で頻出の単語抽出>ワードクラウド表示
になります。
③実行
プロンプトを入力して実行すると、いつものように、つらつらとコードが作られます。

Run Codeボタンを押してpythonコードを実行します。
当初はエラーが出やすかったRun Codeですが、今はエラーもなく実行できて、安定しています。

AIが生成したpythonコードは以下に示しました。
import sys
import subprocess
import pkg_resources
required_packages = ['nltk', 'wordcloud', 'matplotlib']
installed_packages = {pkg.key for pkg in pkg_resources.working_set}
missing_packages = set(required_packages) - installed_packages
if missing_packages:
print("必要なパッケージをインストールしています...")
subprocess.check_call([sys.executable, '-m', 'pip', 'install', *missing_packages])
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# NLTKのデータをダウンロード
nltk.download('punkt')
nltk.download('stopwords')
# サンプルテキスト
sample_text = """
Artificial intelligence (AI) is intelligence demonstrated by machines, as opposed to natural intelligence displayed by animals including humans. AI research has been defined as the field of study of intelligent agents, which refers to any system that perceives its environment and takes actions that maximize its chance of achieving its goals.
The term "artificial intelligence" had previously been used to describe machines that mimic and display "human" cognitive skills that are associated with the human mind, such as "learning" and "problem-solving". This definition has since been rejected by major AI researchers who now describe AI in terms of rationality and acting rationally, which does not limit how intelligence can be articulated.
AI applications include advanced web search engines, recommendation systems, understanding human speech, self-driving cars, automated decision-making and competing at the highest level in strategic game systems. As machines become increasingly capable, tasks considered to require "intelligence" are often removed from the definition of AI, a phenomenon known as the AI effect.
"""
# テキストをトークン化
tokens = word_tokenize(sample_text)
# ストップワードを除去
stop_words = set(stopwords.words('english'))
filtered_tokens = [word.lower() for word in tokens if word.isalnum() and word.lower() not in stop_words]
# ワードクラウドを生成
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(' '.join(filtered_tokens))
# ワードクラウドを表示
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Word Cloud of AI Sample Text')
plt.show()
print("ワードクラウドが生成されました。")
Outputの最後にワードクラウドのPNG画像が表示されます。

これは、サンプルテキスト内の文字を頻度の多い方から、大きく表示したワードクラウドという表現手法になります。

④おわりに
いかがでしたでしょうか?
v0のpython環境の凄さがわかっていただけたでしょうか?
自然言語のプロンプトで指示するだけで、形態素解析からワードクラウドまで作成してくれる、データサイエンティストには夢のような環境なのです。
v0は、画面を通じたファイリングシステムやデータ保存の仕組みが不十分ですので、まだまだこれからです。
でも、v0のようにpythonアプリとWebアプリを自動生成できる環境は、今後もかなりの伸び代があることは言うまでもありません。
P.S メモリ容量について
以下のコードで、v0のpython環境のメモリ容量について調べました。
import os
import psutil
import sys
def get_memory_info():
print("メモリ情報:")
# 仮想メモリサイズ
print(f"仮想メモリサイズ: {sys.maxsize / (1024**3):.2f} GB")
# psutilが利用可能な場合
if hasattr(psutil, "virtual_memory"):
mem = psutil.virtual_memory()
print(f"全体メモリ: {mem.total / (1024**3):.2f} GB")
print(f"利用可能メモリ: {mem.available / (1024**3):.2f} GB")
print(f"使用中メモリ: {mem.used / (1024**3):.2f} GB")
print(f"メモリ使用率: {mem.percent}%")
# 現在のプロセスのメモリ使用量
process = psutil.Process(os.getpid())
print(f"現在のプロセスのメモリ使用量: {process.memory_info().rss / (1024**2):.2f} MB")
try:
get_memory_info()
except Exception as e:
print(f"メモリ情報の取得中にエラーが発生しました: {e}")
print("利用可能な情報:")
print(f"システムのビット数: {64 if sys.maxsize > 2**32 else 32}ビット")
if hasattr(sys, "getsizeof"):
print(f"int型のサイズ: {sys.getsizeof(0)} バイト")
print(f"float型のサイズ: {sys.getseof(0.0)} バイト")

メモリ情報: 仮想メモリサイズ: 8589934592.00 GB
全体メモリ: 0.96 GB
利用可能メモリ: 0.59 GB
使用中メモリ: 0.24 GB
メモリ使用率: 38.6%
現在のプロセスのメモリ使用量: 128.67 MB
1GBで動かしてる・・・スペック的にかなり厳しいですね・・・最低でも4GBにして!