Class: EBSCO::EDS::Titleize

Inherits:
Object
  • Object
show all
Defined in:
lib/ebsco/eds/facet_titleize.rb

Constant Summary collapse

SMALL_WORDS =
%w{a an and as at but by en for if in of on or the to v v. via vs vs.}
ACRONYMS =
%w{u.s. usa npr ieee llc g.p.o. n.y. n.c. b.v.}

Instance Method Summary collapse

Instance Method Details

#phrases(title) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/ebsco/eds/facet_titleize.rb', line 58

def phrases(title)
  phrases = [[]]
  title.split.each do |word|
    phrases.last << word
    phrases << [] if ends_with_punctuation?(word) && !small_word?(word)
  end
  phrases.reject(&:empty?).map { |phrase| phrase.join ' ' }
end

#titleize(title, opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ebsco/eds/facet_titleize.rb', line 8

def titleize(title, opts={})
  title = title.dup
  title.downcase! unless title[/[[:lower:]]/]  # assume all-caps need fixing

  small_words = SMALL_WORDS + (opts[:small_words] || [])
  small_words = small_words + small_words.map { |small| small.capitalize }

  acronyms = ACRONYMS + (opts[:acronyms] || [])
  acronyms = acronyms + acronyms.map { |acronym| acronym.downcase }

  phrases(title).map do |phrase|
    words = phrase.split
    words.map.with_index do |word, index|

      if acronyms.include?(word.gsub(/[()]/,''))
        word.upcase
      else

        def word.capitalize
          # like String#capitalize, but it starts with the first letter
          self.sub(/[[:alpha:]].*/) {|subword| subword.capitalize}
        end

        case word
          when /[[:alpha:]]\.[[:alpha:]]/  # words with dots in, like "example.com"
            word
          when /[-‑]/  # hyphenated word (regular and non-breaking)
            word.split(/([-‑])/).map do |part|
              SMALL_WORDS.include?(part) ? part : part.capitalize
            end.join
          when /^[[:alpha:]].*[[:upper:]]/ # non-first letter capitalized already
            word
          when /^[[:digit:]]/  # first character is a number
            word
          when *small_words
            if index == 0 || index == words.size - 1
              word.capitalize
            else
              word.downcase
            end
          else
            word.capitalize
        end

      end

    end.join(' ')
  end.join(' ')
end