Skip to content

Commit e57c3a2

Browse files
committed
Added new tab for frequent ngrams. Does not seem to work for one-to-one mapping.
1 parent 649d29f commit e57c3a2

4 files changed

Lines changed: 63 additions & 38 deletions

File tree

‎R/ThreadNet_Core.R‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -853,28 +853,29 @@ selectize_frequent_ngrams<- function(e, TN, CF, minN, maxN, threshold){
853853
frequent_ngrams <- function(e, TN, CF, minN, maxN, threshold){
854854

855855
# initialize the output
856-
f = count_ngrams(e,TN, CF,minN)
856+
ng = count_ngrams(e,TN, CF,minN)
857857

858858
if (maxN > minN){
859859
for (i in seq(minN+1,maxN,1)){
860-
f = rbind(f,count_ngrams(e,TN, CF,i)) }
860+
ng = rbind(ng,count_ngrams(e,TN, CF,i)) }
861861
}
862862
# remove the rows that happen once and only keep the columns we want
863-
f=f[f$freq>threshold,c('ngrams','freq')]
863+
ng=ng[ng$freq>threshold,c('ngrams','freq')]
864864

865865
# just take the maximal ones
866-
f=maximal_ngrams(f)
866+
ng=maximal_ngrams(ng)
867867

868868
# return the set sorted by most frequent
869-
return(f[order(-f$freq),])
869+
return(ng[order(-ng$freq),])
870870
}
871871

872872
# this filters out ngrams that are contained within others ('2 2' is part of '2 2 2')
873-
maximal_ngrams <- function(f){
873+
874+
maximal_ngrams <- function(ng){
874875

875876
# find out if each ngram is contained in all the others
876-
w = lapply(1:nrow(f), function(i){
877-
grep(f$ngrams[i],f$ngrams)}
877+
w = lapply(1:nrow(ng), function(i){
878+
grep(ng$ngrams[i],ng$ngrams)}
878879
)
879880

880881
# get howMany times each one appears
@@ -883,14 +884,22 @@ maximal_ngrams <- function(f){
883884
)
884885

885886
# return the ones that are unique
886-
return(f[which(howMany==1),])
887+
return(ng[which(howMany==1),])
887888
}
888889

889890
# compute support level for each ngram
890891
# tv = text vectors for the threads
891892
# ng = frequent ngrams data frame
892893
support_level <- function(tv, ng) {
893894

895+
# create a support matrix where rows are threads and columns are the ngrams
896+
s = matrix(nrow=length(tv), ncol=nrow(ng),0)
897+
print(s)
898+
899+
# find out how many times each ngram is contained in each TV
900+
w = lapply(1:nrow(ng), function(i){
901+
grep(ng$ngrams[i],tv)}
894902

903+
)
895904

896905
}

‎R/ThreadNet_Graphics.R‎

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,18 @@ ng_bar_chart <- function(o,TN, CF, n, mincount){
261261
return(ngp)
262262
}
263263

264+
# this version we pass in the ngram data frame already sorted
265+
ng_bar_chart_freq <- function(ngdf){
266+
267+
# put them in descending order -- tricky (http://stackoverflow.com/questions/40224892/r-plotly-barplot-sort-by-value)
268+
ngdf$ngrams = factor(ngdf$ngrams, levels =unique(ngdf$ngrams)[order(ngdf$freq, decreasing = FALSE)])
269+
270+
ngp <- plot_ly( ngdf, y = ~ngrams, x = ~freq, type = "bar",showlegend=FALSE) %>%
271+
layout(xaxis= list(showticklabels = TRUE, title='Frequency')) %>%
272+
layout(yaxis= list(showticklabels = FALSE, title=''))
273+
274+
return(ngp)
275+
}
264276

265277

266278
#############################################################################
@@ -546,25 +558,25 @@ Comparison_Plots <- function(e, CF, CF_levels, nTimePeriods=1, ng_size , zoom_le
546558
#'
547559
#' @return standard R plot
548560
#' @export
549-
traminer_threadMap <- function(df,TN, CF){
550-
551-
# setting color palettes
552-
# first find the number of distinct colors
553-
nColors = length(unique(df[,CF]))
554-
pal <- diverge_hcl(nColors)
555-
556-
# reformat the data for traminerR
557-
df = convert_TN_to_TramineR(df, TN, CF)
558-
559-
560-
#plot sequence - also try seqfplot?
561-
# add grouping variable?
562-
return(seqiplot( seqdef(df, cpal=pal) , withlegend = T, main = "ThreadMap", border = NA,idxs=1:nrow(df)) )
563-
564-
# return(seqfplot( seqdef(df, cpal=pal) , withlegend = T, main = "ThreadMap", border = NA,idxs=1:nrow(df)) )
565-
566-
567-
}
561+
# traminer_threadMap <- function(df,TN, CF){
562+
#
563+
# # setting color palettes
564+
# # first find the number of distinct colors
565+
# nColors = length(unique(df[,CF]))
566+
# pal <- diverge_hcl(nColors)
567+
#
568+
# # reformat the data for traminerR
569+
# df = convert_TN_to_TramineR(df, TN, CF)
570+
#
571+
#
572+
# #plot sequence - also try seqfplot?
573+
# # add grouping variable?
574+
# return(seqiplot( seqdef(df, cpal=pal) , withlegend = T, main = "ThreadMap", border = NA,idxs=1:nrow(df)) )
575+
#
576+
# # return(seqfplot( seqdef(df, cpal=pal) , withlegend = T, main = "ThreadMap", border = NA,idxs=1:nrow(df)) )
577+
#
578+
#
579+
# }
568580

569581
# This one is not currently used.
570582
threadLengthBarchart <- function(o, TN){

‎R/UI.R‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,10 @@ ui <- fluidPage(
150150

151151
tabPanel("Frequent ngrams",
152152
uiOutput("freqnGramControls"),
153-
tableOutput("freqnGramTable"),
154-
plotlyOutput("freqnGramBarchart")
153+
fluidRow(
154+
column(2, tableOutput("freqnGramTable")),
155+
column(10,plotlyOutput("freqnGramBarchart"))
156+
)
155157
),
156158

157159
tabPanel("Whole Sequences",

‎R/server.R‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -580,16 +580,18 @@ server <- shinyServer(function(input, output, session) {
580580

581581

582582
# frequent NGRAM displays #
583+
# just compute this data once -- not sure of the best way to display it... Table? Bar chart?
583584

584-
output$freqnGramTable <- renderTable(frequent_ngrams(threadedEventsViz() , 'threadNum',
585-
get_Zoom_VIZ(),
586-
input$freqnGramLengthID[1],
587-
input$freqnGramLengthID[2],
588-
input$freqnGramDisplayThresholdID ) )
585+
fng <-reactive(frequent_ngrams(threadedEventsViz() , 'threadNum',
586+
get_Zoom_VIZ(),
587+
input$freqnGramLengthID[1],
588+
input$freqnGramLengthID[2],
589+
input$freqnGramDisplayThresholdID ))
590+
591+
output$freqnGramTable <- renderTable( fng() )
592+
593+
output$freqnGramBarchart = renderPlotly({ ng_bar_chart_freq(fng() )})
589594

590-
output$freqnGramBarchart = renderPlotly({
591-
ng_bar_chart(threadedEventsViz(), "threadNum", get_Zoom_VIZ(), input$freqnGramLengthID[2], input$freqnGramDisplayThresholdID)
592-
})
593595

594596
######## Whole sequence tab ###############
595597

0 commit comments

Comments
 (0)