2010-06-21 103 views
3

我正在处理一个小型项目,涉及基于字典的文本搜索文档集合。我的字典有正面的信号词(又名好词),但在文档集合中,找到一个词并不能保证一个积极的结果,因为可能有负面的词例如(不是不重要的),可能在这些正面词的附近。我想要构造一个矩阵,使其包含文档编号,正面词以及其与负面词的接近度。保持跟踪字接近

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

No significant drug interactions have been reported in studies of candesartan cilexetil given with other drugs such as glyburide, nifedipine, digoxin, warfarin, hydrochlorothiazide. 

这是我的示例文件,其中坎地沙坦酯,格列本脲,硝苯地平,地高辛,华法林,氢氯噻嗪是我积极的话,没有显著是我的否定词。我想在我的正面和耸人听闻的单词之间做一个邻近(基于词)的映射。

任何人都可以提供一些有用的指针吗?

回答

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")) 
+0

我几乎找到了我正在寻找的东西。谢谢尼科! – 2010-06-21 15:26:38

3

你看的

+0

不错的包,不知道他们!不过,我不认为R是做这种分析的最佳工具。 – nico 2010-06-21 15:25:01

+0

是的,我经常使用tm包! – 2010-06-21 15:30:28