Skip to content

Commit 9cdede5

Browse files
committed
2 parents 9caf185 + 36acf5e commit 9cdede5

4 files changed

Lines changed: 128 additions & 5 deletions

File tree

pre-course/beginning_ruby/00-text_analyzer.rb

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,51 @@
1212
# * average number of words per sentence
1313
# * average number of sentences in a paragraph
1414

15-
lines = File.readlines("lib/text.txt")
15+
stopwords = %w{the a by on for of are with just but and to the my I has some in}
16+
17+
# Check file path / usage
18+
if !ARGV.empty? && File.exist?(ARGV[0])
19+
path = ARGV[0]
20+
else
21+
puts "Usage: 00-text_analyzer.rb path"
22+
end
23+
24+
# Read file, count lines
25+
lines = File.readlines(path)
1626
line_count = lines.size
1727
text = lines.join
1828

29+
# Count characters
1930
total_characters = text.length
20-
2131
total_characters_nospaces = text.gsub(/\s+/, '').length
2232

33+
# Count words
2334
word_count = text.split.length
35+
interesting_word_count = text.split.select{|word| !stopwords.include? word}.length
2436

37+
# Count sentences and paragraphs
2538
sentence_count = text.split(/\.|\?|!/).length
26-
2739
paragraph_count = text.split(/\n\n/).length
2840

41+
# Calculate averages and percentage
2942
average_words_per_sentence = word_count / sentence_count
30-
3143
average_sentences_per_paragraph = sentence_count / paragraph_count
44+
percentage_interesting_words = (100 * interesting_word_count.to_f / word_count).round
45+
46+
# Summarize text
47+
sentences = text.gsub(/\s+/, ' ').strip.split(/\.|\?|!/)
48+
sentences_sorted = sentences.sort_by {|sentence| sentence.length}
49+
one_third = sentences_sorted.length / 3
50+
ideal_sentences = sentences_sorted.slice(one_third, one_third + 1)
51+
ideal_sentences = ideal_sentences.select {|sentence| sentence =~ /is|are/}
3252

3353
puts "#{line_count} lines"
3454
puts "#{total_characters} characters"
3555
puts "#{total_characters_nospaces} characters (excluding spaces)"
36-
puts "#{word_count} words"
56+
puts "#{word_count} words, #{percentage_interesting_words}% of which are interesting"
3757
puts "#{sentence_count} sentences"
3858
puts "#{paragraph_count} paragraphs"
3959
puts "#{average_words_per_sentence} words per sentence (on average)"
4060
puts "#{average_sentences_per_paragraph} sentences per paragraph (on average)"
61+
puts "Summary:\n\n" + ideal_sentences.join(". ")
62+
puts "-- End of analysis"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Beginning Ruby - Second Edition
2+
# Exercise from Chapter 4 - Developing your first Ruby Application
3+
# Sam Gerber
4+
5+
text = %q{
6+
Ruby is a great programming language. It is object-oriented
7+
and has many groovy features. Some people don't like it, but that's
8+
not our problem! It's easy to learn. It's great. To learn more about Ruby,
9+
visit the official Ruby web site today.
10+
}
11+
12+
sentences = text.gsub(/\s+/, ' ').strip.split(/\.|\?|!/)
13+
sentences_sorted = sentences.sort_by {|sentence| sentence.length}
14+
one_third = sentences_sorted.length / 3
15+
ideal_sentences = sentences_sorted.slice(one_third, one_third + 1)
16+
ideal_sentences = ideal_sentences.select {|sentence| sentence =~ /is|are/}
17+
puts ideal_sentences.join(". ")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Beginning Ruby - Second Edition
2+
# Exercise from Chapter 4 - Developing your first Ruby Application
3+
# Sam Gerber
4+
5+
puts ARGV.join('-')
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Beginning Ruby - Second Edition
2+
# Exercise from Chapter 6 - Building a Dungeon Text Adventure with Objects
3+
# Sam Gerber
4+
5+
class Dungeon
6+
attr_accessor :player
7+
8+
def initialize(player_name)
9+
@player = Player.new(player_name)
10+
@rooms = []
11+
end
12+
13+
def add_room(reference, name, description, connections)
14+
@rooms << Room.new(reference, name, description, connections)
15+
end
16+
17+
def start(location)
18+
@player.location = location
19+
show_current_description
20+
end
21+
22+
def show_current_description
23+
puts find_room_in_dungeon(@player.location).full_description
24+
end
25+
26+
def find_room_in_dungeon(reference)
27+
@rooms.detect{|room| room.reference == reference}
28+
end
29+
30+
def find_room_in_direction(direction)
31+
find_room_in_dungeon(@player.location).connections[direction]
32+
end
33+
34+
def go(direction)
35+
puts "You go " + direction.to_s
36+
@player.location = find_room_in_direction(direction)
37+
show_current_description
38+
end
39+
40+
class Player
41+
attr_accessor :name, :location
42+
43+
def initialize(name)
44+
@name = name
45+
end
46+
end
47+
48+
class Room
49+
attr_accessor :reference, :name, :description, :connections
50+
51+
def initialize(reference, name, description, connections)
52+
@reference = reference
53+
@name = name
54+
@description = description
55+
@connections = connections
56+
end
57+
58+
def full_description
59+
@name + "\n\nYou are in " + @description
60+
end
61+
end
62+
63+
end
64+
65+
# Create the main dungeon object
66+
my_dungeon = Dungeon.new("Fred Bloggs")
67+
68+
# Add rooms to the dungeon
69+
my_dungeon.add_room(:largecave, "Large Cave", "a large cavernous cave", {
70+
:west => :smallcave})
71+
72+
my_dungeon.add_room(:smallcave, "Small Cave", "a small, claustrophobic cave", {
73+
:east => :largecave})
74+
75+
# Start the dungeon by placing the player in the large cave
76+
my_dungeon.start(:largecave)
77+
my_dungeon.go(:west)
78+
my_dungeon.go(:east)
79+

0 commit comments

Comments
 (0)