需求背景
朋友的單位,有一個小型的圖書室,圖書室中擺放了很多的書,每本書都被編號放在對應的區域,為了讓大家更快,更容易找到這些書,他聯絡我,讓我幫他弄一個圖書查詢系統。按道理來說,這個應該是一個比較複雜的專案,但是考慮到了雲函式的存在,所以就打算把整個應用部署在Serverless架構上。
功能設計讓朋友把書籍整理資訊,儲存到一個Excel表格中;將Excel表放到COS中,雲函式讀取這個表,並且解析;根據詞語的相似尋找相似的圖書;前端頁面通過MUI製作,放在cos中;整體實現Excel樣式:
主要包括書名和編號,同時下面包括分類的tab:
核心程式碼實現:
import jiebaimport openpyxlfrom gensim import corpora, models, similaritiesfrom collections import defaultdictimport urllib.requestwith open("/tmp/book.xlsx", "wb") as f: f.write( urllib.request.urlopen("********").read() )top_str = "abcdefghijklmn"book_dict = {}book_list = []wb = openpyxl.load_workbook('/tmp/book.xlsx')sheets = wb.sheetnamesfor eve_sheet in sheets: print(eve_sheet) sheet = wb.get_sheet_by_name(eve_sheet) this_book_name_index = None this_book_number_index = None for eve_header in top_str: if sheet[eve_header][0].value == "書名": this_book_name_index = eve_header if sheet[eve_header][0].value == "編號": this_book_number_index = eve_header print(this_book_name_index, this_book_number_index) if this_book_name_index and this_book_number_index: this_book_list_len = len(sheet[this_book_name_index]) for i in range(1, this_book_list_len): add_key = "%s_%s_%s" % ( sheet[this_book_name_index][i].value, eve_sheet, sheet[this_book_number_index][i].value) add_value = { "category": eve_sheet, "name": sheet[this_book_name_index][i].value, "number": sheet[this_book_number_index][i].value } book_dict[add_key] = add_value book_list.append(add_key)def getBookList(book, book_list): documents = [] for eve_sentence in book_list: tempData = " ".join(jieba.cut(eve_sentence)) documents.append(tempData) texts = [[word for word in document.split()] for document in documents] frequency = defaultdict(int) for text in texts: for word in text: frequency[word] += 1 dictionary = corpora.Dictionary(texts) new_xs = dictionary.doc2bow(jieba.cut(book)) corpus = [dictionary.doc2bow(text) for text in texts] tfidf = models.TfidfModel(corpus) featurenum = len(dictionary.token2id.keys()) sim = similarities.SparseMatrixSimilarity( tfidf[corpus], num_features=featurenum )[tfidf[new_xs]] book_result_list = [(sim[i], book_list[i]) for i in range(0, len(book_list))] book_result_list.sort(key=lambda x: x[0], reverse=True) result = [] for eve in book_result_list: if eve[0] >= 0.25: result.append(eve) return resultdef main_handler(event, context): try: print(event) name = event["body"] print(name) base_html = '''<div class='mui-card'><div class='mui-card-header'>{{book_name}}</div><div class='mui-card-content'><div class='mui-card-content-inner'>分類:{{book_category}}<br>編號:{{book_number}}</div></div></div>''' result_str = "" for eve_book in getBookList(name, book_list): book_infor = book_dict[eve_book[1]] result_str = result_str + base_html.replace("{{book_name}}", book_infor['name']) \\ .replace("{{book_category}}", book_infor['category']) \\ .replace("{{book_number}}", book_infor['number'] if book_infor['number'] else "") if result_str: return result_str except Exception as e: print(e) return '''<div class='mui-card' style='margin-top: 25px'><div class='mui-card-content'><div class='mui-card-content-inner'>未找到圖書資訊,請您重新搜尋。</div></div></div>'''同時配置APIGW:
首頁:
搜尋結果:
(我是通過Webview封裝成一個APP)
總結這個APP是一個低頻使用APP,可以這樣認為,如果做在一個傳統伺服器上,這應該不是一個明智的選擇,雲函式的按量付費,cos與APIGW的融合,完美解決了資源浪費的問題,同時借用雲函式的APIGW觸發器,很簡單輕鬆的替代傳統的Web框架和部分伺服器軟體的安裝和使用、維護等。這個例子非常小,但是確是一個有趣的小工具,除了圖書查詢之外,還可以考慮做成成績查詢等。
最新評論