examples/word_counter
							parent
							
								
									8b27681de0
								
							
						
					
					
						commit
						44677db75b
					
				|  | @ -0,0 +1,24 @@ | |||
| ``` | ||||
| usage: word_counter [text_file] | ||||
| using cinderella.txt | ||||
| able => 2 | ||||
| afterwards => 1 | ||||
| after => 1 | ||||
| against => 2 | ||||
| again => 10 | ||||
| allowed => 2 | ||||
| allow => 1 | ||||
| all => 12 | ||||
| along => 1 | ||||
| also => 2 | ||||
| always => 2 | ||||
| and => 140 | ||||
| anew => 1 | ||||
| anger => 1 | ||||
| another => 2 | ||||
| answered => 1 | ||||
| anyone => 2 | ||||
| any => 1 | ||||
| an => 4 | ||||
| ... | ||||
| ```: | ||||
|  | @ -0,0 +1,250 @@ | |||
| A rich man's wife became sick, and when she felt that her end was drawing near, | ||||
| she called her only daughter to her bedside and said, "Dear child, remain pious | ||||
| and good, and then our dear God will always protect you, and I will look down | ||||
| on you from heaven and be near you." With this she closed her eyes and died. | ||||
| 
 | ||||
| The girl went out to her mother's grave every day and wept, and she remained | ||||
| pious and good. When winter came the snow spread a white cloth over the grave, | ||||
| and when the spring sun had removed it again, the man took himself another | ||||
| wife. | ||||
| 
 | ||||
| This wife brought two daughters into the house with her. They were beautiful, | ||||
| with fair faces, but evil and dark hearts. Times soon grew very bad for the | ||||
| poor stepchild. | ||||
| 
 | ||||
| "Why should that stupid goose sit in the parlor with us?" they said. "If she | ||||
| wants to eat bread, then she will have to earn it. Out with this kitchen maid!" | ||||
| 
 | ||||
| They took her beautiful clothes away from her, dressed her in an old gray | ||||
| smock, and gave her wooden shoes. "Just look at the proud princess! How decked | ||||
| out she is!" they shouted and laughed as they led her into the kitchen. | ||||
| 
 | ||||
| There she had to do hard work from morning until evening, get up before | ||||
| daybreak, carry water, make the fires, cook, and wash. Besides this, the | ||||
| sisters did everything imaginable to hurt her. They made fun of her, scattered | ||||
| peas and lentils into the ashes for her, so that she had to sit and pick them | ||||
| out again. In the evening when she had worked herself weary, there was no bed | ||||
| for her. Instead she had to sleep by the hearth in the ashes. And because she | ||||
| always looked dusty and dirty, they called her Cinderella. | ||||
| 
 | ||||
| One day it happened that the father was going to the fair, and he asked his two | ||||
| stepdaughters what he should bring back for them. | ||||
| 
 | ||||
| "Beautiful dresses," said the one. | ||||
| 
 | ||||
| "Pearls and jewels," said the other. | ||||
| 
 | ||||
| "And you, Cinderella," he said, "what do you want?" | ||||
| 
 | ||||
| "Father, break off for me the first twig that brushes against your hat on your | ||||
| way home." | ||||
| 
 | ||||
| So he bought beautiful dresses, pearls, and jewels for his two stepdaughters. | ||||
| On his way home, as he was riding through a green thicket, a hazel twig brushed | ||||
| against him and knocked off his hat. Then he broke off the twig and took it | ||||
| with him. Arriving home, he gave his stepdaughters the things that they had | ||||
| asked for, and he gave Cinderella the twig from the hazel bush. | ||||
| 
 | ||||
| Cinderella thanked him, went to her mother's grave, and planted the branch on | ||||
| it, and she wept so much that her tears fell upon it and watered it. It grew | ||||
| and became a beautiful tree. | ||||
| 
 | ||||
| Cinderella went to this tree three times every day, and beneath it she wept and | ||||
| prayed. A white bird came to the tree every time, and whenever she expressed a | ||||
| wish, the bird would throw down to her what she had wished for. | ||||
| 
 | ||||
| Now it happened that the king proclaimed a festival that was to last three | ||||
| days. All the beautiful young girls in the land were invited, so that his son | ||||
| could select a bride for himself. When the two stepsisters heard that they too | ||||
| had been invited, they were in high spirits. | ||||
| 
 | ||||
| They called Cinderella, saying, "Comb our hair for us. Brush our shoes and | ||||
| fasten our buckles. We are going to the festival at the king's castle." | ||||
| 
 | ||||
| Cinderella obeyed, but wept, because she too would have liked to go to the | ||||
| dance with them. She begged her stepmother to allow her to go. | ||||
| 
 | ||||
| "You, Cinderella?" she said. "You, all covered with dust and dirt, and you want | ||||
| to go to the festival?. You have neither clothes nor shoes, and yet you want to | ||||
| dance!" | ||||
| 
 | ||||
| However, because Cinderella kept asking, the stepmother finally said, "I have | ||||
| scattered a bowl of lentils into the ashes for you. If you can pick them out | ||||
| again in two hours, then you may go with us." | ||||
| 
 | ||||
| The girl went through the back door into the garden, and called out, "You tame | ||||
| pigeons, you turtledoves, and all you birds beneath the sky, come and help me | ||||
| to gather: | ||||
| 
 | ||||
| The good ones go into the pot, The bad ones go into your crop." Two white | ||||
| pigeons came in through the kitchen window, and then the turtledoves, and | ||||
| finally all the birds beneath the sky came whirring and swarming in, and lit | ||||
| around the ashes. The pigeons nodded their heads and began to pick, pick, pick, | ||||
| pick. And the others also began to pick, pick, pick, pick. They gathered all | ||||
| the good grains into the bowl. Hardly one hour had passed before they were | ||||
| finished, and they all flew out again.  The girl took the bowl to her | ||||
| stepmother, and was happy, thinking that now she would be allowed to go to the | ||||
| festival with them. | ||||
| 
 | ||||
| But the stepmother said, "No, Cinderella, you have no clothes, and you don't | ||||
| know how to dance. Everyone would only laugh at you." | ||||
| 
 | ||||
| Cinderella began to cry, and then the stepmother said, "You may go if you are | ||||
| able to pick two bowls of lentils out of the ashes for me in one hour," | ||||
| thinking to herself, "She will never be able to do that." | ||||
| 
 | ||||
| The girl went through the back door into the garden, and called out, "You tame | ||||
| pigeons, you turtledoves, and all you birds beneath the sky, come and help me | ||||
| to gather: | ||||
| 
 | ||||
| The good ones go into the pot, The bad ones go into your crop." Two white | ||||
| pigeons came in through the kitchen window, and then the turtledoves, and | ||||
| finally all the birds beneath the sky came whirring and swarming in, and lit | ||||
| around the ashes. The pigeons nodded their heads and began to pick, pick, pick, | ||||
| pick. And the others also began to pick, pick, pick, pick. They gathered all | ||||
| the good grains into the bowls. Before a half hour had passed they were | ||||
| finished, and they all flew out again.  The girl took the bowls to her | ||||
| stepmother, and was happy, thinking that now she would be allowed to go to the | ||||
| festival with them. | ||||
| 
 | ||||
| But the stepmother said, "It's no use. You are not coming with us, for you have | ||||
| no clothes, and you don't know how to dance. We would be ashamed of you." With | ||||
| this she turned her back on Cinderella, and hurried away with her two proud | ||||
| daughters. | ||||
| 
 | ||||
| Now that no one else was at home, Cinderella went to her mother's grave beneath | ||||
| the hazel tree, and cried out: | ||||
| 
 | ||||
| Shake and quiver, little tree, Throw gold and silver down to me.  Then the bird | ||||
| threw a gold and silver dress down to her, and slippers embroidered with silk | ||||
| and silver. She quickly put on the dress and went to the festival.  Her | ||||
| stepsisters and her stepmother did not recognize her. They thought she must be | ||||
| a foreign princess, for she looked so beautiful in the golden dress. They never | ||||
| once thought it was Cinderella, for they thought that she was sitting at home | ||||
| in the dirt, looking for lentils in the ashes. | ||||
| 
 | ||||
| The prince approached her, took her by the hand, and danced with her. | ||||
| Furthermore, he would dance with no one else. He never let go of her hand, and | ||||
| whenever anyone else came and asked her to dance, he would say, "She is my | ||||
| dance partner." | ||||
| 
 | ||||
| She danced until evening, and then she wanted to go home. But the prince said, | ||||
| "I will go along and escort you," for he wanted to see to whom the beautiful | ||||
| girl belonged. However, she eluded him and jumped into the pigeon coop. The | ||||
| prince waited until her father came, and then he told him that the unknown girl | ||||
| had jumped into the pigeon coop. | ||||
| 
 | ||||
| The old man thought, "Could it be Cinderella?" | ||||
| 
 | ||||
| He had them bring him an ax and a pick so that he could break the pigeon coop | ||||
| apart, but no one was inside. When they got home Cinderella was lying in the | ||||
| ashes, dressed in her dirty clothes. A dim little oil-lamp was burning in the | ||||
| fireplace. Cinderella had quickly jumped down from the back of the pigeon coop | ||||
| and had run to the hazel tree. There she had taken off her beautiful clothes | ||||
| and laid them on the grave, and the bird had taken them away again. Then, | ||||
| dressed in her gray smock, she had returned to the ashes in the kitchen. | ||||
| 
 | ||||
| The next day when the festival began anew, and her parents and her stepsisters | ||||
| had gone again, Cinderella went to the hazel tree and said: | ||||
| 
 | ||||
| Shake and quiver, little tree, Throw gold and silver down to me.  Then the bird | ||||
| threw down an even more magnificent dress than on the preceding day. When | ||||
| Cinderella appeared at the festival in this dress, everyone was astonished at | ||||
| her beauty. The prince had waited until she came, then immediately took her by | ||||
| the hand, and danced only with her. When others came and asked her to dance | ||||
| with them, he said, "She is my dance partner." When evening came she wanted to | ||||
| leave, and the prince followed her, wanting to see into which house she went. | ||||
| But she ran away from him and into the garden behind the house. A beautiful | ||||
| tall tree stood there, on which hung the most magnificent pears. She climbed as | ||||
| nimbly as a squirrel into the branches, and the prince did not know where she | ||||
| had gone. He waited until her father came, then said to him, "The unknown girl | ||||
| has eluded me, and I believe she has climbed up the pear tree. | ||||
| 
 | ||||
| The father thought, "Could it be Cinderella?" He had an ax brought to him and | ||||
| cut down the tree, but no one was in it. When they came to the kitchen, | ||||
| Cinderella was lying there in the ashes as usual, for she had jumped down from | ||||
| the other side of the tree, had taken the beautiful dress back to the bird in | ||||
| the hazel tree, and had put on her gray smock. | ||||
| 
 | ||||
| On the third day, when her parents and sisters had gone away, Cinderella went | ||||
| again to her mother's grave and said to the tree: | ||||
| 
 | ||||
| Shake and quiver, little tree, Throw gold and silver down to me.  This time the | ||||
| bird threw down to her a dress that was more splendid and magnificent than any | ||||
| she had yet had, and the slippers were of pure gold. When she arrived at the | ||||
| festival in this dress, everyone was so astonished that they did not know what | ||||
| to say. The prince danced only with her, and whenever anyone else asked her to | ||||
| dance, he would say, "She is my dance partner." When evening came Cinderella | ||||
| wanted to leave, and the prince tried to escort her, but she ran away from him | ||||
| so quickly that he could not follow her. The prince, however, had set a trap. | ||||
| He had had the entire stairway smeared with pitch. When she ran down the | ||||
| stairs, her left slipper stuck in the pitch. The prince picked it up. It was | ||||
| small and dainty, and of pure gold. | ||||
| 
 | ||||
| The next morning, he went with it to the man, and said to him, "No one shall be | ||||
| my wife except for the one whose foot fits this golden shoe." | ||||
| 
 | ||||
| The two sisters were happy to hear this, for they had pretty feet. With her | ||||
| mother standing by, the older one took the shoe into her bedroom to try it on. | ||||
| She could not get her big toe into it, for the shoe was too small for her. Then | ||||
| her mother gave her a knife and said, "Cut off your toe. When you are queen you | ||||
| will no longer have to go on foot." | ||||
| 
 | ||||
| The girl cut off her toe, forced her foot into the shoe, swallowed the pain, | ||||
| and went out to the prince. He took her on his horse as his bride and rode away | ||||
| with her. However, they had to ride past the grave, and there, on the hazel | ||||
| tree, sat the two pigeons, crying out: | ||||
| 
 | ||||
| Rook di goo, rook di goo!  There's blood in the shoe.  The shoe is too tight, | ||||
| This bride is not right!  Then he looked at her foot and saw how the blood was | ||||
| running from it. He turned his horse around and took the false bride home | ||||
| again, saying that she was not the right one, and that the other sister should | ||||
| try on the shoe. She went into her bedroom, and got her toes into the shoe all | ||||
| right, but her heel was too large.  Then her mother gave her a knife, and said, | ||||
| "Cut a piece off your heel. When you are queen you will no longer have to go on | ||||
| foot." | ||||
| 
 | ||||
| The girl cut a piece off her heel, forced her foot into the shoe, swallowed the | ||||
| pain, and went out to the prince. He took her on his horse as his bride and | ||||
| rode away with her. When they passed the hazel tree, the two pigeons were | ||||
| sitting in it, and they cried out: | ||||
| 
 | ||||
| Rook di goo, rook di goo!  There's blood in the shoe.  The shoe is too tight, | ||||
| This bride is not right!  He looked down at her foot and saw how the blood was | ||||
| running out of her shoe, and how it had stained her white stocking all red. | ||||
| Then he turned his horse around and took the false bride home again.  "This is | ||||
| not the right one, either," he said. "Don't you have another daughter?" | ||||
| 
 | ||||
| "No," said the man. "There is only a deformed little Cinderella from my first | ||||
| wife, but she cannot possibly be the bride." | ||||
| 
 | ||||
| The prince told him to send her to him, but the mother answered, "Oh, no, she | ||||
| is much too dirty. She cannot be seen." | ||||
| 
 | ||||
| But the prince insisted on it, and they had to call Cinderella. She first | ||||
| washed her hands and face clean, and then went and bowed down before the | ||||
| prince, who gave her the golden shoe. She sat down on a stool, pulled her foot | ||||
| out of the heavy wooden shoe, and put it into the slipper, and it fitted her | ||||
| perfectly. | ||||
| 
 | ||||
| When she stood up the prince looked into her face, and he recognized the | ||||
| beautiful girl who had danced with him. He cried out, "She is my true bride." | ||||
| 
 | ||||
| The stepmother and the two sisters were horrified and turned pale with anger. | ||||
| The prince, however, took Cinderella onto his horse and rode away with her. As | ||||
| they passed by the hazel tree, the two white pigeons cried out: | ||||
| 
 | ||||
| Rook di goo, rook di goo!  No blood's in the shoe.  The shoe's not too tight, | ||||
| This bride is right!  After they had cried this out, they both flew down and | ||||
| lit on Cinderella's shoulders, one on the right, the other on the left, and | ||||
| remained sitting there.  When the wedding with the prince was to be held, the | ||||
| two false sisters came, wanting to gain favor with Cinderella and to share her | ||||
| good fortune. When the bridal couple walked into the church, the older sister | ||||
| walked on their right side and the younger on their left side, and the pigeons | ||||
| pecked out one eye from each of them. Afterwards, as they came out of the | ||||
| church, the older one was on the left side, and the younger one on the right | ||||
| side, and then the pigeons pecked out the other eye from each of them. And | ||||
| thus, for their wickedness and falsehood, they were punished with blindness as | ||||
| long as they lived. | ||||
| 
 | ||||
| 
 | ||||
|  | @ -0,0 +1,57 @@ | |||
| import os | ||||
| 
 | ||||
| fn main() { | ||||
| 	mut path = 'cinderella.txt' | ||||
| 	if os.args.len != 2 { | ||||
| 		println('usage: word_counter [text_file]') | ||||
| 		println('using $path') | ||||
| 	} | ||||
| 	else { | ||||
| 		path = os.args[1] | ||||
| 	} | ||||
| 	lines := os.read_file_lines(path.trim_space()) | ||||
| 	mut m := map[string]int{} | ||||
| 	for line in lines { | ||||
| 		words := line.to_lower().split(' ') | ||||
| 		for word in words { | ||||
| 			key := filter_word(word) | ||||
| 			if key == '' { | ||||
| 				continue | ||||
| 			} | ||||
| 			m[key] = m[key] + 1 | ||||
| 		} | ||||
| 	} | ||||
| 	// Sort the keys
 | ||||
| 	mut keys := []string | ||||
| 	for e in m.entries { | ||||
| 		keys << e.key | ||||
| 	} | ||||
| 	keys.sort() | ||||
| 	// Print the map
 | ||||
| 	for key in keys { | ||||
| 		val := m[key] | ||||
| 		println('$key => $val') | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // Removes punctuation
 | ||||
| fn filter_word(word string) string { | ||||
| 	if word == '' || word == ' ' { | ||||
| 		return '' | ||||
| 	} | ||||
| 	mut i := 0 | ||||
| 	for i < word.len && !is_letter(word[i]) { | ||||
| 		i++ | ||||
| 	} | ||||
| 	start := i | ||||
| 	for i < word.len && is_letter(word[i]) { | ||||
| 		i++ | ||||
| 	} | ||||
| 	end := i | ||||
| 	return word.substr(start, end) | ||||
| } | ||||
| 
 | ||||
| fn is_letter(c byte) bool { | ||||
| 	return c.is_letter() | ||||
| } | ||||
| 
 | ||||
		Loading…
	
		Reference in New Issue