59doit

의미망 분석 #2 동시 출현 네트워크 (1) 본문

텍스트마이닝

의미망 분석 #2 동시 출현 네트워크 (1)

yul_S2 2022. 12. 25. 21:30
반응형

(2) 동시 출현 네트워크 

  • 동시 출현 빈도를 이용해 단어의 관계를 네트워크 형태로 표현
  • 단어들이 어떤 맥락에서 함께 사용되었는지 이해할 수 있다

 

# 네트워크 그래프 데이터 만들기

tidygraph::as_tbl_graph()
동시 출현 빈도 데이터를 '네트워크 그래프 데이터'로 변환하기
단어를 나타내는 노드(node, 꼭짓점)
단어를 연결하는 엣지(edge, 선)

네트워크가 너무 복잡하지 않도록 25회 이상 사용된 단어 추출해 생성

#1

install.packages("tidygraph")
library(tidygraph)

graph_comment <- pair %>%
  filter(n >= 25) %>%
  as_tbl_graph()

graph_comment 

 

 

#2 네트워크 그래프 만들기

ggraph::ggraph()

library(ggraph)

ggraph(graph_comment) +
  geom_edge_link() +                               # 엣지
  geom_node_point() +                            # 노드
  geom_node_text(aes(label = name))    # 텍스트

 

 

cf ) 그래프를 큰 화면에 출력하는 방법

윈도우: windows()

macOS: x11()

 

 

 

 

#3 그래프 다듬기

한글 폰트 설정

library(showtext)
font_add_google(name = "Nanum Gothic", family = "nanumgothic")
showtext_auto()

 

 

#4 엣지와 노드의 색깔, 크기, 텍스트 위치 수정

ggraph(layout = "fr") : 네트워크 형태 결정

난수를 이용해 매번 형태 달라짐 → set.seed()로 난수 고정

set.seed(1234)                                                          # 난수 고정
ggraph(graph_comment, layout = "fr") +                  # 레이아웃
  geom_edge_link(color = "gray50",                         # 엣지 색깔
                 alpha = 0.5) +                                           # 엣지 명암
  geom_node_point(color = "lightcoral",                     # 노드 색깔
                  size = 5) +                                                # 노드 크기
  geom_node_text(aes(label = name),                       # 텍스트 표시
                 repel = T,                                                   # 노드밖 표시
                 size = 5,                                                    # 텍스트 크기
                 family = "nanumgothic") +                         # 폰트
  theme_graph()                                                         # 배경 삭제





노드 텍스트 폰트 geom_node_text()의 family로 별도 설정. theme()으로 적용 안됨.

 

 

 

#5 네트워크 그래프 함수 만들기

word_network <- function(x) {
  ggraph(x, layout = "fr") +
    geom_edge_link(color = "gray50",
                   alpha = 0.5) +
    geom_node_point(color = "lightcoral",
                    size = 5) +
    geom_node_text(aes(label = name),
                   repel = T,
                   size = 5,
                   family = "nanumgothic") +
    theme_graph()
}

set.seed(1234)
word_network(graph_comment)




 

 

 

 

# 유의어 처리하기

표현은 다르지만 의미가 비슷한 단더
유의어 통일하기 : 네트워크 구조가 간결해지고 단어의 관계가 좀 더 분명하게 드러남

#1 유의어처리하기

comment <- comment %>%
  mutate(word = ifelse(str_detect(word, "감독") &
                         !str_detect(word, "감독상"), "봉준호", word),
         word = ifelse(word == "오르다", "올리다", word),
         word = ifelse(str_detect(word, "축하"), "축하", word))

 

 

# 2 단어 동시 출현 빈도 구하기

pair <- comment %>%
  pairwise_count(item = word,
                 feature = id,
                 sort = T)

 

# 3 네트워크 그래프 데이터 만들기

graph_comment <- pair %>%
  filter(n >= 25) %>%
  as_tbl_graph()

 

 

# 4 네트워크 그래프 만들기

set.seed(1234)
word_network(graph_comment)




 

 

반응형
Comments