![]() |
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[]) // :D--------------------- 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::
Type a line of English: This is it, is it? It is, indeed, it!!Also, that is my test string, it just made it easier for testing. |
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.
|
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)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() |
| All times are GMT -5. The time now is 8:39 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC