Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. #A. Rahman Sadiq
  2. #Due: Monday, November, 24th, 2014
  3. #Submitted to ICS3U1-03
  4. #The input for question 1 is the german sentance, output is the translated english sentance (from mydict)
  5. #The input for the extra part is mylist, the output is the length of the list (# of words)
  6. #The input for question 2 and 3 is sentance, the output is the number of times each word appears (for #2) and a new sentance with the words that appear more than once (for #3).
  7. #The input for question 4 is the english sentance, output is the translated german sentance (from mydict)
  8. #The input for question 5 and 6 is words (the string), the output is the number of times each word appears according to frequency (largest to smallest order).
  9.  
  10.  
  11. #This is the first part
  12. #Part 1-3 from word file
  13. #This is the first part
  14. myDict = {"befreit":"liberated", "baeche":"brooks", "eise":"ice", "sind":"are", "strom":"river", "und":"and", "vom":"from"}
  15. mySent = ("vom eise befreit sind strom und baeche") #defines german words as a sentance.
  16. for x in mySent.split(): #splits german sentance.
  17. print(myDict[x], end =" ") #From dictionary, prints english substitute for mysent.
  18.  
  19.  
  20. ############################################################################################################################################################
  21. #This is the first part (hardcode method)
  22. mysent = dict()
  23. mysent['vom eise befreit sind strom und baeche'] = 'from ice liberated are river and brooks' #Shows that german version equals to the english translation
  24. print(mysent)
  25. ############################################################################################################################################################
  26.  
  27.  
  28. ############################################################################################################################################################
  29. #This is extra (gets length of string)
  30. mylist = ["Felicity WILL you look this way exclaimed Mrs Appleton you have been fidgeting all morning Felicity tore her eyes reluctantly away from the coin she was holding in her hand and tried to concentrate on her class teacher who was talking about maths – something to do with multiplication Felicity vaguely recalled she caught matts eye and they grinned excitedly at each other this was it today was the day the two best friends had been waiting for this day for what had seemed an eternity both of them had been looking at the clock every 5 minutes both willing the hands to move forwards faster so that they could be whisked off home by day"]
  31. numberofwords = [len(sentence.split()) for sentence in mylist] #splits sentance into individual words than finds the length (equals to # of words)
  32. print(numberofwords)
  33. ############################################################################################################################################################
  34.  
  35.  
  36. #This is the second part
  37. mySent = ("Felicity WILL you look this way exclaimed Mrs Appleton you have been fidgeting all morning Felicity tore her eyes reluctantly away from the coin she was holding in her hand and tried to concentrate on her class teacher who was talking about maths – something to do with multiplication Felicity vaguely recalled she caught matts eye and they grinned excitedly at each other this was it today was the day the two best friends had been waiting for this day for what had seemed an eternity both of them had been looking at the clock every 5 minutes both willing the hands to move forwards faster so that they could be whisked off home by day")
  38. mySent = mySent.lower() #lowers the sentance
  39. SentCounter = dict() #makes it into a dict
  40. for x in mySent.split():
  41. SentCounter[x] = 0
  42. for x in mySent.split():
  43. SentCounter[x] += 1
  44. print (SentCounter) #prints out the amount of times the word appears in mysent
  45.  
  46.  
  47. #This is the third part
  48. XDict = dict() #defines XDict as a dict
  49. for x in SentCounter:
  50. if SentCounter[x] != 1: #if the number of words that appears in SentCounter is not 1
  51. XDict[x] = SentCounter[x]
  52. print (XDict) #prints the series of words that appear more than once
  53.  
  54.  
  55. ############################################################################################################################################################
  56. #This is the second and third part (using count)
  57. y=""
  58. sentance = "Felicity WILL you look this way exclaimed Mrs Appleton you have been fidgeting all morning Felicity tore her eyes reluctantly away from the coin she was holding in her hand and tried to concentrate on her class teacher who was talking about maths – something to do with multiplication Felicity vaguely recalled she caught matts eye and they grinned excitedly at each other this was it today was the day the two best friends had been waiting for this day for what had seemed an eternity both of them had been looking at the clock every 5 minutes both willing the hands to move forwards faster so that they could be whisked off home by day"
  59. lowercase = sentance.lower() #lower cases all words in string.
  60. words = lowercase.split()
  61. for word in words:
  62. print(word, " ",lowercase.count(" "+word+" ")) #prints out the number of times each word appears
  63. if lowercase.count(" "+word+ " ")!=1:
  64. y = y+word+" "
  65. print(y) #prints out the words that appear more than once, defined by the previous 2 lines.
  66. ############################################################################################################################################################
  67.  
  68.  
  69. #This is the fourth part
  70. #Part 4-6 from .txt file
  71. mydict = {"liberated":"befreit", "brooks":"baeche", "ice":"eise", "are":"sind", "river":"strom", "and":"und", "from":"vom"}
  72. mysent = ("from ice liberated are river and brooks") #Same as number one, mysent is the english sentance and the code splits the english sentance and replaces the words with its german substitute (from mydict), printing the translated german sentance.
  73. for x in mysent.split():
  74. print(mydict[x], end =" ")
  75.  
  76.  
  77. #This is the fifth and sixth part
  78. from collections import Counter
  79. words = "Banana Banana Banana Banana Banana Banana Banana Apples Apples Apples Apples Apples Strawberries Strawberries Strawberries Strawberries Strawberries Strawberries Eggs Eggs Eggs Eggs"
  80. freqs = Counter(words.split()) #splits the string into individual words and counts the # of times each word appears in the string, sorts according to frequency (largest to smallest)
  81. print(freqs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement