def stop_day(date: str, time: str = None): """ ปิดการแจ้งเตือนเฉพาะวัน """ global db, timezone d = timezone.localize(thai_day2datetime(date)).replace(hour=hour, minute=minute, second=0, microsecond=0) db.update({'alert': False}, N.date == d) return "ปิดการแจ้งเตือนเวลา " + time + " นาฬิกาของ" + date + " แล้วค่ะ"
def add(date: str, time: str, text: str) -> None: global db, timezone t = time.split(":") hour = int(t[0]) minute = int(t[1]) d = timezone.localize(thai_day2datetime(date)).replace(hour=hour, minute=minute, second=0, microsecond=0) if d == None: pass db.insert({'date': str(d), 'text': text, 'alert': True}) print(d)
def look(day: str = "วันนี้"): global db, N, timezone d = str(timezone.localize(thai_day2datetime(day))).split()[0] text = "" s = db.search(N.date.search(d)) #((N.date.search(d)) & (N.alert == True)) #print(s) if len(s) == 0: return "ไม่มีการบันทึก" + day + "ค่ะ" text += "รายการบันทึกความจำ" + day + "มีดังนี้" + "\n" j = 1 for i in s: t = i["date"].split()[1].replace("+07:00", "").split('.')[0] text += "รายการที่ " + str(j) + " เวลา " + t + " หัวข้อ" + i[ "title"] + " มีการบันทึกว่า " + i["note"] + " ค่ะ\n" j += 1 return text
def test_thai_day2datetime(self): now = datetime.datetime.now() self.assertEqual(now + datetime.timedelta(days=0), thai_day2datetime("วันนี้", now)) self.assertEqual(now + datetime.timedelta(days=1), thai_day2datetime("พรุ่งนี้", now)) self.assertEqual(now + datetime.timedelta(days=2), thai_day2datetime("มะรืนนี้", now)) self.assertEqual(now + datetime.timedelta(days=-1), thai_day2datetime("เมื่อวาน", now)) self.assertEqual(now + datetime.timedelta(days=-2), thai_day2datetime("วานซืน", now)) with self.assertRaises(NotImplementedError): thai_day2datetime("ศุกร์ที่แล้ว")
def text2com(text): global ner, tokenizer, thai_day2datetime, thai_time2time, add tag_ner = ner.get_ner(text, pos=False, tag=True) tt = tokenizer.tokenize(tag_ner) print(tt) d = None t = None for i in tt: temp_tag = i[0] temp_txt = i[1] if temp_tag == 'DATE': try: thai_day2datetime(temp_txt) d = temp_txt except: pass elif temp_tag == 'TIME': try: temp_txt = temp_txt.replace(" น.", "").replace("น.", "").replace(" น", "") if ':' in temp_txt: t = temp_txt else: t = thai_time2time(temp_txt) except: pass alert = None for i in [ "แจ้งเตือนว่า", "เตือนว่า", "แจ้งว่า", "แจ้งเตือน", "เตือน", "แจ้ง" ]: if i in text: #print(text.split(i)[1]) alert = text.split(i)[1] #print(alert) #break print(alert) if d == None and (alert != None and alert != '') and (t != None and t != ''): d = "วันนี้" if (alert == None or alert == '') and (d != None and d != '') and ( "ยกเลิก" in text or "ปิด" in text and "แจ้งเตือน" in text): if (t != None and t != ''): text = stop(d, t) # ปิดการแจ้งเตือนทั้งวัน elif ("ยกเลิก" in text or "ปิด" in text and "แจ้งเตือน" in text) and "ทั้งหมด" in text: text = stop_all() # ยกเลิกการแจ้งเตือน elif alert == None and alert == '': text = 'ระบบแจ้งเตือนไม่รองรับคำสั่งของคุณ\nกรุณาลองคำสั่งอื่นนะคะ' elif (d != None and d != '') and (t != None and t != ''): add(d, t, alert) text = "เพิ่มการแจ้งเตือน " + d + " เวลา " + t + " มีการแจ้งเตือนว่า" + alert + " เรียบร้อยแล้วค่ะ" elif (t != None and t != ''): add('วันนี้', t, alert) text = "เพิ่มการแจ้งเตือน " + d + " เวลา " + t + " มีการแจ้งเตือนว่า" + alert + " เรียบร้อยแล้วค่ะ" elif ("ยกเลิก" in text or "หยุด" in text) and ("แจ้งเตือน" in text or "เตือน" in text): if d != None and d != '' and (t != None and t != ''): text = stop(d, t) elif d != None and d != '': text = stop(d, None) elif t != None and t != '': text = stop("วันนี้", t) elif "ทั้งหมด" in text: text = stop_all() elif "แจ้งเตือน" in text or "เตือน" in text and (d != None and d != ''): try: text = look(d) except: text = "ระบบยังไม่รองรับคำสั่งนี้" else: text = 'ระบบแจ้งเตือนไม่รองรับคำสั่งของคุณ\nกรุณาลองคำสั่งอื่นนะคะ' return text