|
library(shiny) |
|
library(udpipe) |
|
|
|
|
|
model <- udpipe_load_model("french-gsd-ud-2.5-191206.udpipe") |
|
|
|
|
|
ui <- fluidPage( |
|
titlePanel("French Readability Analyzer with UDPipe"), |
|
|
|
sidebarLayout( |
|
sidebarPanel( |
|
textInput("text", "Enter French text:", value = "", placeholder = "Type or paste French text here"), |
|
actionButton("analyze", "Analyze") |
|
), |
|
|
|
mainPanel( |
|
h3("Readability Features"), |
|
tableOutput("results") |
|
) |
|
) |
|
) |
|
|
|
|
|
server <- function(input, output) { |
|
|
|
analyze_text <- eventReactive(input$analyze, { |
|
text <- input$text |
|
|
|
|
|
annotated <- udpipe_annotate(model, x = text) |
|
annotated_df <- as.data.frame(annotated) |
|
|
|
|
|
word_count <- nrow(annotated_df[annotated_df$upos == "NOUN" | annotated_df$upos == "VERB" | |
|
annotated_df$upos == "ADJ" | annotated_df$upos == "ADV", ]) |
|
|
|
sentence_count <- length(unique(annotated_df$sentence_id)) |
|
|
|
|
|
syllable_count <- sum(sapply(gregexpr("[aeiouyAEIOUY]", annotated_df$token), function(x) max(0, length(x)))) |
|
|
|
|
|
avg_sentence_length <- ifelse(sentence_count > 0, word_count / sentence_count, 0) |
|
|
|
|
|
avg_syllables_per_word <- ifelse(word_count > 0, syllable_count / word_count, 0) |
|
|
|
|
|
results <- data.frame( |
|
Metric = c("Word Count", "Sentence Count", "Syllable Count", |
|
"Average Sentence Length", "Average Syllables per Word"), |
|
Value = c(word_count, sentence_count, syllable_count, |
|
round(avg_sentence_length, 2), round(avg_syllables_per_word, 2)) |
|
) |
|
|
|
return(results) |
|
}) |
|
|
|
output$results <- renderTable({ |
|
analyze_text() |
|
}) |
|
} |
|
|
|
|
|
shinyApp(ui = ui, server = server) |