Pyphonetics là một thư viện Python 3 cho các thuật toán ngữ âm. Hiện tại, các thuật toán sau được triển khai và hỗ trợ Show
Nhiều hơn sẽ được bổ sung trong tương lai Cài đặtMô-đun này có sẵn trong PyPI, chỉ cần sử dụng pip install pyphonetics Cách sử dụng>>> from pyphonetics import Soundex >>> soundex = Soundex() >>> soundex.phonetics('Rupert') 'R163' >>> soundex.phonetics('Robert') 'R163' >>> soundex.sounds_like('Robert', 'Rupert') True API tương tự áp dụng cho mọi thuật toán, e. g >>> from pyphonetics import Metaphone >>> metaphone = Metaphone() >>> metaphone.phonetics('discrimination') 'TSKRMNXN' Bạn cũng có thể sử dụng phương pháp distance(word1, word2, metric=’levenshtein’) để tìm khoảng cách giữa 2 cách thể hiện ngữ âm >>> from pyphonetics import RefinedSoundex >>> rs = RefinedSoundex() >>> rs.distance('Rupert', 'Robert') 0 >>> rs.distance('assign', 'assist', metric='hamming') 2 Tín dụngMô-đun này chủ yếu dựa trên việc triển khai các thuật toán ngữ âm được tìm thấy trong Talisman. js (https. //github. com/Yomguithereal/talisman) Thư viện Node NLP Tất cả các thuật toán trên đều sử dụng từ điển dựa trên Trie cơ bản để lưu trữ hiệu quả và tính toán nhanh. Việc triển khai tất cả các thuật toán được lấy cảm hứng từ bài viết tuyệt vời Khoảng cách Levenshtein nhanh và dễ sử dụng Trie, của Steve Hanov 📦 Cài đặtCách dễ nhất để cài đặt from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]7 là thông qua from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]8 pip install spellwise 🧑💻 Công dụngHiện tại, có năm thuật toán có sẵn để sử dụng với các tên lớp sau,
Vui lòng kiểm tra thư mục # Fetch suggestions with maximum distance 4 suggestions = algorithm.get_suggestions("spellin", max_distance=4) # Print the suggestions print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}, {'word': 'spell', 'distance': 4}]5 để biết cách sử dụng cụ thể của từng thuật toán. Nhưng theo nghĩa chung, mỗi thuật toán có ba phần,
from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}] Khoảng cách tối đa mặc định được coi là khác nhau đối với các thuật toán khác nhau. Nó có thể được thay đổi trong khi tìm nạp các đề xuất, # Fetch suggestions with maximum distance 4 suggestions = algorithm.get_suggestions("spellin", max_distance=4) # Print the suggestions print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}, {'word': 'spell', 'distance': 4}] 💡 Phân tích từng thuật toánCó nhiều thuật toán hiện có sẵn trong gói, mỗi thuật toán phù hợp với các mục đích khác nhau. Chúng ta sẽ thảo luận cụ thể từng thuật toán trong các phần sau (1) LevenshteinThuật toán from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]9 là phương pháp cơ bản và phổ biến nhất để xác định các từ đúng gần nhất với từ sai chính tả, dựa trên khoảng cách chỉnh sửa (số lần chèn, xóa và thay thế) giữa từ đã cho và từ được viết đúng chính tả from spellwise import Levenshtein # Initialise the algorithm levenshtein = Levenshtein() # Index the words from a dictionary levenshtein.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = levenshtein.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance"))) Levenshtein cung cấp những điều sau đây, Word Distance ================= run 0 bun 1 dun 1 fun 1 gun 1 hun 1 jun 1 jun 1 mun 1 nun 1 (2) Chỉnh sửaThuật toán # Fetch suggestions with maximum distance 4 suggestions = algorithm.get_suggestions("spellin", max_distance=4) # Print the suggestions print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}, {'word': 'spell', 'distance': 4}]0 cung cấp các gợi ý về từ gần với từ đã cho về mặt ngữ âm. Nó cũng sử dụng khoảng cách chỉnh sửa nhưng có chi phí thay thế hoặc xóa khác nhau tùy thuộc vào việc hai chữ cái có thuộc cùng một nhóm ngữ âm hay không from spellwise import Editex # Initialise the algorithm editex = Editex() # Index the words from a dictionary editex.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = editex.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance"))) Editex gợi ý như sau, Word Distance ================= run 0 ran 1 ron 1 ruin 1 rum 1 bun 2 dun 2 dunn 2 fun 2 gun 2 Lưu ý rằng thuật toán from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]9 tính toán khoảng cách giữa # Fetch suggestions with maximum distance 4 suggestions = algorithm.get_suggestions("spellin", max_distance=4) # Print the suggestions print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}, {'word': 'spell', 'distance': 4}]9 và from spellwise import Levenshtein # Initialise the algorithm levenshtein = Levenshtein() # Index the words from a dictionary levenshtein.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = levenshtein.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance")))0 là 1 (vì chỉ cần một phép thay thế). Mặt khác, thuật toán # Fetch suggestions with maximum distance 4 suggestions = algorithm.get_suggestions("spellin", max_distance=4) # Print the suggestions print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}, {'word': 'spell', 'distance': 4}]0 tính khoảng cách này là 2 vì về mặt ngữ âm, các từ cách xa nhau hơn Như đã đề cập ở trên, thuật toán Editex sử dụng các chi phí khác nhau để thay thế và xóa. Các giá trị này có thể được sửa đổi để tìm nạp các kết quả khác nhau from spellwise import Editex # Initialise the algorithm editex = Editex(group_cost=0.5, non_group_cost=3) # configure the group cost and non-group cost # Index the words from a dictionary editex.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = editex.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance"))) Định cấu hình from spellwise import Levenshtein # Initialise the algorithm levenshtein = Levenshtein() # Index the words from a dictionary levenshtein.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = levenshtein.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance")))2 và from spellwise import Levenshtein # Initialise the algorithm levenshtein = Levenshtein() # Index the words from a dictionary levenshtein.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = levenshtein.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance")))3 trong ví dụ trên dẫn đến các đề xuất sau, Word Distance ================= run 0 ran 0.5 ron 0.5 ruin 0.5 rum 0.5 lan 1.0 len 1.0 lin 1.0 lon 1.0 loon 1.0 (3) SoundexThuật toán Soundex, tương tự như Editex nhằm mục đích cung cấp các từ tương tự về mặt ngữ âm cho từ cho. Đây là một trong những thuật toán đối sánh ngữ âm ban đầu và tồn tại nhiều biến thể from spellwise import Soundex # Initialise the algorithm soundex = Soundex() # Index the words from a dictionary soundex.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = soundex.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance"))) Soundex gợi ý như sau, from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]0 (4) Caverphone 1. 0 và Caverphone 2. 0Thuật toán Caverphone được phát triển như một phần của dự án Caversham để xác định ngữ âm tên của các trường hợp khác nhau của cùng một người từ nhiều nguồn khác nhau. Nói cách khác, nó được sử dụng để xác định ngữ âm các mục trùng lặp của một thực thể hoặc một từ. Sự khác biệt giữa v1 và v2 của thuật toán là trong quá trình xử lý trước các từ trong quá trình lập chỉ mục from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]1 Caverphone v2 cung cấp các gợi ý sau, from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]2 (5) Bệnh đánh máy# Fetch suggestions with maximum distance 4 suggestions = algorithm.get_suggestions("spellin", max_distance=4) # Print the suggestions print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}, {'word': 'spell', 'distance': 4}]4 là thuật toán sửa lỗi dựa trên Typographic được tối ưu hóa để sửa lỗi chính tả trong bàn phím QWERTY. Điều này tương tự như thuật toán # Fetch suggestions with maximum distance 4 suggestions = algorithm.get_suggestions("spellin", max_distance=4) # Print the suggestions print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}, {'word': 'spell', 'distance': 4}]0, ngoại trừ việc các chữ cái được nhóm lại dựa trên vị trí của chúng trên bàn phím, thay vì nhóm chúng theo ngữ âm. Bài báo gốc không có sẵn để đọc miễn phí và do đó đây có thể không phải là cách triển khai chính xác của nó from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]3 Typox cung cấp các từ sau, from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]4 Lưu ý rằng # Fetch suggestions with maximum distance 4 suggestions = algorithm.get_suggestions("spellin", max_distance=4) # Print the suggestions print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}, {'word': 'spell', 'distance': 4}]4 không gợi ý những từ như from spellwise import Levenshtein # Initialise the algorithm levenshtein = Levenshtein() # Index the words from a dictionary levenshtein.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = levenshtein.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance")))7, from spellwise import Levenshtein # Initialise the algorithm levenshtein = Levenshtein() # Index the words from a dictionary levenshtein.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = levenshtein.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance")))8, from spellwise import Levenshtein # Initialise the algorithm levenshtein = Levenshtein() # Index the words from a dictionary levenshtein.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = levenshtein.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance")))9, Word Distance ================= run 0 bun 1 dun 1 fun 1 gun 1 hun 1 jun 1 jun 1 mun 1 nun 10, v.v. , (mà from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]9 sẽ đề xuất) mặc dù chúng có khoảng cách chỉnh sửa 2 với từ Word Distance ================= run 0 bun 1 dun 1 fun 1 gun 1 hun 1 jun 1 jun 1 mun 1 nun 12. Nhưng nó gợi ý những từ gần nhất dựa trên cách bố trí bàn phím QWERTY là Word Distance ================= run 0 bun 1 dun 1 fun 1 gun 1 hun 1 jun 1 jun 1 mun 1 nun 13 và Word Distance ================= run 0 bun 1 dun 1 fun 1 gun 1 hun 1 jun 1 jun 1 mun 1 nun 14 Như đã đề cập ở trên, thuật toán Typox tương tự như Editex và sử dụng các chi phí khác nhau để thay thế và xóa. Các giá trị này có thể được sửa đổi để tìm nạp các kết quả khác nhau from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]5 Typox cung cấp gợi ý sau cho từ Word Distance ================= run 0 bun 1 dun 1 fun 1 gun 1 hun 1 jun 1 jun 1 mun 1 nun 15 sau khi đặt from spellwise import Levenshtein # Initialise the algorithm levenshtein = Levenshtein() # Index the words from a dictionary levenshtein.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = levenshtein.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance")))2 và from spellwise import Levenshtein # Initialise the algorithm levenshtein = Levenshtein() # Index the words from a dictionary levenshtein.add_from_path("examples/data/american-english") # Fetch suggestions suggestions = levenshtein.get_suggestions("run") # Print the top 10 suggestions print("Word \t Distance") print("=================") for suggestion in suggestions[0:10]: print("{} \t {}".format(suggestion.get("word"), suggestion.get("distance")))3 from spellwise import (CaverphoneOne, CaverphoneTwo, Editex, Levenshtein, Soundex, Typox) # (1) Initialize the desired algorithm algorithm = Editex() # this can be CaverphoneOne, CaverphoneTwo, Levenshtein or Typox as well # (2) Index the words/names to the algorithm # Indexing can be done by adding words from a file algorithm.add_from_path("") # or by adding them manually algorithm.add_words(["spell", "spelling", "check"]) # (3) Fetch the suggestions for the word suggestions = algorithm.get_suggestions("spellin") # The `suggestions` is a list of dict with fields `word` and `distance` # [{"word": ..., "distance": ...}, ...] print(suggestions) # Output would be similar to the following, # [{'word': 'spelling', 'distance': 2}]6 ⚡️ Hồ sơ bộ nhớ và thời gianSau đây là thống kê sử dụng trên MacBook Pro, 2. Intel Core i5 lõi tứ 4 GHz với RAM 16 GB Thuật toánKhông. của từ Kích thước văn bản trên đĩa Bộ nhớ đã sử dụng Thời gian để lập chỉ mục Thời gian để suy luận Nhận xétLevenshtein119,0951. 1 MB~ 127 MB~ 1160 mili giây~ 36 mili giây
🙌 Đóng gópXin vui lòng tăng PR. 😃 Có rất nhiều thuật toán được thêm vào và các cải tiến được thực hiện cho gói này. Gói này vẫn còn trong phiên bản đầu và rất mong nhận được sự đóng góp của bạn |