class BibTeX::Entry

Public Instance Methods

merge_interactively(other_entries, required_fields) click to toggle source
# File lib/bibtex_cleaner.rb, line 34
def merge_interactively(other_entries, required_fields)
  new_entry = {
    bibtex_type: multiple_choice(:type, [self] + other_entries),
    key: self.key
  }
  fields = required_fields[new_entry[:bibtex_type]]
  if fields.nil?
    self.key = "XXX_NOT_CLEANED_#{self.key}"
    fields = []
  end

  unless fields.empty?
    other_entries = other_entries.select do |b|
      b.type == new_entry[:bibtex_type]
    end
    new_entry[:title] = multiple_choice(:title, [self] + other_entries)
    other_entries = other_entries.select do |b|
      Levenshtein.normalized_distance(b.title.to_s, new_entry[:title].to_s) < 0.5
    end
    fields.each do |f|
      new_entry[f] = multiple_choice(f, [self] + other_entries)
    end
    BibTeX::Entry.new(new_entry)
  else
    self
  end
end
multiple_choice(f, bibtexen) click to toggle source
# File lib/bibtex_cleaner.rb, line 11
def multiple_choice(f, bibtexen)
  choices = bibtexen.map { |b| [(b.send(f) if b.respond_to?(f)), b.title, b.author] }
  choices = choices.reject { |c| "#{c.first}".empty? }.uniq { |c| "#{c.first}" }

  return choices.first.first if choices.size == 1
  return "" if choices.size == 0

  puts "Choose #{f}! (or enter your own, 0 is the original or suggested entry)"
  choices.each_with_index do |choice, idx|
    puts "[#{idx}] \"#{choice.first}\"\n\t\t\t\t(#{choice[1]}, #{choice[2]})"
  end
  answer = Readline.readline.strip
  result = if answer =~ /\d+/
             choices[answer.to_i].first
           elsif answer.empty?
             choices[0].first
           else
             answer
           end
  puts result
  result
end