iT邦幫忙

0

用 Python 暢玩 Line bot - 10:File message, Location message

File message 應用

如果想寫一個分析使用者傳送的文章或是文件檔的 Line bot,可以考慮使用 jieba 來幫助斷詞。
jieba 是一個可以幫助中文斷詞的套件,它有三種斷詞模式:精確模式,嘗試將句子最精確地切開,適合文本分析;全模式,把句子中所有可以成詞的詞語都切割出來,但可能會有歧義;搜尋引擎模式,在精確模式的基礎上,對長詞再次切分,提高召回率,適合用於搜尋引擎分詞。
斷詞時候所使用的停用字字典可以自行建立,也可以去網路上找已有的 txt 檔來做使用或增減。

pip install

jieba
docx

範例程式碼

import jieba.analyse
import docx

@handler.add(MessageEvent)
def handle_message(event):
    if event.message.type == 'file'
        try:
            UserSendFile = line_bot_api.get_message_content(event.message.id)
            # 存檔案
            path='./Files/' + UserId + '.docx'

            with open(path, 'wb') as fd:
                for chunk in UserSendFile.iter_content():
                    fd.write(chunk)

            # 讀取文章
            doc = docx.Document(path)
            words = ""
            for para in doc.paragraphs:
                words += para.text + '\n'

            # 刪去停用字
            for ch in stops:
                words = words.replace(ch,"")

            # 抓取關鍵字
            texts = jieba.analyse.extract_tags(words, topK=10, withWeight=False, allowPOS=())

            # 如果有多則回覆,可使用 list 去存放
            reply = [
                TextSendMessage(text="meow 認為這篇文章的關鍵字如下:\n"),
                TextSendMessage(text="\n".join(texts))
            ]
        except:
            reply = TextSendMessage(text="您的檔案可能非文字檔。")

            line_bot_api.reply_message(event.reply_token, reply)

Location message 應用

LocationSendMessage 參數如下:
title 位置資訊的標題
address 該點的名稱
latitude 經度
longitude 緯度

範例程式碼

@handler.add(MessageEvent)
def handle_message(event):
	line_bot_api.reply_message(event.reply_token, LocationSendMessage(title='Location message test', address='輔仁大學', latitude = 25.03659458277292, longitude = 121.43222234092521))

而如果現在你的 Line bot 要做一個周遭美食地圖,或是實境解謎遊戲時,借助 Location message 的資訊會是一個不錯的主意,例如說,當使用者送出一個 Location message 後,可以計算出離該位置距離 1km 以內的美食資訊給使用者。或著當使用者送出一個 Location message 後,告知使用者離任務地點距離多遠以及所在方向。以上這些都會需要去計算到經緯度的距離,下面則是範例的程式碼。

import math

# 經緯度距離計算公式
def getDistance(latA, lonA, latB, lonB):  
	ra = 6378140  # radius of equator: meter  
	rb = 6356755  # radius of polar: meter  
	flatten = (ra - rb) / ra  # Partial rate of the earth  
	# change angle to radians  
	radLatA = math.radians(latA)  
	radLonA = math.radians(lonA)  
	radLatB = math.radians(latB)  
	radLonB = math.radians(lonB)  
  
	pA = math.atan(rb / ra * math.tan(radLatA))  
	pB = math.atan(rb / ra * math.tan(radLatB))  
	x = math.acos(math.sin(pA) * math.sin(pB) + math.cos(pA) * math.cos(pB) * math.cos(radLonA - radLonB))  
	c1 = (math.sin(x) - x) * (math.sin(pA) + math.sin(pB))**2 / math.cos(x / 2)**2  
	c2 = (math.sin(x) + x) * (math.sin(pA) - math.sin(pB))**2 / math.sin(x / 2)**2  
	dr = flatten / 8 * (c1 - c2)  
	distance = ra * (x + dr)  
	return math.round(distance,3)
    
@handler.add(MessageEvent)
def handle_message(event):
	print(event.message.latitude)
	distance = getDistance(event.message.latitude, event.message.longitude, 25.03659458277292, 121.43222234092521)
	line_bot_api.reply_message(event.reply_token, TextSendMessage(text="離輔大的距離為:" + str(distance) + "m"))


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言