[SnowNLP] Python3で中国語の自然言語処理

$ pip3 install snownlp

tokenization

from snownlp import SnowNLP

s = SnowNLP(u'今天是周六。')
print(s.words)

$ python3 snow.py
[‘今天’, ‘是’, ‘周六’, ‘。’]

speech tagにするとnoun, adverb, verb, adjectiveなどを表現できます。

print(list(s.tags))

$ python3 snow.py
[(‘今天’, ‘t’), (‘是’, ‘v’), (‘周六’, ‘t’), (‘。’, ‘w’)]

pinyin

print(s.pinyin)

$ python3 snow.py
[‘jin’, ‘tian’, ‘shi’, ‘zhou’, ‘liu’, ‘。’]

sentences

s = SnowNLP(u'在茂密的大森林里,一只饥饿的老虎逮住了一只狐狸。老虎张开大嘴就要把狐狸吃掉。"慢着"!狐狸虽然很害怕但还是装出一副很神气的样子说,"你知道我是谁吗?我可是玉皇大帝派来管理百兽的兽王,你要是吃了我,玉皇大帝是决不会放过你的"。')
print(s.sentences)

[‘在茂密的大森林里’, ‘一只饥饿的老虎逮住了一只狐狸’, ‘老虎张开大嘴就要把狐狸吃掉’, ‘”慢着”‘, ‘狐狸虽然很害怕但还是装出一副很神气的样子说’, ‘”你知道我是谁吗’, ‘我可是玉皇大帝派来管理百兽的兽王’, ‘你要是吃了我’, ‘玉皇大帝是决不会放过你的”‘]

keyword

print(s.keywords(5))

$ python3 snow.py
[‘狐狸’, ‘大’, ‘老虎’, ‘大帝’, ‘皇’]

summary

print(s.summary(3))

[‘老虎张开大嘴就要把狐狸吃掉’, ‘我可是玉皇大帝派来管理百兽的兽王’, ‘玉皇大帝是决不会放过你的”‘]

sentiment analysis

text = SnowNLP(u'这个产品很好用,这个产品不好用,这个产品是垃圾,这个也太贵了吧,超级垃圾,是个垃圾中的垃圾')
sent = text.sentences
for sen in sent:
	s = SnowNLP(sen)
	print(s.sentiments)

$ python3 snow.py
0.7853504415636449
0.5098208142944668
0.13082804652201174
0.5
0.0954842128485538
0.04125325276132508

0から1の値を取り、1に近づくほどポジティブ、0に近いほどネガティブとなります。

[laravel8.5.19] LaravelでPython3を実行する書き方

Laravelで作成するアプリケーションで、一部の言語に関する処理のところをPython(自然言語処理)で実行したい。
方法としては、1.PHP組み込み関数のexec関数を使用する、2.symfony/processライブラリを使う の2通りがあるようだ。
今回は「exec関数」を使用する。python実行のテストのみなので、以下簡略化して記述している。

### exec関数とは?
exec関数はPHPの組み込み関数で、外部プログラムを実行できる

### Laravelプロジェクトインストール
$ composer create-project –prefer-dist laravel/laravel laravel-python
$ cd laravel-python
$ composer require laravel/jetstream
$ php artisan jetstream:install livewire
$ npm install && npm run dev
$ php artisan migrate
$ php artisan serve –host 192.168.33.10 –port 8000

### Routing
web.php
 L index.bladeからpostした際に、ExecControllerのexecutePython() メソッドでpythonを実行する

use App\Http\Controllers\ExecController;
use App\Http\Controllers\TestController;

Route::post('/python', [ExecController::class, 'executePython']);
Route::get('/test', [TestController::class, 'index']);

### View
index.blade.php
 L フォームのactionから、ExecController@executePythonにpostする

	<h1>Test</h1>
	<form action="python" method="post">
		@csrf
		<input type="submit" name="submit">
	</form>

### Controller
ExecController.php
L exec($command, $output);で実行する。
L $commandは、「python3 ${絶対パス}.py」で実行する
L laravelでは、app_path()でappディレクトリのpathを取得するので、app_path()とする
L execの第二引数である$outputは、コマンドで*.pyを実行した際の出力が返ってくる。

class ExecController extends Controller
{
    //
    public function executePython(Request $request){
    	$path = app_path() . "/Python/app.py";
    	$command = "python3 " . $path;
    	exec($command, $output);
    	dd($output);
    	// return view('index', compact('output'));
    }
}

※テストなのでdd($output)で出力を確認します。

laravel-python/app/Python/app.py
※今回はlaravelプロジェクトのapp/Python/配下にapp.pyを作成した。絶対パスなので、どこれも良いっぽいが、appフォルダに作るのが一般的のよう。今回はテストなので単にprint()するだけにした

print("hello")

### Laravelでpythonを実行した実行結果
bladeで「送信」ボタンを押すと、pythonで*.pyを実行する

実行後
L 配列で返ってくる

返ってきた変数(値)は、viewに再度返却できますね。
うおおおおおおおおおおおおおおおおおおおおおおおおお
やべえええええええええええええええええ
やりたいこと(自然言語処理)の9割くらいはイメージできた!!!!!!!!!!!!!

当初、Pythonの処理(nlpなど)の部分は、サブドメインを取得して別サーバを立ててDjangoで実装しようかと思ってたが、Laravelでpythonを実行できるなら、わざわざそんな冗長なことしなくて良いですね。
よっしゃああああああああああああああああああああああ
設計書作るぞーーーーーーーー

Pythonで英文のデータセットを使ってTextClassificationをしたい1

#### choosing a Data Set
Sentiment Labelled Sentences Data Set
https://archive.ics.uci.edu/ml/machine-learning-databases/00331/

※Yelpはビジネスレビューサイト(食べログのようなもの)
※imdbは映画、テレビなどのレビューサイト

こちらから、英文のポジティブ、ネガティブのデータセットを取得します。
$ ls
amazon_cells_labelled.txt imdb_labelled.txt readme.txt yelp_labelled.txt

import pandas as pd 

filepath_dict = {
	'yelp': 'data/yelp_labelled.txt',
	'amazon': 'data/amazon_cells_labelled.txt',
	'imdb': 'data/imdb_labelled.txt'
}

df_list = []
for source, filepath in filepath_dict.items():
	df = pd.read_csv(filepath, names=['sentence', 'label'], sep='\t')
	df['source'] = source
	df_list.append(df)

df = pd.concat(df_list)
print(df.iloc[0])

$ python3 app.py
sentence Wow… Loved this place.
label 1
source yelp
Name: 0, dtype: object

This data, predict sentiment of sentence.
vocabularyごとにベクトル化して重みを学習して判定する
>>> sentences = [‘John likes ice cream’, ‘John hates chocolate.’]
>>> from sklearn.feature_extraction.text import CountVectorizer
>>> vectorizer = CountVectorizer(min_df=0, lowercase=False)
>>> vectorizer.fit(sentences)
CountVectorizer(lowercase=False, min_df=0)
>>> vectorizer.vocabulary_
{‘John’: 0, ‘likes’: 5, ‘ice’: 4, ‘cream’: 2, ‘hates’: 3, ‘chocolate’: 1}
>>> vectorizer.transform(sentences).toarray()
array([[1, 0, 1, 0, 1, 1],
[1, 1, 0, 1, 0, 0]])

### Defining Baseline Model
First, split the data into a training and testing set

from sklearn.model_selection import train_test_split
import pandas as pd 

filepath_dict = {
	'yelp': 'data/yelp_labelled.txt',
	'amazon': 'data/amazon_cells_labelled.txt',
	'imdb': 'data/imdb_labelled.txt'
}

df_list = []
for source, filepath in filepath_dict.items():
	df = pd.read_csv(filepath, names=['sentence', 'label'], sep='\t')
	df['source'] = source
	df_list.append(df)

df = pd.concat(df_list)

df_yelp = df[df['source'] == 'yelp']
sentences = df_yelp['sentence'].values
y = df_yelp['label'].values

sentences_train, sentences_test, y_train, y_test = train_test_split(
	sentences, y, test_size=0.25, random_state=1000)

.value return NumPy array

from sklearn.feature_extraction.text import CountVectorizer

// 省略

sentences_train, sentences_test, y_train, y_test = train_test_split(
	sentences, y, test_size=0.25, random_state=1000)

vectorizer = CountVectorizer()
vectorizer.fit(sentences_train)

X_train = vectorizer.transform(sentences_train)
X_test = vectorizer.transform(sentences_test)
print(X_train)

$ python3 split.py
(0, 125) 1
(0, 145) 1
(0, 201) 1
(0, 597) 1
(0, 600) 1
(0, 710) 1
(0, 801) 2
(0, 888) 1
(0, 973) 1
(0, 1042) 1
(0, 1308) 1
(0, 1345) 1
(0, 1360) 1
(0, 1494) 2
(0, 1524) 2
(0, 1587) 1
(0, 1622) 1
(0, 1634) 1
(1, 63) 1
(1, 136) 1
(1, 597) 1
(1, 616) 1
(1, 638) 1
(1, 725) 1
(1, 1001) 1
: :
(746, 1634) 1
(747, 42) 1
(747, 654) 1
(747, 1193) 2
(747, 1237) 1
(747, 1494) 1
(747, 1520) 1
(748, 600) 1
(748, 654) 1
(748, 954) 1
(748, 1001) 1
(748, 1494) 1
(749, 14) 1
(749, 15) 1
(749, 57) 1
(749, 108) 1
(749, 347) 1
(749, 553) 1
(749, 675) 1
(749, 758) 1
(749, 801) 1
(749, 1010) 1
(749, 1105) 1
(749, 1492) 1
(749, 1634) 2

#### LogisticRegression

from sklearn.linear_model import LogisticRegression

classifier = LogisticRegression()
classifier.fit(X_train, y_train)
score = classifier.score(X_test, y_test)

print("Accuracy:", score)

$ python3 split.py
Accuracy: 0.796

for source in df['source'].unique():
	df_source = df[df['source'] == source]
	sentences = df_source['sentence'].values
	y = df_source['label'].values

	sentences_train, sentences_test, y_train, y_test = train_test_split(
		sentences, y, test_size=0.25, random_state=1000)

	vectorizer = CountVectorizer()
	vectorizer.fit(sentences_train)
	X_train = vectorizer.transform(sentences_train)
	X_test = vectorizer.transform(sentences_test)

	classifier = LogisticRegression()
	classifier.fit(X_train, y_train)
	score = classifier.score(X_test, y_test)
	print('Accuracy for {} data: {:.4f}'.format(source, score))

$ python3 split.py
Accuracy for yelp data: 0.7960
Accuracy for amazon data: 0.7960
Accuracy for imdb data: 0.7487

[python3] フォームのデータを受信し表示

index.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<h1>翻訳したい言語を入力してください</h1>
	<form method="POST" action="result.py">
		<label>テキスト:</label>
		<textarea name="text"></textarea>
		<button type="submit">送信</button>
	</form>
</body>
</html>

result.py

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import cgi
import cgitb
import sys

cgitb.enable()

form = cgi.FieldStorage()

print("Content-Type: text/html; charset=UTF-8")
print("")

if "text" not in form:
	print("<h1>Erro!</h1>")
	print("<br>")
	print("テキストを入力してください!")
	print("<a href='/'><button type='submit'>戻る</button></a>")
	sys.exit()

text = form.getvalue("text")
print(text)

ん。。。なんか上手くいかんな。

[python3] googletransで日本語->英語に翻訳する

$ sudo pip3 install googletrans
Successfully installed chardet-3.0.4 contextvars-2.4 googletrans-3.0.0 h11-0.9.0 h2-3.2.0 hpack-3.0.0 hstspreload-2020.12.22 httpcore-0.9.1 httpx-0.13.3 hyperframe-5.2.0 immutables-0.15 rfc3986-1.4.0 sniffio-1.2.0

# -*- coding: utf-8 -*-
from googletrans import Translator
translator = Translator()

translation = translator.translate("こんにちは", src='ja', dest="en")
print(translation.text)

$ python3 app.py
AttributeError: ‘NoneType’ object has no attribute ‘group’

どうやら3.0.0ではなく、4系が動いているとのこと
$ sudo pip3 uninstall googletrans
$ sudo pip3 install googletrans==4.0.0-rc1

# -*- coding: utf-8 -*-
from googletrans import Translator
translator = Translator()

translation = translator.translate("h音楽アーティストやレコードレーベルが保有する楽曲を NFT 化し世界中に販売", src='ja', dest='en')
print(translation.text)

$ python3 app.py
Music artists and songs owned by record labels NFT and sell worldwide

なかなか凄いな
formからpostして翻訳して返却したいな

[python3] twitterのつぶやきからmecabとwordcloudで解析する

# -*- coding: utf-8 -*-
import MeCab
import matplotlib.pyplot as plt
import csv
from wordcloud import WordCloud

dfile = "test.txt"

fname = r"'" + dfile + "'"
fname = fname.replace("'","")

mecab = MeCab.Tagger("-Owakati")

words = []

with open(fname, 'r', encoding="utf-8") as f:

	reader = f.readline()

	while reader:

		node = mecab.parseToNode(reader)

		while node:
			word_type = node.feature.split(",")[0]

			if word_type in ["名詞", "動詞", "形容詞", "副詞"]:

				words.append(node.surface)

			node = node.next

		reader = f.readline()

font_path = "NotoSansMonoCJKjp-Regular.otf"

txt = "	".join(words)

stop_words = ['そう', 'ない', 'いる', 'する', 'まま', 'よう', 'てる', 'なる', 'こと', 'もう', 'いい', 'ある', 'ゆく', 'れる', 'ん', 'の']

wordcloud = WordCloud(background_color="black", font_path=font_path, stopwords=set(stop_words),
	width=800, height=600).generate(txt)

wordcloud.to_file('./wordcloud.png')

なるほど、一応できたか
キーワードを自然言語処理ではなく、「自民 -RT」でstop_wordsに自民を指定してやってみる

政党名が多くて、ちょっと期待してたのと違うな。。。

[python3] BeautifulSoupでプロキシ経由でスクレイピングする

beautifulsoupでUserAgentとProxyを設定する

# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
from pprint import pprint

URL = 'https://news.yahoo.co.jp/'
USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"

proxies = {
	'http':'http://43.248.24.158:51166/',
	'https':'http://43.128.23.107:8080/'
}
headers = {"User-Agent": USER_AGENT}

resp = requests.get(URL, proxies=proxies, headers=headers, timeout=10)
resp.encoding = 'utf8' 
soup = BeautifulSoup(resp.text, "html.parser")

titles = soup.select('.sc-esjQYD a')
titles = [t.contents[0] for t in titles]

pprint(titles)

$ python3 app.py
[‘春の嵐 太平洋側激しい雷雨も’,
‘コロナワクチン種類選択OKに’,
‘北ミサイル 安保理緊急協議へ’,
‘生息域40年で倍 減らぬシカ’,
‘中国の謎の文明 黄金仮面発見’,
‘羽生まるで別人 専門家の目’,
‘みちょぱ 結婚の話している’,
‘水卜アナ 局アナ続ける理由’]

なるほど、BSでも行けますね、OK

[Python3] RSSフィードの取得

CentOS8で作業します。

$ python3 –version
Python 3.6.8
$ pip3 –version
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

### feedparserをインストール
$ sudo pip3 install feedparser
// 動作テスト
$ python3
Python 3.6.8 (default, Aug 24 2020, 17:57:11)
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import feedparser
>>> d = feedparser.parse(‘https://news.yahoo.co.jp/pickup/rss.xml’)
>>> d[‘feed’][‘title’]
‘Yahoo!ニュース・トピックス – 主要’

# -*- coding: utf-8 -*-
import feedparser

URL = 'https://news.yahoo.co.jp/rss/topics/top-picks.xml'

d = feedparser.parse(URL)
for entry in d.entries:
	print(entry.title, entry.link)

$ python3 app.py
山形県 独自の緊急宣言を拡大 https://news.yahoo.co.jp/pickup/6388973?source=rss
殺せない 逃げたミャンマー兵 https://news.yahoo.co.jp/pickup/6388959?source=rss
18歳刺殺 車に注意し口論か https://news.yahoo.co.jp/pickup/6388974?source=rss
兄の無実60年間信じ 妹の訴え https://news.yahoo.co.jp/pickup/6388951?source=rss
辛ラーメン開発 辛春浩氏死去 https://news.yahoo.co.jp/pickup/6388971?source=rss
楽天則本が離婚「僕に原因」 https://news.yahoo.co.jp/pickup/6388968?source=rss
元力士の漫画家 琴剣さん死去 https://news.yahoo.co.jp/pickup/6388962?source=rss
谷原章介 帯の司会頭にあった https://news.yahoo.co.jp/pickup/6388972?source=rss

RSSにMeCabを使う

# -*- coding: utf-8 -*-
import feedparser
import MeCab

wakati=MeCab.Tagger("-Owakati")
URL = 'https://news.yahoo.co.jp/rss/topics/top-picks.xml'
sentence_wakati = []

d = feedparser.parse(URL)
for entry in d.entries:
	# print(entry.title, entry.link)
	sentence = wakati.parse(entry.title).split()
	sentence_wakati.append(sentence)

print(sentence_wakati)

$ python3 app.py
[[‘ミャンマー’, ‘クーデター’, ‘正当’, ‘化’], [‘山形’, ‘県’, ‘独自’, ‘の’, ‘緊急’, ‘宣言’, ‘を’, ‘拡大’], [’18’, ‘歳’, ‘刺殺’, ‘車’, ‘に’, ‘注意’, ‘し’, ‘口論’, ‘か’], [‘聖火’, ‘リレー’, ‘初’, ‘の’, ‘週末’, ‘密集’, ‘警戒’], [‘辛’, ‘ラーメン’, ‘開発’, ‘辛’, ‘春’, ‘浩’, ‘氏’, ‘死去’], [‘楽天’, ‘則’, ‘本’, ‘が’, ‘離婚’, ‘「’, ‘僕’, ‘に’, ‘原因’, ‘」’], [‘東海’, ‘大’, ‘菅生’, ‘が’, ‘サヨナラ’, ‘初’, ‘8’, ‘強’], [‘谷原’, ‘章介’, ‘帯’, ‘の’, ‘司会’, ‘頭’, ‘に’, ‘あっ’, ‘た’]]

RSSテキストのカテゴリ分けをやりたい。

ubuntu bionicでpython3のcronを設定する

まず実行したいpyファイルを用意します。
/home/vagrant/local/cron/app.py

$ crontab -l
no crontab for vagrant
$ crontab -e

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 2

$ which python
/usr/bin/python

$ crontab -e

*/5 * * * * /usr/bin/python /home/vagrant/local/cron/app.py

// cronリスタート
$ /etc/init.d/cron restart
// 実行ログの確認
$ sudo journalctl -f -u cron

実行テストは、5分おき(*/5 * * * *)ではなく、1分おきの方が良いですね。
続いて、raspberry pi4でcronを設定します。

[Python 3.8.0] S3に画像をアップロード・ダウンロードする

### S3へupload
1.まず送信したい画像を用意します。SVMで使用したgeorgia.jpgを使いましょう。

2.AWSコンソールにログインし、S3からbucketを作成します。
取り敢えず、hanbai-raspi としておきます。

画像の送受信のみに利用する為、Block all public accessにします。

3.AmazonS3FullAccessのユーザを作成

4.ubuntu側でAWS SDKのboto3をインストール
$ pip -V
pip 20.1.1 from /home/vagrant/.local/lib/python3.8/site-packages/pip (python 3.8)
$ pip install boto3

import boto3

accesskey = "****"
secretkey = "****"
region = "ap-northeast-1"

s3 = boto3.client('s3', aws_access_key_id=accesskey, aws_secret_access_key= secretkey, region_name=region)

filename = "georgia.jpg"
bucket_name = "hanbai-raspi"

s3.upload_file(filename,bucket_name,filename)
print("upload {0}".format(filename))

$ python app.py
upload georgia.jpg

はい、アップロードできてます。

続いて、downloadもやっちゃいましょう。

### download

import boto3

accesskey = "****"
secretkey = "****"
region = "ap-northeast-1"

s3 = boto3.resource('s3', aws_access_key_id=accesskey, aws_secret_access_key= secretkey, region_name=region)

bucket = s3.Bucket("hanbai-raspi")
filename="georgia.jpg"
path="img/georgia.jpg"


bucket.download_file(filename,path)
print("upload {0}".format(filename))

アップロードとダウンロードだと書き方が若干異なるので注意が必要ですね。
さあ、続いて、ubuntuとラズパイ4でcronの実行の仕方。