File: upFirst.s

package info (click to toggle)
hmisc 5.2-4-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,044 kB
  • sloc: asm: 28,905; f90: 590; ansic: 415; xml: 160; fortran: 75; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 1,799 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
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
upFirst <- function(txt, lower=FALSE, alllower=FALSE) {
  f <- function(x) {
  notcap <- c('a', 'about', 'above', 'across', 'after', 'against',
                'along', 'among', 'an', 'and', 'around', 'as', 'at',
                'before', 'behind', 'below', 'beneath', 'beside',
                'besides', 'between', 'beyond', 'but', 'by', 'despite',
                'down', 'during', 'except', 'following', 'for', 'from',
                'in', 'inside', 'into', 'like', 'mid', 'minus', 'near',
                'next', 'nor', 'of', 'off', 'on', 'onto', 'opposite',
                'or', 'out', 'outside', 'over', 'past', 'per', 'plus',
                'regarding', 'round', 'save', 'since', 'so', 'than',
                'the', 'through', 'throughout', 'till', 'times',
                'to', 'toward', 'towards', 'under', 'underneath',
                'unlike', 'until', 'up', 'upon', 'via', 'vs.', 'when',
                'with', 'within', 'without', 'worth', 'yet')
  s <- strsplit(x, " ")[[1]]
  ## Find words that have more than one upper case letter; assume these
  ## are acronyms that need capitalization preserved
  a <- grepl('[A-Z]{1,}.*[A-Z]{1,}', s)
  s <- if(alllower)
         ifelse(a, s, tolower(s))
  else if(lower)
         ifelse(a, s, ifelse((1 : length(s)) == 1,
                             paste(toupper(substring(s, 1, 1)),
                                   tolower(substring(s, 2)), sep=''),
                             tolower(s)))
       else
         ifelse(a, s, ifelse((1 : length(s)) == 1 | s %nin% notcap,
                             paste(toupper(substring(s, 1, 1)),
                                   tolower(substring(s, 2)), sep=''),
                             tolower(s)))
  paste(s, collapse=' ')
}
  for(i in 1 : length(txt)) txt[i] <- f(txt[i])
  txt
}