Python - 根据输入的英文字母排列出有意义的单词-参考笔记

Python - 根据输入的英文字母排列出有意义的单词-参考笔记

参考资料

说明&心得

如题,当初来会撰写这篇参考笔记,主要也是因为,之前很刚好的看到了这篇教学文章:python PyEnchant(拼写检查),所以就有想到,或许可以利用 PyEnchant 套件,来重新制作一个类似之前写过的 Scrabble Word Finder 专案的专案(好绕口w),简单来说就是要根据键盘输入所得到的英文字母来重新排列出一些可能有意义的单词,因此就可以利用 PyEnchant 套件来快速的查找出有意义的英文单词是哪些,然後因为这个程序码是在八个月前随意写的,所以可能没那麽好看,请见谅w。

然後程序码 1 跟 2 的差异,就是有没有使用到笛卡尔积 itertools.product 函式。

特此撰写本篇文章作为纪录文件,用以方便後续有需要的时候,可以快速的重复查阅,虽然後面比较没有什麽机会再用到,但也算是一个还不错的经验。

环境准备

安装 PyEnchant 套件

使用 pip3 套件直接安装即可,指令如下:

pip3 install pyenchant

程序码 version1

程序码 version1,如下所示:

import enchant
import time

d = enchant.Dict("en_US")

input_words = input()
tStart = time.time()#计时开始

thirdwords = set(permutations(input_words,3))
# print(len(thirdwords))
fouthwords = set(permutations(input_words,4))
# print(len(fouthwords))
fifthwords = set(permutations(input_words,5))
# print(len(fifthwords))
sixthwords = set(permutations(input_words,6))
# print(len(sixthwords))

thirdprocessedwords = []
fouthprocessedwords = []
fifthprocessedwords = []
sixthprocessedwords = []
for thirdword in thirdwords:
    tmpword = ''
    for w in thirdword:
        tmpword+=w
    thirdprocessedwords.append(tmpword)
for fouthword in fouthwords:
    tmpword = ''
    for w in fouthword:
        tmpword+=w
    fouthprocessedwords.append(tmpword)
for fifthword in fifthwords:
    tmpword = ''
    for w in fifthword:
        tmpword+=w
    fifthprocessedwords.append(tmpword)
for sixthword in sixthwords:
    tmpword = ''
    for w in sixthword:
        tmpword+=w
    sixthprocessedwords.append(tmpword)

# print(thirdprocessedwords)
# print(len(thirdprocessedwords))
# print(fouthprocessedwords)
# print(len(fouthprocessedwords))
# print(fifthprocessedwords)
# print(len(fifthprocessedwords))
# print(sixthprocessedwords)
# print(len(sixthprocessedwords))
thirdprocessedwords = set(thirdprocessedwords)
fouthprocessedwords = set(fouthprocessedwords)
fifthprocessedwords = set(fifthprocessedwords)
sixthprocessedwords = set(sixthprocessedwords)

thirdcheckedwords = []
fouthcheckedwords = []
fifthcheckedwords = []
sixthcheckedwords = []
for word in thirdprocessedwords:
    if d.check(word):
        thirdcheckedwords.append(word)
for word in fouthprocessedwords:
    if d.check(word):
        fouthcheckedwords.append(word)
for word in fifthprocessedwords:
    if d.check(word):
        fifthcheckedwords.append(word)
for word in sixthprocessedwords:
    if d.check(word):
        sixthcheckedwords.append(word)

print('-'*100)

print('Three letters checked:',thirdcheckedwords)
print('Four letters checked:',fouthcheckedwords)
print('Five letters checked:',fifthcheckedwords)
print('Six letters checked:',sixthcheckedwords)

tEnd = time.time()#计时结束
print('It cost ',format(tEnd-tStart,'0.2f'),'second')

程序码 version2

参考:python 生成不重复和可重复的排列组合

改用可重复的排列组合

笛卡尔积:itertools.product(*iterables[, repeat])

程序码 version2,如下所示:

from itertools import product
import enchant
import time

d = enchant.Dict("en_US")

input_words = input()
tStart = time.time()#计时开始

thirdwords = set(product(input_words, repeat = 3))
# print(len(thirdwords))
fouthwords = set(product(input_words, repeat = 4))
# print(len(fouthwords))
fifthwords = set(product(input_words, repeat = 5))
# print(len(fifthwords))
sixthwords = set(product(input_words, repeat = 6))
# print(len(sixthwords))

thirdprocessedwords = []
fouthprocessedwords = []
fifthprocessedwords = []
sixthprocessedwords = []
for thirdword in thirdwords:
    tmpword = ''
    for w in thirdword:
        tmpword+=w
    thirdprocessedwords.append(tmpword)
for fouthword in fouthwords:
    tmpword = ''
    for w in fouthword:
        tmpword+=w
    fouthprocessedwords.append(tmpword)
for fifthword in fifthwords:
    tmpword = ''
    for w in fifthword:
        tmpword+=w
    fifthprocessedwords.append(tmpword)
for sixthword in sixthwords:
    tmpword = ''
    for w in sixthword:
        tmpword+=w
    sixthprocessedwords.append(tmpword)

# print(thirdprocessedwords)
# print(len(thirdprocessedwords))
# print(fouthprocessedwords)
# print(len(fouthprocessedwords))
# print(fifthprocessedwords)
# print(len(fifthprocessedwords))
# print(sixthprocessedwords)
# print(len(sixthprocessedwords))
thirdprocessedwords = set(thirdprocessedwords)
fouthprocessedwords = set(fouthprocessedwords)
fifthprocessedwords = set(fifthprocessedwords)
sixthprocessedwords = set(sixthprocessedwords)

thirdcheckedwords = []
fouthcheckedwords = []
fifthcheckedwords = []
sixthcheckedwords = []
for word in thirdprocessedwords:
    if d.check(word):
        thirdcheckedwords.append(word)
for word in fouthprocessedwords:
    if d.check(word):
        fouthcheckedwords.append(word)
for word in fifthprocessedwords:
    if d.check(word):
        fifthcheckedwords.append(word)
for word in sixthprocessedwords:
    if d.check(word):
        sixthcheckedwords.append(word)

print('-'*100)

print('Three letters checked:',sorted(thirdcheckedwords))
print('Four letters checked:',sorted(fouthcheckedwords))
print('Five letters checked:',sorted(fifthcheckedwords))
print('Six letters checked:',sorted(sixthcheckedwords))

tEnd = time.time()#计时结束
print('It cost ',format(tEnd-tStart,'0.2f'),'second')

<<:  [Day25] Query Builder查询生产器

>>:  Day 26 : 公平指标与实作 Fairness Indicators

[DAY 30] 未完的终点是下一场的起点 : 总结以及各式好用连结

前言 总算来到第30天啦 XDDDD 用废文开始就用废文结束吧 : 不知不觉已经来到了最後一章啦,这...

Day04 - Python基本语法 Part 1

今天开始将进行Python基本语法练习,因大部分语法跟很多程序语言相似,故这个部分将主要以笔记方式注...

Leetcode 挑战 Day 02 [9. Palindrome Number]

9. Palindrome Number Palindrome Number中文意思即是回文数字,这...

Day17 - 进入轻前端 Vue 前的范例

这篇主要整合以下几点来实作 Tag Helper 动态 新增/删除 订单项目 後端加上计算功能 方便...