wordsoup 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.byebug_history +7 -0
- data/.gitignore +10 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/author.rb +110 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/bible.txt +30382 -0
- data/lib/hamlet.txt +5588 -0
- data/lib/omats.txt +2830 -0
- data/lib/wordsoup/version.rb +3 -0
- data/lib/wordsoup.rb +108 -0
- data/wordsoup.gemspec +34 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0fbb14045261c2ce7618e9b1a0c5e30118922aed
|
4
|
+
data.tar.gz: 758797db870ed696c6a7cac56a0bbb5866fca97e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ab3328c0073de488b7a7377c75a8ba9b6678a748f7aba98571c2bd5aa517b28d6da305b37ed3ac246e75921f7e702d10531563473180ca07e67127bf5d84cc47
|
7
|
+
data.tar.gz: 9f7d4ebdc8d51d6b56b2b2b3b8135406fc7f83d7419ff20963b6d12ae382b5022544530eba2d639c375295ce9ba0a5052d22f101cbc6bf1d8a959771e7f241b1
|
data/.byebug_history
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Wordsoup
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/wordsoup`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'wordsoup'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install wordsoup
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/wordsoup.
|
36
|
+
|
data/Rakefile
ADDED
data/author.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'byebug'
|
2
|
+
class Author
|
3
|
+
|
4
|
+
attr_accessor :sentences, :word_hash, :first_words, :last_words
|
5
|
+
|
6
|
+
def initialize(file)
|
7
|
+
@sentences = make_sentence_array(file)
|
8
|
+
@word_hash = Hash.new { |h, k| h[k] = [] }
|
9
|
+
@first_words = []
|
10
|
+
@last_words = []
|
11
|
+
make_word_hash
|
12
|
+
end
|
13
|
+
|
14
|
+
def make_sentence_array(file)
|
15
|
+
text = ""
|
16
|
+
File.open(file).each do |line|
|
17
|
+
text << line
|
18
|
+
end
|
19
|
+
text = text.gsub(/\n/, " ")
|
20
|
+
text = text.gsub(/"/, "")
|
21
|
+
sentences = text.split(" ")
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
def make_word_hash()
|
26
|
+
@sentences.each do |sentence|
|
27
|
+
sentence_array = sentence.split(" ")
|
28
|
+
sentence_array.each_with_index do |word, i|
|
29
|
+
@first_words << word if i == 0
|
30
|
+
@last_words << word if i == sentence_array.length - 1
|
31
|
+
unless i == sentence_array.length - 1
|
32
|
+
@word_hash[word] << sentence_array[i+1]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def sentence
|
39
|
+
sentence = [@first_words.sample.capitalize]
|
40
|
+
word = ""
|
41
|
+
until (sentence.length == 30)
|
42
|
+
word = @word_hash[sentence.last].sample
|
43
|
+
break if word.nil?
|
44
|
+
word = fix_word(word)
|
45
|
+
sentence << word
|
46
|
+
break if ["!","?","."].include?(word[word.length-1])
|
47
|
+
end
|
48
|
+
sentence = sentence.join(" ")
|
49
|
+
sentence[sentence.length-1] = "" if [",", ":", ";"].include?(sentence[sentence.length-1])
|
50
|
+
unless ["!","?","."].include?(sentence[sentence.length-1])
|
51
|
+
sentence << "?"
|
52
|
+
end
|
53
|
+
sentence
|
54
|
+
end
|
55
|
+
|
56
|
+
def paragraph
|
57
|
+
paragraph = ""
|
58
|
+
until paragraph.length > 250
|
59
|
+
paragraph << " "
|
60
|
+
paragraph << self.sentence
|
61
|
+
end
|
62
|
+
paragraph
|
63
|
+
end
|
64
|
+
|
65
|
+
def fix_word(word)
|
66
|
+
word = word.downcase
|
67
|
+
capitalized_words = ["i'll", "i", "god", "i."]
|
68
|
+
if capitalized_words.include?(word)
|
69
|
+
word = word.capitalize
|
70
|
+
end
|
71
|
+
word
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
class Shakespeare < Author
|
77
|
+
|
78
|
+
def initialize(file = "hamlet.txt")
|
79
|
+
super(file)
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
class Hemingway < Author
|
85
|
+
|
86
|
+
def initialize(file = "omats.txt")
|
87
|
+
super(file)
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
class Hemingway < Author
|
93
|
+
|
94
|
+
def initialize(file = "omats.txt")
|
95
|
+
super(file)
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
class Bible < Author
|
100
|
+
|
101
|
+
def initialize(file = "bible.txt")
|
102
|
+
super(file)
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
$Shakespeare = Author.new('hamlet.txt')
|
110
|
+
$Hemingway = Author.new('omats.txt')
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "wordsoup"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|