Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distance metrics added #85

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions global.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,37 @@ library(quanteda)
library(pdftools)
library(readtext)
library(msgxtractr)
library(transport)

#Enable bookmarking via url
#enableBookmarking(store = "server")

#Custom functions

#Jensen-Shannon divergence
JensenShannon <- function(query, matrix){
p <- query[1,]
q <- t(matrix) # transpose matrix
m = (p + q)/2
KLDp = mapply(KLD, p, m)
KLDq = mapply(KLD, p, m)
jsd = sqrt(0.5*(KLDp + KLDq))
jsd[is.nan(jsd)] <- 0
return (jsd)
}

#Kullback Leibler divergence
KLD <- function(a,b){
sum(a * log(a/b))
}

#Earth Movers distance
EarthMoversDistance <- function(query, matrix){
matrix <- t(matrix)
emdismatrix = apply(matrix, 1,function(i) { wasserstein1d(query, i, p = 1, wa = NULL, wb = NULL)})
return(emdismatrix)
}

CiteListPubmed <- function(elinkxml){
#Function to return data frame of citations for articles in entrez link xml document.
#Input:
Expand Down
14 changes: 13 additions & 1 deletion server.R
Original file line number Diff line number Diff line change
Expand Up @@ -2463,13 +2463,25 @@ if((file.exists(paste0(getwd(),"/ToPMine/topicalPhrases/win_run.bat")) == TRUE)
topicvec <- c(temp$theta)
names(topicvec) <- paste("Topic", c(1:length(topicvec)))

##Distance Metrics
#Find the cosine distance between search document and all existing documents by topic proportions
# jsdist <- JensenShannon(temp$theta, topicmodel$TopicModel$theta)
cosdist <- proxy::dist(x = temp$theta, y = topicmodel$TopicModel$theta, method = "cosine")
# emdist <- EarthMoversDistance(temp$theta, t(topicmodel$TopicModel$theta))
# browser()

#Get document order from closest match to furthest
#Get document order from closest match to furthest using Jensen Shannon
# closedocs <- topicmodel$Metadata$PMID[order(jsdist)]
# distperc <- jsdist[order(jsdist)]

#Get document order from closest match to furthest using Cosine Distance
closedocs <- topicmodel$Metadata$PMID[order(cosdist)]
distperc <- cosdist[order(cosdist)]

#Get document order from closest match to furthest using Earth Movers Distance
# closedocs <- topicmodel$Metadata$PMID[order(emdist)]
# distperc <- emdist[order(emdist)]

# #Old cosine distance code that calculates distance between all documents instead of just the required distances to the new document
# #Find the cosine distance between search document and all existing documents by topic proportions
# cosdist <- proxy::dist(rbind(temp$theta, topicmodel$TopicModel$theta), method = "cosine")
Expand Down