It is nothing related with AR and VR. But in the era of AI, who wouldn’t want to develop a cute chatbot?
Feel free to scan the QR code below to follow our cute pet 🙂
Most of the code is based upon Python. I have developed the following functionalities.
- AIML matching
- Dictionary
- Idiom
- Wikipedia
- Calculator
- Jokes and twisters
- Face recognition
- Weather
The greatest part about python is the coding speed – I don’t have to worry about much details when designing complex data structures.
Here are some of the Python code to scrape the weather information from weather.com.cn:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# -*- coding: utf-8 -*- import paras import requests from urllib2 import urlopen from bs4 import BeautifulSoup from datatypes.message import Message from datatypes.response import Response from utils import utils_str, utils_io, utils_debug module_name = u"天气" module_name_en = "weather" try: keywords = utils_io.read_keywords_list(module_name_en) lines = utils_io.read_stripped_lines_of_knowledge_base("weather_city", module_name_en) city_map = {} code_map = {} for line in lines: p = line.find("=") if p < 0: continue city_map[line[p+1:].strip()] = line[:p].strip() code_map[line[:p].strip()] = line[p+1:].strip() utils_debug.success_load(module_name, len(keywords), len(city_map)) except Exception as e: utils_debug.error_load(module_name, e) def get_city_weather(city_id): url = "http://www.weather.com.cn/weather/%s.shtml" % city_id weekly_weather = urlopen(url).read().decode('utf-8') soup = BeautifulSoup(weekly_weather, 'html.parser') seven_days = soup.find('ul', class_='t clearfix').get_text() seven_days = seven_days.split('\n') days = [] for day in seven_days: if day != '': days.append(day) else: pass ans = u"玲酱现在为您播报" + code_map[city_id] + u"天气~\n" for i in range(3): ans += days[i * 4] + u":\n\t" ans += days[i * 4 + 1] ans += days[i * 4 + 2] wind = days[i * 4 + 3] if wind[0] == u"<": wind = u"" #wind = u"风力小于" + wind[1:] elif wind[0] == u">": wind = u"" #wind = u"风力大于" + wind[1:] ans += wind + u"\n" return ans def match(s): # type: (s) -> bool return utils_str.match(s, keywords) def get_response(res, msg): # type: (Response, Message, int) -> bool try: if not match(msg.content): return False flag = False for key in keywords: if msg.content == key: city_name = paras.users.loc[msg.uid] flag = True break if not flag: (city_name, key) = utils_str.extract_str_in_message_before_keywords(msg.content, keywords) #print city_name #if city_map.get(city_name) is None: # city_name = paras.users.loc[msg.uid] if city_map.get(city_name) is not None: city_id = city_map[city_name] res.data = get_city_weather(city_id) res.set_lib(paras.libs.weather) return True else: return False except Exception as e: utils_debug.error_load(module_name, e) return False res.data = u"没有查询到" return True |
test