gloignon commited on
Commit
39cd34f
·
verified ·
1 Parent(s): 263e305

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +57 -49
app.R CHANGED
@@ -1,58 +1,66 @@
1
  library(shiny)
2
- library(bslib)
3
- library(dplyr)
4
- library(ggplot2)
5
 
6
- df <- readr::read_csv("penguins.csv")
7
- # Find subset of columns that are suitable for scatter plot
8
- df_num <- df |> select(where(is.numeric), -Year)
9
 
10
- ui <- page_sidebar(
11
- theme = bs_theme(bootswatch = "minty"),
12
- title = "Penguins explorer",
13
- sidebar = sidebar(
14
- varSelectInput("xvar", "X variable", df_num, selected = "Bill Length (mm)"),
15
- varSelectInput("yvar", "Y variable", df_num, selected = "Bill Depth (mm)"),
16
- checkboxGroupInput("species", "Filter by species",
17
- choices = unique(df$Species), selected = unique(df$Species)
18
  ),
19
- hr(), # Add a horizontal rule
20
- checkboxInput("by_species", "Show species", TRUE),
21
- checkboxInput("show_margins", "Show marginal plots", TRUE),
22
- checkboxInput("smooth", "Add smoother"),
23
- ),
24
- plotOutput("scatter")
25
  )
26
 
27
- server <- function(input, output, session) {
28
- subsetted <- reactive({
29
- req(input$species)
30
- df |> filter(Species %in% input$species)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  })
32
-
33
- output$scatter <- renderPlot(
34
- {
35
- p <- ggplot(subsetted(), aes(!!input$xvar, !!input$yvar)) +
36
- theme_light() +
37
- list(
38
- theme(legend.position = "bottom"),
39
- if (input$by_species) aes(color = Species),
40
- geom_point(),
41
- if (input$smooth) geom_smooth()
42
- )
43
-
44
- if (input$show_margins) {
45
- margin_type <- if (input$by_species) "density" else "histogram"
46
- p <- p |> ggExtra::ggMarginal(
47
- type = margin_type, margins = "both",
48
- size = 8, groupColour = input$by_species, groupFill = input$by_species
49
- )
50
- }
51
-
52
- p
53
- },
54
- res = 100
55
- )
56
  }
57
 
58
- shinyApp(ui, server)
 
 
1
  library(shiny)
2
+ library(udpipe)
 
 
3
 
4
+ # Load the French bsd model (ensure it's downloaded and adjust path if necessary)
5
+ model <- udpipe_load_model("french-gsd-ud-2.5-191206.udpipe")
 
6
 
7
+ # Define UI for the application
8
+ ui <- fluidPage(
9
+ titlePanel("French Readability Analyzer with UDPipe"),
10
+
11
+ sidebarLayout(
12
+ sidebarPanel(
13
+ textInput("text", "Enter French text:", value = "", placeholder = "Type or paste French text here"),
14
+ actionButton("analyze", "Analyze")
15
  ),
16
+
17
+ mainPanel(
18
+ h3("Readability Features"),
19
+ tableOutput("results")
20
+ )
21
+ )
22
  )
23
 
24
+ # Define server logic
25
+ server <- function(input, output) {
26
+
27
+ analyze_text <- eventReactive(input$analyze, {
28
+ text <- input$text
29
+
30
+ # Annotate text using udpipe with the French model
31
+ annotated <- udpipe_annotate(model, x = text)
32
+ annotated_df <- as.data.frame(annotated)
33
+
34
+ # Calculate readability metrics
35
+ word_count <- nrow(annotated_df[annotated_df$upos == "NOUN" | annotated_df$upos == "VERB" |
36
+ annotated_df$upos == "ADJ" | annotated_df$upos == "ADV", ])
37
+
38
+ sentence_count <- length(unique(annotated_df$sentence_id))
39
+
40
+ # Syllable count - count vowels in each token as an approximation
41
+ syllable_count <- sum(sapply(gregexpr("[aeiouyAEIOUY]", annotated_df$token), function(x) max(0, length(x))))
42
+
43
+ # Average sentence length (words per sentence)
44
+ avg_sentence_length <- ifelse(sentence_count > 0, word_count / sentence_count, 0)
45
+
46
+ # Average syllables per word
47
+ avg_syllables_per_word <- ifelse(word_count > 0, syllable_count / word_count, 0)
48
+
49
+ # Compile results into a data frame
50
+ results <- data.frame(
51
+ Metric = c("Word Count", "Sentence Count", "Syllable Count",
52
+ "Average Sentence Length", "Average Syllables per Word"),
53
+ Value = c(word_count, sentence_count, syllable_count,
54
+ round(avg_sentence_length, 2), round(avg_syllables_per_word, 2))
55
+ )
56
+
57
+ return(results)
58
+ })
59
+
60
+ output$results <- renderTable({
61
+ analyze_text()
62
  })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  }
64
 
65
+ # Run the application
66
+ shinyApp(ui = ui, server = server)