本文介绍了做一个简单的词库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嘿那里,我完成了python的所有基础课程,作为一个项目,我想制作一个简单的词库,我该如何制作呢? 这是我到目前为止所做的事情来感受...... import random sentence = raw_input( 句子:)a = sentence.split() for n,i in enumerate(a): if i == ' hi':a [n] = ' hello' if i == ' dumb':a [n] = random.choice([' 白痴',' stupid']) 打印 $ 我的主要问题是如何使用字典缩短字典?解决方案 我不懂Python,但看看文档,它会像这样(可能包含一些语法错误): thesaurus = dict() dataFile = open(' somefile.dat',' r') for line in dataFile: parts = line.split(' =') word = parts [ 0 ] synonyms = parts [ 1 ] .split(' ,')同义词库[word] =同义词 sentence = raw_input( 句子:) originals = sentence.split() replacementments = [] for i ,word in 枚举(原文): replacementments [i] = random.choice(thesaurus [word]) print 替换 请记住,这可能不是好代码。您需要添加检查以查看词库中是否存在该词等。此外,该代码假定文件采用以下格式: jump = hop,跳过声音=音频,噪音,球拍 如果你想避免浪费大量内存,你可以将同义词库存储在数据库而不是内存中的字典中。 Hey there, ive finished all the basic courses in python, and as a project i want to make a simple thesaurus, how should i go about making this?This is what ive done so far to get a feel....import randomsentence = raw_input("sentence: ")a = sentence.split()for n,i in enumerate(a): if i=='hi': a[n]='hello' if i == 'dumb': a[n] =random.choice(['idiot', 'stupid'])print amy main problem is how do i use a dictionary to make it shorter? 解决方案 I don't know Python, but looking at the documentation, it'd go something like this (probably contains a few syntax errors):thesaurus = dict()dataFile = open('somefile.dat', 'r')for line in dataFile: parts = line.split('=') word = parts[0] synonyms = parts[1].split(',') thesaurus[word] = synonymssentence = raw_input("sentence: ")originals = sentence.split()replacements = []for i,word in enumerate(originals): replacements[i] = random.choice(thesaurus[word])print replacementsKeep in mind that is probably not good code. You'll want to add checks to see if the word exists in the thesaurus and such. Also, that code assumes the file is in this format:jump=hop,skipsound=audio,noise,racketIf you wanted to avoid using up lots of memory, you could store the thesaurus in a database rather than in an in-memory dict. 这篇关于做一个简单的词库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 08:30
查看更多