Skip to content

Commit

Permalink
feat: planet name generator
Browse files Browse the repository at this point in the history
  • Loading branch information
hrbrmstr committed Apr 12, 2023
1 parent ba0e62a commit 8726329
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 13 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ Suggests:
Depends:
R (>= 3.6.0),
Imports:
httr,
jsonlite,
ggplot2,
scales
scales,
stringi
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Generated by roxygen2: do not edit by hand

export(generate_orbits)
export(generate_random_planets)
export(plot_orbits)
export(randomize_planet_positions)
export(sol_planets)
export(theme_enhance_solar)
import(ggplot2)
import(httr)
import(scales)
importFrom(jsonlite,fromJSON)
import(stringi)
39 changes: 39 additions & 0 deletions R/aaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,42 @@
#' @docType data
#' @export
sol_planets <- c("Mercury", "Venus", "Earth", "Mars", "Jupyter", "Saturn", "Uranus", "Neptune", "Pluto")




c("cen-tau-ri", "pro-xi-ma", "al-pha", "pro-xi-mus", "ter-ra",
"sa-turn", "ju-pi-ter", "mer-cu-ry", "ve-nus", "u-ra-nus", "mars",
"nep-tune", "plu-to", "ni-bi-ru", "tra-pist", "zeus", "aph-ro-dite",
"a-res", "heph-aes-tus", "ty-phoon", "da-scher", "en-dor", "ka-mi-no",
"qua-sar", "no-va", "om-ni-sen", "dune", "a-ra-kis", "gla-tius",
"co-do-mi-ni-um", "dor-sai", "o-si-mio", "he-li-os", "leo", "hein-lein",
"a-ca-mar", "ach-ra-dy", "a-di-ge-on", "a-las-tria", "al-drea",
"von-tria", "al-di-bran", "al-ge-ron", "ach-eon", "a-lon-dra",
"cyg-nus", "a-ri-done", "a-ri-da-ni", "lep-tus", "am-leth", "am-di-vian",
"om-ri-con", "ma-jo-ris", "mi-no-ris", "leo-nis", "an-do-ria",
"an-go-sia", "ar-ge-lius", "ar-gus", "per-cii", "pro-me-thi-us",
"the-os", "ar-ret", "ar-va-da", "au-re-lia", "ba-lon-si", "bar-son",
"ba-ri-di-on", "ben-zar", "ben-thos", "kup-sic", "bo-kara", "bo-la-rus",
"bo-ra-dis", "bos-lic", "bren-ta-lia", "bri-toid", "he-lia",
"cal-do-nia", "cal-dos", "cal-leb", "ca-li-os", "ca-mus", "ca-no-pius",
"car-da-sia", "cas-tal", "ca-tu-lia", "ce-ti", "cor-van", "co-ri-dan",
"cyg-nia", "cyg-net", "da-ka-ra", "da-lan", "da-nu-la", "oxi-lia",
"dav-los", "de-cos", "de-des-tris", "cen-tris", "cat-los", "der-bia",
"der-mi-nia", "de-ni-us", "nig-mus", "stag-ni-us", "spho-li-us",
"shpo-rio", "dre-on", "val-les", "al-pes", "al-pus", "xar-ria",
"cor-do-via", "met-ne-ria", "cos-ma-dov", "ju-ne-ria", "del-ta",
"o-rio-na", "ci-ca-da", "am-ne-li-um", "noe-li-um", "au-ra",
"au-ro-ra", "em-bo-ria", "phan-tax", "ob-lox", "ob-li-via", "za-thu-ra",
"ar-chea", "cen-tus", "ep-si-lon", "au-pho-ria", "au-ro-ria",
"ma-ri-dia", "po-tel-mia", "hy-pho-ria", "ti-tan", "la-vi-tan",
"no-mi-cron", "mag-nus", "kep-lar", "del-phi", "nim-bus", "am-phus",
"mor-phus", "lamb-dus", "dus-to-pia", "mo-pi-um", "mo-li-um",
"pho-ri-um", "pho-ri-oso", "oso-li-um", "xe-li-um", "so-li-sia",
"so-lus", "ap-po-lus", "pho-bos", "chro-nos", "as-tos", "seg-mos",
"co-los", "dei-mos", "cog-nus", "sig-mus", "co-ro-li-os", "pe-ri-li-os",
"sep-tus", "mes-mus", "op-tus", "ne-bu-lus", "hec-tus", "me-gus",
"gi-gus", "te-rus", "pe-tus", "lu-mus", "pri-mus", "op-ti-mus",
"ori-gus", "xe-lo-phus", "meg-lo-vus", "al-pha-ni-us", "es-la-var",
"cel-les-ti-us", "stel-le-ra-sus", "ra-le-os", "pa-go-ya", "pul-sar"
) -> planet_fragments
117 changes: 117 additions & 0 deletions R/generate-random-planets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#' Generate a vector of `n` believable planetary names using hidden Markov model.
#'
#' \if{html}{
#' \out{<div style="text-align: center">}\figure{README-rando-1.png}{options: style="width:750px;max-width:75\%;"}\out{</div>}
#' }
#' \if{latex}{
#' \out{\begin{center}}\figure{README-rando-1.png}\out{\end{center}}
#' }
#'
#' @param num_planets how many planets?
#' @return character vector of planet names
#' @references Riffed from [planet-name-generator](https://github.com/sayamqazi/planet-name-generator).
#' @export
#' @examples
#' generate_random_planets(15)
#'
#' set.seed(42)
#' rando_planets <- generate_random_planets(12)
#'
#' rando_orbits <- generate_orbits(rando_planets)
#'
#' set.seed(123) # this produced a decent placements
#'
#' placed_planets <- randomize_planet_positions(rando_orbits)
#'
#' plot_orbits(
#' orbits = rando_orbits,
#' planet_positions = placed_planets,
#' label_planets = TRUE,
#' label_family = hrbrthemes::font_es_bold
#' ) +
#' hrbrthemes::theme_ipsum_es(grid="") +
#' coord_equal() +
#' labs(
#' title = "Rando System"
#' ) +
#' theme_enhance_solar()

generate_random_planets <- function(num_planets = 20) {

planets <- planet_fragments

syllables <- stringi::stri_split_fixed(planets, "-", simplify = TRUE)

total_syllables <- length(syllables)

syllables <- unique(syllables)

div_index <- length(syllables) / total_syllables

size <- length(syllables) + 1

freq <- matrix(0, nrow = size, ncol = size)

for (p in planets) {
lex <- stringi::stri_split_fixed(p, "-", simplify = TRUE)
lex_indices <- match(lex, syllables) - 1

if (length(lex_indices) > 1) {
row_indices <- lex_indices[-length(lex_indices)]
col_indices <- lex_indices[-1]
freq[cbind(row_indices, col_indices)] <- freq[cbind(row_indices, col_indices)] + 1
}

last_syllable_index <- lex_indices[length(lex_indices)]
freq[last_syllable_index, size - 1] <- freq[last_syllable_index, size - 1] + 1
}

max_names <- num_planets
num_names <- 0
planet_name <- ""

c("prime", "",
"B", "",
"alpha", "",
'proxima', "",
"IV", "",
"V", "",
"C", "",
"VI", "",
"VII", "",
"VIII", "",
"X", "",
"IX", "",
"D", "",
"", "") -> suffixes

generated_planet_names <- c()

while (num_names < max_names) {

length <- sample(2:3, 1)
initial <- sample(0:(size - 2), 1)

planet_name <- ""
while (length > 0) {
while (all(freq[initial + 1, ] != 1)) {
initial <- sample(0:(size - 2), 1)
}

planet_name <- paste0(planet_name, syllables[initial + 1])
initial <- which(freq[initial + 1, ] == 1)[1] - 1
length <- length - 1
}

suffix_index <- sample(1:length(suffixes), 1)
planet_name <- paste(planet_name, suffixes[suffix_index])
generated_planet_names <- append(generated_planet_names, planet_name)

num_names <- num_names + 1
}

stringi::stri_trans_totitle(
stringi::stri_trim_both(generated_planet_names)
)

}
3 changes: 1 addition & 2 deletions R/ggsolar-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
#' @name ggsolar
#' @keywords internal
#' @author Bob Rudis (bob@@rud.is)
#' @import httr ggplot2 scales
#' @importFrom jsonlite fromJSON
#' @import ggplot2 scales stringi
"_PACKAGE"
28 changes: 27 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ packageVersion("ggsolar")
```{r sol, fig.width=6, fig.height=6, dev.args=list(bg="transparent")}
sol_orbits <- generate_orbits(sol_planets)
set.seed(1323) # this produced a decent placements
set.seed(1323) # this produced decent placements
placed_planets <- randomize_planet_positions(sol_orbits)
Expand All @@ -64,6 +64,32 @@ plot_orbits(
theme_enhance_solar()
```

## Rando!

```{r rando, fig.width=6, fig.height=6, dev.args=list(bg="transparent")}
set.seed(42)
(rando_planets <- generate_random_planets(12))
rando_orbits <- generate_orbits(rando_planets)
set.seed(123) # this produced decent placements
placed_planets <- randomize_planet_positions(rando_orbits)
plot_orbits(
orbits = rando_orbits,
planet_positions = placed_planets,
label_planets = TRUE,
label_family = hrbrthemes::font_es_bold
) +
hrbrthemes::theme_ipsum_es(grid="") +
coord_equal() +
labs(
title = "Rando System"
) +
theme_enhance_solar()
```

## ggsolar Metrics

```{r cloc, echo=FALSE}
Expand Down
43 changes: 38 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package.
The following functions are implemented:

- `generate_orbits`: Generate concentric circular orbits for “n” planets
- `generate_random_planets`: Generate a vector of n believable planetary
names using hidden Markov model.
- `plot_orbits`: Return a skeleton of a ggplot2 plot of solar system
plot
- `randomize_planet_positions`: Generate random planetary positions
Expand All @@ -34,6 +36,7 @@ The following functions are implemented:
## Installation

``` r
remotes::install_github("hrbrmstr/ggsolar")
```

NOTE: To use the ‘remotes’ install options you will need to have the
Expand All @@ -55,7 +58,7 @@ packageVersion("ggsolar")
``` r
sol_orbits <- generate_orbits(sol_planets)

set.seed(1323) # this produced a decent placements
set.seed(1323) # this produced decent placements

placed_planets <- randomize_planet_positions(sol_orbits)

Expand All @@ -76,14 +79,44 @@ plot_orbits(

<img src="man/figures/README-sol-1.png" width="576" />

## Rando!

``` r
set.seed(42)
(rando_planets <- generate_random_planets(12))
## [1] "Ososi" "Morlambtune" "Obli Vi" "Lantitan" "Tanthos" "Onhy" "Lesbomor C" "Radido"
## [9] "Lesstel" "Altan Prime" "Lasdre" "Brenta"

rando_orbits <- generate_orbits(rando_planets)

set.seed(123) # this produced decent placements

placed_planets <- randomize_planet_positions(rando_orbits)

plot_orbits(
orbits = rando_orbits,
planet_positions = placed_planets,
label_planets = TRUE,
label_family = hrbrthemes::font_es_bold
) +
hrbrthemes::theme_ipsum_es(grid="") +
coord_equal() +
labs(
title = "Rando System"
) +
theme_enhance_solar()
```

<img src="man/figures/README-rando-1.png" width="576" />

## ggsolar Metrics

| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) |
|:-----|---------:|-----:|----:|-----:|------------:|-----:|---------:|-----:|
| R | 7 | 0.35 | 98 | 0.30 | 35 | 0.27 | 97 | 0.37 |
| YAML | 2 | 0.10 | 38 | 0.12 | 10 | 0.08 | 2 | 0.01 |
| Rmd | 1 | 0.05 | 25 | 0.08 | 20 | 0.15 | 31 | 0.12 |
| SUM | 10 | 0.50 | 161 | 0.50 | 65 | 0.50 | 130 | 0.50 |
| R | 8 | 0.36 | 191 | 0.35 | 61 | 0.31 | 141 | 0.40 |
| Rmd | 1 | 0.05 | 42 | 0.08 | 26 | 0.13 | 34 | 0.10 |
| YAML | 2 | 0.09 | 38 | 0.07 | 10 | 0.05 | 2 | 0.01 |
| SUM | 11 | 0.50 | 271 | 0.50 | 97 | 0.50 | 177 | 0.50 |

clock Package Metrics for ggsolar

Expand Down
Loading

0 comments on commit 8726329

Please sign in to comment.