Note that there are some explanatory texts on larger screens.

plurals
  1. POSmartly Tracking Words Written in a Rails App
    primarykey
    data
    text
    <h2>The problem</h2> <p>I'm working on a creative writing application in Rails 4 and users have requested a feature that keeps them accountable to writing X words every day/week/month. What are the best ways to handle the problem of tracking words added over time?</p> <h2>My current solution</h2> <p>I store a limited history of total words for each user, allowing me to compare total words in all of their chapters today to the total words in all of their chapters yesterday, last week, or last month.</p> <h2>Edge cases I'm not handling (and am not sure how to handle)</h2> <p>What if a user deletes a large portion of a chapter and rewrites it or deletes an entire chapter or story? I don't want to penalize them for throwing out what they've previously written.</p> <h2>EDIT:</h2> <p>I've just modified <a href="http://en.wikipedia.org/wiki/Levenshtein_distance" rel="nofollow">the Levenshtein algorithm</a> to count all words added, removed, or substituted, to give writers credit towards their writing goals for all those activities. You can see the code here:</p> <pre><code>def words_changed_since(second) first = self.split second = second.split matrix = [(0..first.length).to_a] (1..second.length).each do |j| matrix &lt;&lt; [j] + [0] * (first.length) end (1..second.length).each do |i| (1..first.length).each do |j| if first[j-1] == second[i-1] matrix[i][j] = matrix[i-1][j-1] else matrix[i][j] = [ matrix[i-1][j], matrix[i][j-1], matrix[i-1][j-1], ].min + 1 end end end return matrix.last.last end </code></pre> <p>That's monkey-patched onto the String class in an initializer so that I can call <code>new_chapter_content.words_changed_since old_chapter_content</code> and it will only give me a positive number. I'm open to feedback on that algorithm, but I'm pretty happy with it for now. My biggest problems are now:</p> <ol> <li><strong>Should I just store this in my postgres db, or should I use another store like redis?</strong></li> <li><strong>Would it be a Very Bad Idea to <em>not</em> expire daily words and even track more frequently than daily, like every hour that the user writes? This would allow me to give writers a very granular history of their writing and also help them keep track of when they're most productive.</strong></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload