一、QPython安裝配置
1.1. QPython介紹
QPython是一個可以在安卓設備運行python的腳本引擎,版本有QPython 3L和QPython 3C,3L為官方版本,可以在應用市場搜索下載,3C版本為"乘著船"大佬的修改版本,由于3L版本有許多權限限制及很多包不能安裝,文章中使用3C版本完成,
1.2. 下載地址
百度搜索"QPython 3C開源版",進入gitee,找到鏈接即可下載,如下圖:

百度網盤下載:https://pan.baidu.com/s/1zT1NGtYTe55m6bSRWlePRg
提取碼:zxcv
幫助檔案:https://www.bilibili.com/read/cv13322251
二、獲取短信內容并生成詞云
-
獲取短信內容
獲取短信將使用SL4A的api,關于SL4A的介紹及檔案,打開第一點gitee中相關鏈接,如下圖:

話不多說,直接上代碼: -
獲取所有短信并存入csv
import androidhelper
import csv
droid=androidhelper.Android()
# 獲取短信具體內容并存入csv
def saveSMSToFile(save_path):
# 獲取所有收取的短信,False為獲取所有短信,True為獲取未讀短信;inbox為收件箱,outbox為發件箱
sms_data=https://www.cnblogs.com/bushrose/archive/2023/01/29/droid.smsGetMessages(False,'inbox').result
'''
id:每條短信的原始id
address:對方手機號
date:短資訊的時間戳
body:短信具體內容
read:已讀未讀,1為已讀,0是未讀,
status不知道是啥
type,發資訊還是收資訊,1為收,2為發
'''
headers=['_id', 'address', 'date', 'body', 'read', 'status', 'type']
with open(save_path,'w') as f:
f_scv = csv.DictWriter(f, headers)
f_scv.writeheader()
f_scv.writerows(sms_data)
return save_path
- 利用
jieba分詞及pyecharts生成詞云
# 停用詞,生成詞云會過濾,根據實際情況修改
FILTER_WORDS = ['你', '我','他','我們', '他們', ',','驗證碼',':', '的','賬號', 'cn', 'https', '0.00', '點擊', '退訂', '尊敬','客戶', 'TD', '登錄','http', '12582', '61.56', '0.42','u.10010', 'http', 'com']
#獲取關鍵詞數量,用于詞云展示時的數量,num可以修改,詞云展示生成時的數量
def getKeyWordsCounts(filepath, num=30):
with open(filepath,'r') as f:
f_csv=csv.reader(f)
headers=next(f_csv)
content=",".join([row[3] for row in f_csv])
#print(content)
seg_list=list(jieba.cut(content))
# print(seg_list)
keywords_counts = pd.Series(seg_list)
keywords_counts = keywords_counts[keywords_counts.str.len()>1]
keywords_counts = keywords_counts[~keywords_counts.str.contains('|'.join(FILTER_WORDS))]
keywords_counts = keywords_counts.value_counts()[:num]
return keywords_counts
# 構建生成詞云的元組
def getWords(keywords_counts):
words=[]
for i, v in keywords_counts.items():
words.append((i,v))
return words
# 渲染html
def render_html(html_filepath,words):
c = (
WordCloud()
.add(
"",
words,
word_size_range=[20, 100],
textstyle_opts=opts.TextStyleOpts(font_family="cursive"),
)
.set_global_opts(title_opts=opts.TitleOpts(title=os.path.splitext(html_filepath)[0]))
.render(html_filepath)
)
# 生成詞云
def genWordCloud(csv_filepath,html_filepath, num=30): csv_filepath=saveSMSToFile(csv_filepath)
keywords_counts=getKeyWordsCounts(csv_filepath, num=30)
words=getWords(keywords_counts)
render_html(html_filepath, words)
- 傳入檔案地址及html地址
csv_filepath='/storage/emulated/0/0/sms.csv'
html_filepath='/storage/emulated/0/0/短信詞云分析.html'
genWordCloud(csv_filepath, html_filepath)
# 使用qpython自帶的瀏覽器訪問
jsla('viewHtml', html_filepath)
最終實作效果如下圖:

完整版代碼,點擊底部閱讀原文,回復【Qpython詞云】
三、總結
文章采用Sl4A和QPython完成了短信詞云生成,SL4A提供了豐富的api,如通過QPython結合scikit-learn監測并過濾洗掉垃圾短信等,這些都由大家自己去探索,
閱讀原文
本文由公眾號【產品經理不是經理】同步發布,歡迎關注
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/542522.html
標籤:其他
