2016-09-26 49 views
-3

我需要建立一个程序,它打印出在我写的字典中的所有单词,所以我们可以说我写的房子,它能够站出来像字典打印出所有单词的I型什么

家庭,房子,高,中空,出来,过来,高估了,对,团结,无标题,急,歌曲,沉默,沉默,对不起,情感,精灵,鸡蛋,结束

所以它得到的所有词了我的字典与我写的字,这里是代码。

#User types in a word 
word = input("Type in a word: ") 

#initiate the dictionary struct. 
dictionary = {} 
#run through the dictionary file, one line at a time. 
with open("dict.txt") as dict_filehandle: 
for cword in dict_filehandle: 
#here a single line has been placed into the variable cword. 

    #no newlines are allowed in the words. 
    cword=cword.replace("\n",""); 

    #give the given word the value 1. 
    dictionary[cword]=1; 

#here we have a dictionary struct. 

#here we push through all the words in the dictionary 


print(dictionary["house"]) 

在我上面的代码中,我做了搜索“房子”,它表明,它是通过获取1号属实,我不知道该怎么做我上面说的事情,但我需要能够做到这一点,因为这是一个学校项目,我的老师建议我们在互联网上看,所以我决定只问人。

+0

您的注册已关闭。你能不能显示'dict.txt'文件中的内容? –

+0

@ Mr.goosberry https://www.dropbox.com/s/4a1ps1hkl187f8m/dict.txt?dl=0 – Kobbi

+2

我不明白你的问题,你希望用户输入'home',你的程序会识别出是指你的字典中的'house'?在你的代码中没有使用变量'word',所以我不知道如何建立连接...... –

回答

1

也许不是一个清晰的解决方案,而是一种方法,这是较长的注释说明:

之前,你可以得到所有匹配特定的字别人给你的程序的话,你需要进行分类的文件中的文字分类并建立映射。也就是说,它应该建立在某个地方,最好是在你的数据文件中,一个条目可以映射到哪个单词。然而,这是一项复杂的任务。正如你所看到的,一个给定的单词和其他你想要引用这个单词的单词需要通过一些逻辑来建模。构建这个逻辑就是你的问题的全部。如果你需要的是为一个给定的单词提供标准同义词,我可以建议这样的:words api。我真的不知道他们的API如何工作以及使用限制是多少。如果你做了一些搜索,你也可以找到一个开源的解决方案。祝你好运!