This repository has been archived by the owner on Aug 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Part-2.R
67 lines (58 loc) · 2.3 KB
/
Part-2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
## Part-2.R
#
# Requer a execução prévia do Part-1.R ou leitura dos dados de ambiente R.
#
# ##
#setwd("C:\\Users\\jonas\\JONAS\\MIA\\mia-pln")
# Prepara a lista de documentos para tratamento.
#
# Retira identificação de processos.
docs.clear <- sub(docs.text, pattern = "Proc: P([0-9]{6})/([0-9]{4})", replacement = "", perl=TRUE)
# Converte para caixa baixa.
docs.lower <- stri_trans_tolower(docs.clear)
# Corrige pontuação, em função de se ter detectado erros crassos neste aspecto.
docs.lower <- gsub(docs.lower, pattern = "[.]", replacement = ". ", perl = TRUE, fixed = FALSE)
docs.lower <- gsub(docs.lower, pattern = "[,]", replacement = ", ", perl = TRUE, fixed = FALSE)
docs.lower <- gsub(docs.lower, pattern = "sr:", replacement = "sr. ", perl = TRUE)
docs.lower <- gsub(docs.lower, pattern = "sra:", replacement = "sra. ", perl = TRUE)
# Retira espaços extras.
docs.lower <- gsub(trimws(docs.lower), pattern = "\\s+", replacement = " ")
# Carrega ligações e funções de uso da Rosette API.
source(".\\Rosette.R", encoding = "UTF-8")
# Carrega funções de busca por relações entre as pessoas.
source(".\\Relatives.R", encoding = "UTF-8")
# PROCESSAMENTO DO CORPUS COM EXTRAÇÃO DAS ENTIDADES NOMEADAS
# E RELAÇÕES ENTRE AS PESSOAS.
#
library(foreach)
length <- length(docs.lower)
result <- foreach(i = 1 : length) %do%
unlist(list( EntitiesFrom(docs.lower[[i]], index = i),
list( relations = FindRelations(docs.lower[[i]], index = i)$relations)
), recursive = FALSE
)
# ##
# GERAÇÃO DA SAÍDA EM PLANILHA XLSX.
# @see http://www.sthda.com/english/wiki/r2excel-read-write-and-format-easily-excel-files-using-r-software
#
# Instala o pacote necessário: r2Excel
#
library(rJava)
install.packages("devtools")
devtools::install_github("kassambara/r2excel")
## Faz exportação
#
ExportResult(result, TRUE)
## Recupera todas as relações identificadas entre as pessoas.
#
result.allRelations <- unlist(lapply(FUN = function(x) {
if (is.null(x$relations) || length(x$relations) == 0) NA else x$relations[1]
}, result))
result.relations <- table(result.allRelations)
## Recupera todos os nomes de pessoas identificados.
#
result.names <- lapply(result, function(x) {
if (is.null(x$entities)) return(NA)
entities <- data.frame(x$entities)
return (as.character(subset(entities, type %in% c("PERSON"))$mention))
})