跟踪单词接近度

Shr*_*nik 3 r text-mining

我正在开发一个小项目,该项目涉及在文档集合中进行基于字典的文本搜索.我的字典有正面的信号词(又名好词),但在文档集中只是找到一个单词并不能保证肯定的结果,因为可能存在负面词,例如(不是,不重要)可能在这些正面词附近.我想构建一个矩阵,使其包含文档编号,正文字及其与否定字的接近程度.

任何人都可以建议一种方法来做到这一点.我的项目处于非常早期阶段,所以我给出了我的文本的基本示例.

No significant drug interactions have been reported in studies of candesartan cilexetil given with other drugs such as glyburide, nifedipine, digoxin, warfarin, hydrochlorothiazide.   
Run Code Online (Sandbox Code Playgroud)

这是我的示例文件,其中坎地沙坦西酯,格列本脲,硝苯地平,地高辛,华法林,氢氯噻嗪是我的正面词,没有重要的是我的否定词.我想在我的积极和有意义的词之间做一个接近(基于词的)映射.

谁能提供一些有用的指示?

nic*_*ico 5

首先,我建议不要使用R来完成这项任务.R很适合很多东西,但文本处理不是其中之一.Python可能是一个很好的选择.

也就是说,如果我在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"))
Run Code Online (Sandbox Code Playgroud)