lib/twitter_ebooks/suffix.rb
def generate(passes=5, n=:unigrams)
      index = rand(@sentences.length)
      tikis = @sentences[index]
      used = [index] # Sentences we've already used
      verbatim = [tikis] # Verbatim sentences to avoid reproducing

      0.upto(passes-1) do
        varsites = {} # Map bigram start site => next tiki alternatives

        tikis.each_with_index do |tiki, i|
          next_tiki = tikis[i+1]
          break if next_tiki.nil?

          alternatives = (n == :unigrams) ? @unigrams[next_tiki] : @bigrams[tiki][next_tiki]
          # Filter out suffixes from previous sentences
          alternatives.reject! { |a| a[1] == INTERIM || used.include?(a[0]) }
          varsites[i] = alternatives unless alternatives.empty?
        end

        variant = nil
        varsites.to_a.shuffle.each do |site|
          start = site[0]

          site[1].shuffle.each do |alt|
            start, alt = site[0], site[1].sample
            verbatim << @sentences[alt[0]]
            suffix = @sentences[alt[0]][alt[1]..-1]
            potential = tikis[0..start+1] + suffix

            # Ensure we're not just rebuilding some segment of another sentence
            unless verbatim.find { |v| NLP.subseq?(v, potential) || NLP.subseq?(potential, v) }
              used << alt[0]
              variant = potential
              break
            end
          end

          break if variant
        end

        tikis = variant if variant
      end

      tikis
    end
# Generate a recombined sequence of tikis
# @param passes [Integer] number of times to recombine
# @param n [Symbol] :unigrams or :bigrams (affects how conservative the model is)
# @return [Array<Integer>]
Generate a recombined sequence of tikis
Generate a recombined sequence of tikis
def generate(passes=5, n=:unigrams)
      index = rand(@sentences.length)
      tikis = @sentences[index]
      used = [index] 
      verbatim = [tikis] 
      0.upto(passes-1) do
        varsites = {} 
        tikis.each_with_index do |tiki, i|
          next_tiki = tikis[i+1]
          break if next_tiki.nil?
          alternatives = (n == :unigrams) ? @unigrams[next_tiki] : @bigrams[tiki][next_tiki]
          alternatives.reject! { |a| a[1] == INTERIM || used.include?(a[0]) }
          varsites[i] = alternatives unless alternatives.empty?
        end
        variant = nil
        varsites.to_a.shuffle.each do |site|
          start = site[0]
          site[1].shuffle.each do |alt|
            start, alt = site[0], site[1].sample
            verbatim << @sentences[alt[0]]
            suffix = @sentences[alt[0]][alt[1]..-1]
            potential = tikis[0..start+1] + suffix
            unless verbatim.find { |v| NLP.subseq?(v, potential) || NLP.subseq?(potential, v) }
              used << alt[0]
              variant = potential
              break
            end
          end
          break if variant
        end
        tikis = variant if variant
      end
      tikis
    end
def generate ( passes = 5 , n = :unigrams ) index = rand ( @sentences . length ) tikis = @sentences [ index ] used = [ index ] verbatim = [ tikis ] 0 . upto ( passes - 1 ) do varsites = { } tikis . each_with_index do | tiki , i | next_tiki = tikis [ i + 1 ] break if next_tiki . nil? alternatives = ( n == :unigrams ) ? @unigrams [ next_tiki ] : @bigrams [ tiki ] [ next_tiki ] alternatives . reject! { | a | a [ 1 ] == INTERIM || used . include? ( a [ 0 ] ) } varsites [ i ] = alternatives unless alternatives . empty? end variant = nil varsites . to_a . shuffle . each do | site | start = site [ 0 ] site [ 1 ] . shuffle . each do | alt | start , alt = site [ 0 ] , site [ 1 ] . sample verbatim << @sentences [ alt [ 0 ] ] suffix = @sentences [ alt [ 0 ] ] [ alt [ 1 ] .. - 1 ] potential = tikis [ 0 .. start + 1 ] + suffix unless verbatim . find { | v | NLP . subseq? ( v , potential ) || NLP . subseq? ( potential , v ) } used << alt [ 0 ] variant = potential break end end break if variant end tikis = variant if variant end tikis end
Generate a recombined sequence of tikis
Generate a recombined sequence of tikis
# Sentences we've already used # Verbatim sentences to avoid reproducing # Map bigram start site => next tiki alternatives # Filter out suffixes from previous sentences # Ensure we're not just rebuilding some segment of another sentence
None
Array<Integer>
passes
number of times to recombine
number of times to recombine
n
:unigrams or :bigrams (affects how conservative the model is)
: unigrams or : bigrams ( affects how conservative the model is )