Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 17th, 2005, 3:37 PM   #1
MrSmiley
Programmer
 
MrSmiley's Avatar
 
Join Date: May 2005
Posts: 41
Rep Power: 0 MrSmiley is on a distinguished road
PigLattin Converter, count number of words used in dictionary.

Ok, tried for a descriptive title. Anyways, I am having some troubles with my homework...

[PHP]
# Assignment 5 translate a set of strings into piglatting based strings
# It does so by moving leading consonants to the end of the word
# and appending "ay" to the end. (If there are no leading consonants,
# then "shay" is appended.)

#Global Variables, hey if I had a pointer, I'd store these in a 3d array and return all my info in a pointer.--
store = []
storen = []
stores = []
#Function String Breakup ------------------------------------------------------
def strbrk(word, ssize):
tstr = ""
subcount = 0
proper = "?.,';/><\=+-_()!~`@#$%^&*[]{}" #This here code belongs to the daily wtf.
counter = 0
#$store stores the letter, $storen stores the word it's in, and stores stores what part the non alpha is in.--
while counter <= (ssize-1) :
subcount = 0
for letter in word[counter] :
if letter in proper :
store.insert(counter, letter)
storen.insert(counter, counter)
stores.insert(counter, subcount)
subcount+=1
counter+=1
size=len(store)
counter = 0
#Takes the symbols out of the words. Will need these later for reconstruction.
if(size > 0):
while(counter <= size-1):
for letter in word[storen[counter]]:
if(letter in proper) :
subcount =0
else :
tstr+=letter
word[storen[counter]]= tstr.lower()
tstr=""
counter+=1
return word

#Function PigLatin---------------------------------------------------------
def piglatin(ssize, word):
vowels = "aeiou"
tstr =""
counter = 0
while(counter <= ssize-1):
for letter in word[counter]:
tstr+=letter
if tstr[0] in vowels :
tstr = tstr + "sh"
else :
tstr = tstr[1:] + tstr[0]
vowels = vowels + "y"
while not(tstr[0] in vowels) :
tstr = tstr[1:] + tstr[0]
tstr+="ay"
word[counter]=tstr
counter+=1
tstr=""
return word
#Function Translated Output-------------------------------------------------
def toutput(pigword, ssize):
subcount = 0
counter = 0
size = len(storen)
print "\nTranslated sentence:"
while(counter<=size-1) :
pigword[storen[counter]]+= store[counter]
counter+=1
counter = 0
while(counter<=ssize-1):
print pigword[counter],
counter +=1

#void main(int argc, char** argv[], char** env[]) // ---------------------
name = { }
subcount = 0
storec = []
counter = 0
#words2 = raw_input("Please type a line of English words: ")
words2 = "This is it, is it? It is, indeed, it!!"
word = words2.split()
ssize = len(word)
while counter < ssize :
word[counter] = word[counter].lower()
counter+=1
word = strbrk(word, ssize)
pigword = piglatin(ssize, word)
#Stuffs-----------------------------------------------------
counter = 0
while(counter < ssize):
for letter in pigword[counter]:
name[pigword[counter]]=0
counter+=1
counter = 0
size3=len(name)

while (counter < ssize) :
while(subcount < ssize):
if pigword[counter] == pigword[subcount]:
t+=1
name[pigword[counter]] = t
subcount+=1
t=0
counter +=1
#End Stuffs-----------------------------------------------------

toutput(pigword, ssize)
print "\n\nContents of pig-latin dictionary:"
print name
raw_input("\nPress Enter to quit")
[/PHP]

Perticularly in the "Stuffs" block. What I am attempting to do is count how many times a single word is used (such as "is" is used 3 times). I just cannot, for the life of me, figure out how to run a recursive array and get that information out.

Output looks like this:
Translated sentence:
isthay isshay itshay, isshay itshay? itshay isshay, indeedshay, itshay!! 

Contents of pig-latin dictionary:
{'isthay': 1, 'isshay': 0, 'itshay': 0, 'indeedshay': 0}
It is suppose to look like this:
Type a line of English: This is it, is it?   It is, indeed, it!!

Translated sentence:
isthay isshay itshay, isshay itshay?   itshay isshay, indeedshay, itshay!!

Contents of pig-latin dictionary:
indeed : indeedshay 1
is : isshay 3
it : itshay 4
this : isthay 1

Type Enter to finish.

Also, that is my test string, it just made it easier for testing.
MrSmiley is offline   Reply With Quote
Old Oct 17th, 2005, 3:54 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Break your data into words, sort them, and count the duplicates. You will find that breaking material into "words" isn't trivial, except for trivial material, but you can make a good start.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 17th, 2005, 4:47 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Eeep! You're doing it the hard way! Honestly, you don't need to work so hard. Python is very good at making things easy, but it does take some practise to wrap your head round.

I always find the best way to approach any programming problem is to divide it up into pieces. Something like:

1. Split sentence up into words.
2. Encode each word into pig latin.
3. Join sentence back up again.

You can then write out some pseudo-code:
words = split_words(sentence)
words = [piglatin(word) for word in words]
sentence = join_words(words)
Now, instead of one problem, you have three smaller problems. Namely, the functions split_words, piglatin, and join_words. If you keep dividing the problem into smaller and simpler pieces, you'll eventually end up at the answer.

However, I'm lecturing, and this doesn't immediately help you. To count the number of words, I suggest you look into 'bag' data structures. Bags are structures that might be used something like this (note that you'd need to create a Bag class first ):
>>> bag = Bag()
>>> bag.add("banana")
>>> bag.add("apple")
>>> bag.add("banana")
>>> bag.count("apple")
1
>>> bag.count("banana")
2
>>> bag.remove("banana")
>>> bag.count("banana")
1
>>> bag.contents
{"banana" : 1, "apple" : 1}
If you're still stuck, post up again. When you've finished the work, tell us how you did
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC