markovian 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/.gitignore +18 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/changelog.md +8 -0
- data/db/seeds.rb +7 -0
- data/lib/.DS_Store +0 -0
- data/lib/markovian/chain/dictionary.rb +24 -0
- data/lib/markovian/chain/text_compiler.rb +66 -0
- data/lib/markovian/chain.rb +41 -0
- data/lib/markovian/chain_set.rb +22 -0
- data/lib/markovian/importers/twitter/csv_importer.rb +43 -0
- data/lib/markovian/importers/twitter/tweet.rb +37 -0
- data/lib/markovian/text_builder.rb +63 -0
- data/lib/markovian/utils/text_splitter.rb +35 -0
- data/lib/markovian/version.rb +3 -0
- data/lib/markovian.rb +11 -0
- data/markovian.gemspec +25 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 86536007f535d2ba077ed8ff3aeabf61d9ee6107
|
4
|
+
data.tar.gz: 61bd78dab63cafa0b0643ddb39793dec563beae5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e2695ad84f944bbb850e62693f3a96b4678f7d01ef54a557f58804056b6dea5f0431ca9cd21c38365676c61ae505cd931696b125b666672b9c30e42e335ec095
|
7
|
+
data.tar.gz: 4ba6686bba2a42acb3afc5effec994e18b168fab8c286db00afe1b401dc73cf09d9eb3f039e7aa66fa5202796795297ff1fd98d21963fd83f902ce25543b6d62
|
data/.gitignore
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile '~/.gitignore_global'
|
6
|
+
|
7
|
+
# Ignore bundler config.
|
8
|
+
/.bundle
|
9
|
+
|
10
|
+
# Ignore the default SQLite database.
|
11
|
+
/db/*.sqlite3
|
12
|
+
/db/*.sqlite3-journal
|
13
|
+
|
14
|
+
# Ignore all logfiles and tempfiles.
|
15
|
+
/log/*.log
|
16
|
+
/tmp
|
17
|
+
.DS_Store
|
18
|
+
Gemfile.lock
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
markov-ahkoppel
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in markov-ahkoppel2.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :development, :test do
|
7
|
+
gem 'byebug'
|
8
|
+
end
|
9
|
+
|
10
|
+
group :test do
|
11
|
+
gem "rspec"
|
12
|
+
gem "faker"
|
13
|
+
gem "codeclimate-test-reporter"
|
14
|
+
end
|
15
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Alex Koppel
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
[![Code Climate](https://codeclimate.com/github/arsduo/markovian/badges/gpa.svg)](https://codeclimate.com/github/arsduo/markovian)
|
2
|
+
[![Test Coverage](https://codeclimate.com/github/arsduo/markovian/badges/coverage.svg)](https://codeclimate.com/github/arsduo/markovian/coverage)
|
3
|
+
[![Build Status](https://travis-ci.org/arsduo/markovian.svg)](https://travis-ci.org/arsduo/markovian)
|
4
|
+
|
5
|
+
# A Markov Implementation
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Clone from Github, and then execute:
|
10
|
+
|
11
|
+
$ bundle
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Fuller documentation will come shortly. For now, let's see how we can use Markovian to build some tweets from a Twitter archive we've downloaded:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
> path = #{path_to_twitter_archive}
|
19
|
+
=> path_to_twitter_archive
|
20
|
+
> importer = Markovian::Importers::Twitter::CsvImporter.new(path)
|
21
|
+
=> #<Markovian::Importers::Twitter::CsvImporter:0x007fd0ca3282a8 @path=path_to_twitter_archive>
|
22
|
+
> tweets = importer.texts_for_markov_analysis; puts tweets.count
|
23
|
+
14394
|
24
|
+
=> nil
|
25
|
+
|
26
|
+
# Create a Chainset (the structure holding all the word relations)...
|
27
|
+
> chainset = Markovian::ChainSet.new
|
28
|
+
=> #<Markovian::ChainSet:0x007fd0ca03df70 ...>
|
29
|
+
# And add all the tweets to the Markov dictionary
|
30
|
+
> tweets.each {|t| Markovian::Chain::TextCompiler.new(t, chainset).incorporate_into_chain}; puts "done."
|
31
|
+
done.
|
32
|
+
=> nil
|
33
|
+
|
34
|
+
# Now, we can build some text!
|
35
|
+
> Markovian::TextBuilder.new("markov", chainset).construct
|
36
|
+
=> "markov chains a lot better than a month, i've been here half an hour of night when you can get behind belgium for the offline train journey"
|
37
|
+
```
|
38
|
+
|
39
|
+
Exactly!
|
40
|
+
|
41
|
+
## Development
|
42
|
+
|
43
|
+
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.
|
44
|
+
|
45
|
+
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).
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/arsduo/markovian. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
54
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "markovian"
|
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
|
data/bin/setup
ADDED
data/changelog.md
ADDED
data/db/seeds.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# This file should contain all the record creation needed to seed the database with its default values.
|
2
|
+
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
|
3
|
+
#
|
4
|
+
# Examples:
|
5
|
+
#
|
6
|
+
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
|
7
|
+
# Mayor.create(name: 'Emanuel', city: cities.first)
|
data/lib/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# This class represents a dictionary of words or phrases and the various words that can follow
|
2
|
+
# them. Currently it's implemented as a hash of arrays, but more advanced representations may
|
3
|
+
# follow.
|
4
|
+
#
|
5
|
+
# The key is an opaque value, which could represent either a single word or a phrase as desired.
|
6
|
+
module Markovian
|
7
|
+
class Chain
|
8
|
+
class Dictionary
|
9
|
+
def push(key, word)
|
10
|
+
dictionary[key] += [word]
|
11
|
+
end
|
12
|
+
|
13
|
+
def next_word(key)
|
14
|
+
dictionary[key].sample
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def dictionary
|
20
|
+
@dictionary ||= Hash.new([])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'markovian/utils/text_splitter'
|
2
|
+
|
3
|
+
# Given a text to analyze, this class returns a hash of Markov results: two-word phrases (two by
|
4
|
+
# default) pointing to an array of historical next words.
|
5
|
+
#
|
6
|
+
# So, for instance, the phrase "Cats are cute, cats are annoying" would map to:
|
7
|
+
# {
|
8
|
+
# "cats are" => [cute, annoying],
|
9
|
+
# "are cute" => [cats],
|
10
|
+
# "cute cats" => [are],
|
11
|
+
# }
|
12
|
+
#
|
13
|
+
# Notes:
|
14
|
+
# * Next words (in v1) are not unique, in order to represent weighting. There are definitely more
|
15
|
+
# space-compact ways to do that, but that's left for future implementation.
|
16
|
+
# * Punctuation is for later.
|
17
|
+
# * Handling sentences or newlines is later -- I'm not sure the right way to do it.
|
18
|
+
# * Capitalization is deferred for later.
|
19
|
+
module Markovian
|
20
|
+
class Chain
|
21
|
+
class TextCompiler
|
22
|
+
# Pass in a text, and optionally an existing Markov chain to add data to. In many cases, you
|
23
|
+
# may be building a chain using a set of smaller texts instead of one large texts (dialog,
|
24
|
+
# for instance, or Twitter archives), and so may call this class repeatedly for elements of
|
25
|
+
# the parent corpus.
|
26
|
+
attr_reader :text, :chainset
|
27
|
+
def initialize(text, starter_chainset = ChainSet.new)
|
28
|
+
@text = text
|
29
|
+
@chainset = starter_chainset
|
30
|
+
end
|
31
|
+
|
32
|
+
def incorporate_into_chain
|
33
|
+
add_text_to_chain(interesting_split_text, forward_chain)
|
34
|
+
# to assemble backward text, we just create a chainset with all the texts reversed
|
35
|
+
# that allows us to see what words precede any given word
|
36
|
+
add_text_to_chain(interesting_split_text.reverse, backward_chain)
|
37
|
+
chainset
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def forward_chain
|
43
|
+
chainset.forward
|
44
|
+
end
|
45
|
+
|
46
|
+
def backward_chain
|
47
|
+
chainset.backward
|
48
|
+
end
|
49
|
+
|
50
|
+
def add_text_to_chain(text_elements, chain)
|
51
|
+
previous_word = nil
|
52
|
+
text_elements.each_with_index do |word, index|
|
53
|
+
# if we're not at the beginning or the end of the text -- e.g. we have a full triple
|
54
|
+
if next_word = text_elements[index + 1]
|
55
|
+
chain.lengthen(word, next_word: next_word, previous_word: previous_word)
|
56
|
+
end
|
57
|
+
previous_word = word
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def interesting_split_text
|
62
|
+
@interesting_split_text ||= Utils::TextSplitter.new(text).components
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'markovian/chain/dictionary'
|
2
|
+
|
3
|
+
# The Chain represents Markov info as it's being assembled or expanded from a text. To compensate
|
4
|
+
# for small sample sizes, we track multiple chains (derived from both two-word phrases and single
|
5
|
+
# word). Phrases are prefered, but if we can't find a match, we'll try with a single word.
|
6
|
+
module Markovian
|
7
|
+
class Chain
|
8
|
+
def initialize
|
9
|
+
@one_key_dictionary = Dictionary.new
|
10
|
+
@two_key_dictionary = Dictionary.new
|
11
|
+
end
|
12
|
+
|
13
|
+
attr_reader :one_key_dictionary, :two_key_dictionary
|
14
|
+
def lengthen(word, next_word:, previous_word: nil)
|
15
|
+
@one_key_dictionary.push(word, next_word)
|
16
|
+
@two_key_dictionary.push(two_word_key(previous_word, word), next_word)
|
17
|
+
word
|
18
|
+
end
|
19
|
+
|
20
|
+
def next_word(word, previous_word: nil)
|
21
|
+
result_for_two_words(previous_word, word) || result_for_one_word(word)
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def result_for_two_words(previous_word, word)
|
27
|
+
@two_key_dictionary.next_word(two_word_key(previous_word, word)) if previous_word
|
28
|
+
end
|
29
|
+
|
30
|
+
def result_for_one_word(word)
|
31
|
+
@one_key_dictionary.next_word(word)
|
32
|
+
end
|
33
|
+
|
34
|
+
# We represent the two words as a space-delimited phrase for simplicity and speed of access via
|
35
|
+
# hash keys.
|
36
|
+
def two_word_key(word1, word2)
|
37
|
+
"#{word1} #{word2}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'markovian/chain'
|
2
|
+
|
3
|
+
# This class represents a pair of chains, one going forward and one going backward. With this, we
|
4
|
+
# can construct phrases in which the original seed word appears at any point in the text (going
|
5
|
+
# backward to create the earlier text, forward to create the rest).
|
6
|
+
module Markovian
|
7
|
+
class ChainSet
|
8
|
+
attr_reader :forward, :backward
|
9
|
+
def initialize
|
10
|
+
@forward, @backward = Chain.new, Chain.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def next_word(word, previous_word: nil)
|
14
|
+
forward.next_word(word, previous_word: previous_word)
|
15
|
+
end
|
16
|
+
|
17
|
+
def previous_word(word, following_word: nil)
|
18
|
+
# backward goes in the opposite direction to forward
|
19
|
+
backward.next_word(word, previous_word: following_word)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require 'markovian/importers/twitter/tweet'
|
3
|
+
|
4
|
+
# This class will import a Twitter archive CSV, returning a set of tweets suitable for importation
|
5
|
+
# into a Markovian chain.
|
6
|
+
module Markovian
|
7
|
+
module Importers
|
8
|
+
module Twitter
|
9
|
+
class CsvImporter
|
10
|
+
attr_reader :path
|
11
|
+
def initialize(path)
|
12
|
+
@path = path
|
13
|
+
end
|
14
|
+
|
15
|
+
def texts_for_markov_analysis
|
16
|
+
# reject any blank tweets -- in our case, those with only a stripped-out URL
|
17
|
+
tweet_enumerator.reject {|t| t.empty?}
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
def csv_enumerator
|
23
|
+
# returns an iterator object that we can roll through
|
24
|
+
# this does not actually start reading the file
|
25
|
+
@csv_enumerator ||= CSV.open(path, headers: true).each
|
26
|
+
end
|
27
|
+
|
28
|
+
# an iterator over personal tweets (e.g. not RTs)
|
29
|
+
# the lazy iterator allows us to add the condition without having to parse the entire file at
|
30
|
+
# once (which could easily encounter tens of thousands of rows).
|
31
|
+
def personal_tweet_enumerator
|
32
|
+
csv_enumerator.select {|row| row["retweeted_status_id"].empty? }
|
33
|
+
end
|
34
|
+
|
35
|
+
def tweet_enumerator
|
36
|
+
personal_tweet_enumerator.map do |row|
|
37
|
+
Tweet.new(row["text"]).interesting_text
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Markovian
|
2
|
+
module Importers
|
3
|
+
module Twitter
|
4
|
+
# Represents an individual tweet
|
5
|
+
class Tweet
|
6
|
+
attr_reader :text
|
7
|
+
def initialize(text)
|
8
|
+
@text = text
|
9
|
+
end
|
10
|
+
|
11
|
+
# Not currently used, but we might want to weight mentions later.
|
12
|
+
def mentions
|
13
|
+
text.scan(/(\@[a-z0-9_]+)/).flatten
|
14
|
+
end
|
15
|
+
|
16
|
+
def interesting_text
|
17
|
+
without_urls(without_leading_dot(text))
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
# We don't want URLs to be considered inside our Markov machine.
|
23
|
+
# URL matching is nearly impossible, but this regexp should be good enough: http://stackoverflow.com/questions/17733236/optimize-gruber-url-regex-for-javascript
|
24
|
+
# Nowadays Twitter replaces URLS with their own link shortener, but historically that wasn't
|
25
|
+
# always true.
|
26
|
+
def without_urls(string)
|
27
|
+
string.gsub(/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)\S+(?:[^\s`!\[\]{};:'".,?«»“”‘’]))/i, "")
|
28
|
+
end
|
29
|
+
|
30
|
+
# Avoid dots used to trigger mentions
|
31
|
+
def without_leading_dot(string)
|
32
|
+
string.gsub(/^\.\@/, "@")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'markovian/utils/text_splitter'
|
2
|
+
|
3
|
+
# This class, given a seed word and a Markov chain_set, will attempt to construct a new text using
|
4
|
+
# the Markov associations.
|
5
|
+
module Markovian
|
6
|
+
class TextBuilder
|
7
|
+
attr_reader :seed_text, :chain_set
|
8
|
+
def initialize(seed_text, chain_set)
|
9
|
+
@seed_text = seed_text
|
10
|
+
@chain_set = chain_set
|
11
|
+
end
|
12
|
+
|
13
|
+
def construct(length: 140, seed: default_seed, start_result_with_seed_word: false)
|
14
|
+
# TODO: if we don't hit a result for the first pair, move backward through the original text
|
15
|
+
# until we get something
|
16
|
+
result_with_next_word(
|
17
|
+
previous_pair: seed,
|
18
|
+
result: start_result_with_seed_word ? format_result_array(seed) : nil,
|
19
|
+
length: length
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
def default_seed
|
24
|
+
if split_seed_text.length >= 2
|
25
|
+
split_seed_text[-2..-1]
|
26
|
+
else
|
27
|
+
# if we only have a one-word seed text, the previous word is nil
|
28
|
+
[nil, split_seed_text.first]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def result_with_next_word(previous_pair:, result:, length:)
|
35
|
+
previous_word, current_word = previous_pair
|
36
|
+
if next_word = chain_set.next_word(current_word, previous_word: previous_word)
|
37
|
+
# we use join rather than + to avoid leading spaces, and strip to ignore leading nils or
|
38
|
+
# empty strings
|
39
|
+
interim_result = format_result_array([result, next_word])
|
40
|
+
if interim_result.length > length
|
41
|
+
result
|
42
|
+
else
|
43
|
+
result_with_next_word(
|
44
|
+
previous_pair: [current_word, next_word],
|
45
|
+
result: interim_result,
|
46
|
+
length: length
|
47
|
+
)
|
48
|
+
end
|
49
|
+
else
|
50
|
+
result
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Turn an array of words into an ongoing string
|
55
|
+
def format_result_array(array_of_words)
|
56
|
+
array_of_words.compact.map(&:strip).join(" ")
|
57
|
+
end
|
58
|
+
|
59
|
+
def split_seed_text
|
60
|
+
@split_seed_text ||= Utils::TextSplitter.new(seed_text).components
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Markovian
|
2
|
+
module Utils
|
3
|
+
class TextSplitter
|
4
|
+
attr_reader :text
|
5
|
+
def initialize(text)
|
6
|
+
@text = text
|
7
|
+
end
|
8
|
+
|
9
|
+
# We split on spaces, quotes, (various symbols followed by either another dash, a space,
|
10
|
+
# another dot, or the end of the text), or (colons preceded by space or the beginning of the
|
11
|
+
# text).
|
12
|
+
# We don't want to block things like Jones-Smith, tl;dr, abc.def, or it's.
|
13
|
+
# Any of the following:
|
14
|
+
# [\s\(\)] - a space or parentheses on their own
|
15
|
+
# " - a quote on its own
|
16
|
+
# [\.-:;\?\!]([-\.\s]|$) - a period, dash, ?, or ! followed by a space, period, dash, or the
|
17
|
+
# end of the word
|
18
|
+
# [\s^]' - a single ' following a non-letter
|
19
|
+
WORD_DELIMITERS = /([\s\(\)]|"|[\.\-:;\?\!]([\-\.\s]|$)|[\s^]')/
|
20
|
+
|
21
|
+
# anything that doesn't contain any letters is not a word we need to care about
|
22
|
+
MARKERS_OF_INTEREST = /[A-Za-z]/
|
23
|
+
|
24
|
+
def components
|
25
|
+
split_text.select {|t| t.match(MARKERS_OF_INTEREST)}
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def split_text
|
31
|
+
text.downcase.split(WORD_DELIMITERS)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/markovian.rb
ADDED
data/markovian.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'markovian/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "markovian"
|
8
|
+
spec.version = Markovian::VERSION
|
9
|
+
spec.authors = ["Alex Koppel"]
|
10
|
+
spec.email = ["alex@alexkoppel.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple, hopefully easy-to-use Markov chain generator.}
|
13
|
+
spec.description = %q{A simple, hopefully easy-to-use Markov chain generator.}
|
14
|
+
spec.homepage = "https://github.com/arsduo/markov-ahkoppel"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency "oj"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: markovian
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Koppel
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: oj
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: A simple, hopefully easy-to-use Markov chain generator.
|
56
|
+
email:
|
57
|
+
- alex@alexkoppel.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".ruby-gemset"
|
65
|
+
- ".ruby-version"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- changelog.md
|
74
|
+
- db/seeds.rb
|
75
|
+
- lib/.DS_Store
|
76
|
+
- lib/markovian.rb
|
77
|
+
- lib/markovian/chain.rb
|
78
|
+
- lib/markovian/chain/dictionary.rb
|
79
|
+
- lib/markovian/chain/text_compiler.rb
|
80
|
+
- lib/markovian/chain_set.rb
|
81
|
+
- lib/markovian/importers/twitter/csv_importer.rb
|
82
|
+
- lib/markovian/importers/twitter/tweet.rb
|
83
|
+
- lib/markovian/text_builder.rb
|
84
|
+
- lib/markovian/utils/text_splitter.rb
|
85
|
+
- lib/markovian/version.rb
|
86
|
+
- markovian.gemspec
|
87
|
+
homepage: https://github.com/arsduo/markov-ahkoppel
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.4.6
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: A simple, hopefully easy-to-use Markov chain generator.
|
111
|
+
test_files: []
|