caesar

Author

Ксения Войтова

knitr::opts_chunk$set(message = FALSE, warning = FALSE)
library(tidyverse)
library(udpipe)
library(igraph)
library(ggraph)
library(visNetwork)
caesar <- udpipe_read_conllu("https://github.com/locusclassicus/text_analysis_2024/raw/main/files/bg_latinpipe.conllu")

caesar_nouns <- caesar |> filter(upos == "NOUN")

cooc <- cooccurrence(
  caesar_nouns,
  term = "lemma",
  group = c("doc_id", "sentence_id")
) |>
  as_tibble() |>
  filter(cooc > 25)

g <- graph_from_data_frame(cooc, directed = FALSE)
V(g)$degree <- degree(g)
V(g)$community <- membership(cluster_louvain(g))

data <- toVisNetworkData(g)
colors <- RColorBrewer::brewer.pal(8, "Set2")
data$nodes$color <- colors[V(g)$community %% 8 + 1]
data$nodes$size <- degree(g) * 3

visNetwork(
  nodes = data$nodes,
  edges = data$edges,
  width = "100%",
  height = "700px",
  main = "Лексические коллокации в Записках о Галльской войне Цезаря"
) |>
  visOptions(
    highlightNearest = list(enabled = TRUE, degree = 1, hover = TRUE),
    nodesIdSelection = TRUE
  ) |>
  visPhysics(maxVelocity = 20, stabilization = FALSE) |>
  visInteraction(dragNodes = TRUE, zoomView = TRUE)