Preface:

There are media reports that Beijing Lu Xun Museum official website information query online retrieval system can achieve “Lu Xun said, you can query” function. The day after the report came out, the system crashed. I was wondering if I could also do a simple query system to play. Let’s get started happily

The development tools

Python version: 3.6.4

Related modules:

PyQt5 module;

Python – Levenshtein module;

Fuzzywuzzy module;

And some of the modules that come with Python.

Environment set up

Install Python and add it to the environment variable, and the PIP will install the appropriate module.

Introduction of the principle

First, download a copy of the complete works of Lu Xun.txt file:

Then use PyQt layout to implement a simple GUI:

Class GUI(QWidget): def __init__(self, parent=None): __init__() self.setWindowTitle(' Qicon ('data/icon.jpg')) self.setWindowTitle(' Qicon ('data/icon.jpg')) Self. Label1 = QLabel (' sentence: ') self. Line_edit = QLineEdit () the self. Label2 = QLabel (' query result: ') the self. The text = QTextEdit () Self.button = QPushButton() self.button.setText(' query ') self.cmb = QComboBox() self.cmb = QPushButton() self.button. SetText (' query ') self.cmb = QComboBox() Self. CMB.setStyle (QstyleFactory. Create ('Fusion')) self. CMB.addItem (' Match: 100%') self. CMB.addItem (' Match: 100%') self. ') self.cmb.addItem(' Match: 80%') self.cmb.addItem(' Match: 80%') 70%') self.grid = QGridLayout() self.grid.setSpacing(12) self.grid.addWidget(self.label1, 1, 0) self.grid.addWidget(self.line_edit, 1, 1, 1, 38) self.grid.addWidget(self.button, 1, 39) self.grid.addWidget(self.label2, 2, 0) self.grid.addWidget(self.text, 2, 1, 1, 40) self.grid.addWidget(self.cmb, 1, 40) self.setLayout(self.grid) self.resize(200, 400) self.button.clicked.connect(self.inquiry) self.paragraphs = self.loadData('data/book.txt')

Something like this:

Then, when the user clicks the query button, according to the lower limit of matching degree selected by the user, find all the paragraphs in the book.txt file whose matching degree is greater than the lower limit of matching degree of the sentence to be searched and display them in the query result box.

Fuzzywuzzy is a Python package that uses Python fuzzywuzzy.

FuzzyWuzzy is a small tool for fuzzy matching of strings and is very simple to use.

Here we call its partial_ratio method directly to match the similarity of two paragraphs and display all paragraphs whose similarity is greater than the given threshold in the result box. Specifically, the code implements the following:

Def inquiry(self): sentence = self.line_edit.text() matched = [] score_thresh = self.getScoreThresh() if not sentence: QMessageBox. Warning (self, "warning", 'please input need query lu xun words') else: for p in the self. Paragraphs: score = fuzz.partial_ratio(p, sentence) if score >= score_thresh and len(sentence) <= len(p): Append ([score, p]) infos = [] for match in matched: infos. Append (' %d\n ': %d\n]: %s\n' % (match[0], match[1])) if not infos: Infos.append (' Did not match to any sentence with similarity greater than %d.\n' % score_thresh) self.text.setText('\n\n\n'.join(infos)[:-1])

That’s the end of this article. Thank you for watching. Follow my daily series of Python gadgets

To thank you readers, I’d like to share with you some of my recent collection of dry programming goodies and give something back to every reader in the hope of helping you out.

Dry goods mainly include:

① More than 2000 Python e-books (mainstream and classic books are available)

(2) The Python Standard Library (Chinese version)

③ project source code (40 or 50 interesting and classic hands-on project and source code)

④Python basic introduction, crawler, web development, big data analysis video (suitable for small white learning)

⑤Python Learning Roadmap (Goodbye to Slow Learning)

⑥ Two days of Python crawler training camp live access

All done~ complete source code + dry goods see personal profile or private message to obtain the relevant files.