Hướng dẫn how to make autocomplete in python - cách tạo tự động hoàn thành trong python

. Sử dụng mô -đun Readline.

Dưới đây là một ví dụ nhanh dựa trên bài viết Pymotw của Doug Hellmann trên Readline.

import readline

class MyCompleter(object):  # Custom completer

    def __init__(self, options):
        self.options = sorted(options)

    def complete(self, text, state):
        if state == 0:  # on first trigger, build possible matches
            if text:  # cache matches (entries that start with entered text)
                self.matches = [s for s in self.options 
                                    if s and s.startswith(text)]
            else:  # no text entered, all matches possible
                self.matches = self.options[:]

        # return match indexed by state
        try: 
            return self.matches[state]
        except IndexError:
            return None

completer = MyCompleter(["hello", "hi", "how are you", "goodbye", "great"])
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')

input = raw_input("Input: ")
print "You entered", input

Điều này dẫn đến hành vi sau ( đại diện cho khóa tab đang được nhấn):

Input: 
goodbye      great        hello        hi           how are you

Input: h
hello        hi           how are you

Input: hoow are you

Trong dòng cuối cùng (Hotab đã nhập), chỉ có một trận đấu có thể và toàn bộ câu "Bạn thế nào" được tự động hoàn thành.

Kiểm tra các bài viết được liên kết để biết thêm thông tin về readline.


"Và tốt hơn là sẽ là nếu nó hoàn thành các từ không chỉ ngay từ đầu ... hoàn thành từ phần tùy ý của chuỗi."

Điều này có thể đạt được bằng cách đơn giản là sửa đổi các tiêu chí phù hợp trong chức năng tuân thủ, tức là. từ:

self.matches = [s for s in self.options 
                   if s and s.startswith(text)]

một cái gì đó như:

self.matches = [s for s in self.options 
                   if text in s]

Điều này sẽ cung cấp cho bạn hành vi sau:

Input: 
goodbye      great        hello        hi           how are you

Input: o
goodbye      hello        how are you

Cập nhật: Sử dụng bộ đệm lịch sử (như đã đề cập trong bình luận)

Một cách đơn giản để tạo một menu giả để cuộn/tìm kiếm là tải các từ khóa vào bộ đệm lịch sử. Sau đó, bạn sẽ có thể cuộn qua các mục nhập bằng các phím mũi tên lên/xuống cũng như sử dụng CTRL+R để thực hiện tìm kiếm ngược.

Để thử điều này, hãy thực hiện các thay đổi sau:

keywords = ["hello", "hi", "how are you", "goodbye", "great"]
completer = MyCompleter(keywords)
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')
for kw in keywords:
    readline.add_history(kw)

input = raw_input("Input: ")
print "You entered", input

Khi bạn chạy tập lệnh, hãy thử gõ ctrl+r theo sau là a. Điều đó sẽ trả về trận đấu đầu tiên có chứa "A". Nhập Ctrl+R một lần nữa cho trận đấu tiếp theo. Để chọn một mục, nhấn Enter.

Ngoài ra, hãy thử sử dụng các phím lên/xuống để cuộn qua các từ khóa.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Trong bài viết này, chúng tôi sẽ tìm hiểu cách cung cấp các tính năng như tự động hoàn thành dữ liệu được truyền từ bình. Về cơ bản, AutoComplete có nghĩa là dự đoán phần còn lại của từ khi người dùng gõ một cái gì đó. Bằng cách này, sự tương tác của con người cũng tăng lên với mỗi dự đoán chính xác. Hãy cùng xem cách làm điều tương tự.

    Chúng tôi sẽ sử dụng jQuery để tự động hoàn thành.

    pip install flask

    Cài đặt :app.py.

    app.py

    Python3

    Để cài đặt bình gõ lệnh bên dưới trong thiết bị đầu cuối.

    Đầu tiên, tạo một thư mục mới cho dự án. Bên trong đó tạo một tệp mới và đặt tên cho nó app.py.

    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    7
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    8
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    9
    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    
    0
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    5
    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    
    2
    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    
    3
    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    
    4
    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    
    5
    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    
    6

    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    0
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    1
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    2
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    3

    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    4
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    5
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    6

    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    
    7
    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    
    8

    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    
    9
    self.matches = [s for s in self.options 
                       if text in s]
    
    0
    self.matches = [s for s in self.options 
                       if text in s]
    
    1
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    5
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    5
    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    
    5
    self.matches = [s for s in self.options 
                       if text in s]
    
    5

    Các

    Các

    self.matches = [s for s in self.options 
                       if s and s.startswith(text)]
    
    9
    python app.py
    9
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    512

    self.matches = [s for s in self.options 
                       if text in s]
    
    6
    pip install flask
    6
    pip install flask
    7
    pip install flask
    8
    pip install flask
    9
    Input: 
    goodbye      great        hello        hi           how are you
    
    Input: h
    hello        hi           how are you
    
    Input: hoow are you
    
    5
    python app.py
    1templates. In this file, we have an input field where the user will type a string and the jquery function will provide the suggestions.

    index.html

    self.matches = [s for s in self.options if text in s] 0 python app.py3Input: goodbye great hello hi how are you Input: h hello hi how are you Input: hoow are you 5Input: goodbye great hello hi how are you Input: h hello hi how are you Input: hoow are you 5 python app.py6self.matches = [s for s in self.options if text in s] 5

    Sau đó, tạo một thư mục mới bên trong dự án để chứa tất cả các tệp HTML và đặt tên cho chúng các mẫu. Trong tệp này, chúng tôi có một trường đầu vào trong đó người dùng sẽ nhập chuỗi và chức năng jQuery sẽ cung cấp các đề xuất.

    python app.py

    Output:

    Hướng dẫn how to make autocomplete in python - cách tạo tự động hoàn thành trong python


    Làm thế nào để bạn tự động hoàn thành văn bản trong Python?

    QlineEdit Auto hoàn thành Ví dụ Bắt đầu bằng cách tạo danh sách các tùy chọn (tên) / đề xuất.Thencreate một QCompleter, một người tuân thủ = QCompleter (tên).Tiện ích QlineEdit là một hộp văn bản SIMPE có thể được thêm vào cửa sổ của bạn.Bạn có thể tạo một tiện ích chỉnh sửa dòng với bản thân dòng.Start by creating a list of options (names) / suggestions. Thencreate a QCompleter, a completer = QCompleter(names) . The QLineEdit widget is a simpe text box that can be added to your window. You can create a line edit widget with the line self.

    Làm cách nào để kích hoạt tự động hoàn thành trong python idle?

    Hoàn thành mã và các mẹo gọi Python Idle có chức năng hoàn thành mã cơ bản.Nó chỉ có thể tự động hoàn thành tên của các chức năng và các lớp.Để sử dụng tự động hoàn thành trong trình chỉnh sửa, chỉ cần nhấn phím tab sau một chuỗi văn bản.press the tab key after a sequence of text.

    Làm cách nào để kích hoạt các đề xuất trong Python?

    Chỉ cần nhấn phím Tab Tab trong khi viết mã.Điều này sẽ mở một menu với các đề xuất.Nhấn vào Enter Enter để chọn gợi ý.hit the “Tab” key while writing code. This will open a menu with suggestions. Hit “Enter” to choose the suggestion.

    Làm cách nào để kích hoạt tự động hoàn thành trong pycharm?

    Nhấn Ctrl+Space hoặc chọn mã |Hoàn thành mã |Cơ bản từ menu chính.Nếu cần, nhấn không gian Ctrl+lần thứ hai (hoặc nhấn Ctrl+Alt+Space).. If necessary, press Ctrl+Space for the second time (or press Ctrl+Alt+Space ).