Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Quick implementation of simple version query tool based on Serverless

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you an example analysis of the quick implementation of a simple version of the query tool based on Serverless. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Demand background

My friend's unit, there is a small library, there are many books in the library, each book is numbered in the corresponding area, in order to make people faster and easier to find these books, he contacted me and asked me to help him set up a book query system. Through user input, fuzzy matching to the corresponding results, and provide the corresponding location of the book.

Function design

Ask a friend to sort out the books and store them in an Excel table

Put the Excel table in the object store, and the cloud function reads the file and parses the

Look for similar books according to the similarity of words

The front-end page is made by MUI, placed in object storage, and uses the Website function of object storage.

Realize the data form as a whole

The Excel style mainly includes the book title and number, and the following includes the classified tab:

Function-based search function

Core code implementation:

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 ("https://********").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 = "title": this_book_name_index = eve_header if sheet [eve _ header] [0] .value = "number": 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 [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_ key [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 =''{book_name}} Category: {{book_category}}

Serial number: {{book_number}}''result_str = "" for eve_book in getBookList (name, book_list): book_infor = book_ [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''No book information was found Please search again.''

Configure APIGW at the same time:

Function Page Book Retrieval system html, body {background-color: # efeff4 } function getResult () {var UTFTranslate = {Change: function (pValue) {return pValue.replace (/ [^\ u0000 -\ u00FF] / g, function ($0) {return escape ($0). Replace (/ (% u) (\ w {4}) / gi, & # xanth2 (")});}, ReChange: function (pValue) {return unescape (pValue.replace (/ & # x _ pValue.replace,'% u'). Replace (/\\ u _ replace (/ g,''));}}; var xmlhttp If (window.XMLHttpRequest) {/ / IE7+, Firefox, Chrome, Opera, Safari browser executes code xmlhttp = new XMLHttpRequest ();} else {/ / IE6, IE5 browser executes code xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP") } xmlhttp.onreadystatechange = function () {if (xmlhttp.readyState = = 4 & & xmlhttp.status = = 200 & & xmlhttp.responseText) {document.getElementById ("result") [xss_clean] = UTFTranslate.ReChange (xmlhttp.responseText) .slice (1,-1). Replace ("\",'') } xmlhttp.open ("POST", "https://********", true"); xmlhttp.setRequestHeader ("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send (document.getElementById ("book") .value);} Book retrieval system

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report