우선이 작업에 R을 사용하지 않기를 제안합니다. R은 많은 것들을 위해 훌륭하지만, 텍스트 조작은 그것들 중 하나가 아닙니다. 파이썬은 좋은 대안이 될 수 있습니다. 내가 R에이를 구현한다면
아마 같은 (매우 매우 거친을) 할 것, 말했다 :
# You will probably read these from an external file or a database
goodWords <- c("candesartan cilexetil", "glyburide", "nifedipine", "digoxin", "blabla", "warfarin", "hydrochlorothiazide")
badWords <- c("no significant", "other drugs")
mytext <- "no significant drug interactions have been reported in studies of candesartan cilexetil given with other drugs such as glyburide, nifedipine, digoxin, warfarin, hydrochlorothiazide."
mytext <- tolower(mytext) # Let's make life a little bit easier...
goodPos <- NULL
badPos <- NULL
# First we find the good words
for (w in goodWords)
{
pos <- regexpr(w, mytext)
if (pos != -1)
{
cat(paste(w, "found at position", pos, "\n"))
}
else
{
pos <- NA
cat(paste(w, "not found\n"))
}
goodPos <- c(goodPos, pos)
}
# And then the bad words
for (w in badWords)
{
pos <- regexpr(w, mytext)
if (pos != -1)
{
cat(paste(w, "found at position", pos, "\n"))
}
else
{
pos <- NA
cat(paste(w, "not found\n"))
}
badPos <- c(badPos, pos)
}
# Note that we use -badPos so that when can calculate the distance with rowSums
comb <- expand.grid(goodPos, -badPos)
wordcomb <- expand.grid(goodWords, badWords)
dst <- cbind(wordcomb, abs(rowSums(comb)))
mn <- which.min(dst[,3])
cat(paste("The closest good-bad word pair is: ", dst[mn, 1],"-", dst[mn, 2],"\n"))
나는 거의 내가 찾고있는 것을 얻었습니다. 고마워 니코! –