使用Python NLTK中的concordance返回字典中找到的键的值

时间:2013-03-13 20:47:39

标签: python dictionary nltk

我想使用concordance在文本中查找单词或短语的实例,然后在字典中查找找到的单词/短语并返回相应的值。这是我到目前为止的代码。

from __future__ import division
import nltk, re, pprint
OutFileName = "shark_uri.txt"
OutFile = open(OutFileName, 'w')
book1 = open('shark_test.txt', 'rU').read() 
token1 = nltk.word_tokenize(book1)
text1 = nltk.Text(token1)
LineNumber = 0
for k, v in bio_dict.iteritems(): 
        text1.concordance(k)
    #if k is found then print v, else go on to next k
    if k #is found:
        OutFile.write(v)
        OutFile.write('\n')
        LineNumber += 1
    else
        LineNumber += 1
OutFile.close()

此代码应该在shark_test.txt文件中读取有关鲨鱼的段落。 bio_dict包含像这样的键值对

'ovoviviparous':'http://dbpedia.org/resource/Ovoviviparity', 
'predator':'http://dbpedia.org/resource/Predation',

键表示程序正在查找的单词或短语。值是与单词/短语对应的DBpedia URI。这个想法是,当在文本中找到像“捕食者”这样的单词时,程序将返回DBpedia URI for Predation。 我得到了很多奇怪的结果,我认为这是因为我需要告诉程序,如果发现k返回v,则转到下一个k。我在上面的代码块中放了一个占位符。我不太清楚如何用Python来表达这一点。会不会像k == True? 没有这个条件,似乎只是通过字典打印所有值,无论是否找到键。有什么建议?提前谢谢。

2 个答案:

答案 0 :(得分:1)

您的代码现在的工作方式是,您正在迭代bio_dict字典中的所有键值对,然后使用concordance打印text1其中{{1行}的行1}}存在。需要注意的是,使用k 不会返回任何内容,而只是打印。因此,即使您尝试使用返回值(实际上并未在代码中使用),也不能。当您编写concordance时,这将始终为if k: - 假设您的密钥是非空字符串(没有任何密钥评估为True)。

如果我理解你的问题,你真的不应该使用False。相反,做这样的事情:

concordance

此外,您的for word in token1: # Go through every word in your text if word in bio_dict: # Check if the word is in the dict OutFile.write(bio_dict[word]+'\n') # Output the value to your file 计数器实际上并不计算您想要的数量,因为您正在同时读取输入文件并在LineNumber中对整个事物进行标记。但由于您实际上并未使用token1,因此您可以删除该变量并仍然获得所需的输出。

答案 1 :(得分:-1)

我设法用这段代码得到了我需要的东西。

from __future__ import division
import urllib
import re, pprint, time
in_file_name = "shark_id.txt"
in_file = open(in_file_name, 'r')
out_file_name = "shark_uri.txt"
out_file = open(out_file_name, 'w')

for line in in_file:                                                    
line = line.strip()                                             
address = 'http://eol.org/api/data_objects/1.0/' + line + '.xml'    
web_content = urllib.urlopen(address)                           
results = web_content.read().lower()                                        
temp_file_name = "Temp_file.xml"                                    
temp_file = open(temp_file_name, 'w')                               
temp_file.write(results)    
temp_file.close()                                           
print line
print len(results)              
temp_file = open('Temp_file.xml')
data = temp_file.read()
temp_file.close()
for k, v in bio_dict.iteritems():                           
    if k in data:                       
        out_file.write(line + ',')                                  
        out_file.write(k + ',')                                 
        out_file.write(v)                                       
        out_file.write('\n')                                        
time.sleep(.5)
in_file.close()                                                     
out_file.close()