Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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....
Python
import random

sentence = 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 a


my main problem is how do i use a dictionary to make it shorter?
Posted
Updated 8-Mar-14 23:56pm
v2
Comments
AspDotNetDev 9-Mar-14 0:33am    
You'd want to gather some data (words/synonyms) and build an application that uses that data. Did you have a particular question in mind that you might update your current question to reflect?
Vidhu Shah 9-Mar-14 1:15am    
actually now that i think of it, the problem i really have is how do i change each word in a sentence..
AspDotNetDev 9-Mar-14 1:17am    
Could you update your question with some further info regarding what you've tried and some sample code? Basically, you'd have to split the sentence into words, lookup each word in the thesaurus, then use the replacement rather than the original word.
Vidhu Shah 9-Mar-14 1:18am    
also i'd have to make a dictionary, wouldn't i... is there an easier way because it'd be pretty big ( all the words)...
AspDotNetDev 9-Mar-14 1:20am    
So, that's a second issue... acquiring the data. I've tried this in the past and it's a pain, but there are sources out there, though none that are up to date and that you'd be able to use for free. Again, please update your question with exactly the obstacles you are encountering and any details regarding what you've tried or where you're stuck.

1 solution

I don't know Python, but looking at the documentation, it'd go something like this (probably contains a few syntax errors):
Python
thesaurus = dict()
dataFile = open('somefile.dat', 'r')
for line in dataFile:
    parts = line.split('=')
    word = parts[0]
    synonyms = parts[1].split(',')
    thesaurus[word] = synonyms
sentence = raw_input("sentence: ")
originals = sentence.split()
replacements = []
for i,word in enumerate(originals):
    replacements[i] = random.choice(thesaurus[word])
print replacements

Keep 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,skip
sound=audio,noise,racket

If you wanted to avoid using up lots of memory, you could store the thesaurus in a database rather than in an in-memory dict.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900