将NLTK中生成的两个列表映射到字典

2024-04-25 12:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理由NLTK的PlaintextCorpusReader生成的两个列表,我想将它们合并到一个字典中。你知道吗

字典的关键应该是语料库中的句子,我使用plaintextcoppusreader的.sents()提取了这些句子。这些值应该是每个句子在语料库中所处位置的fileid,我使用.fileids()提取了它们。你知道吗

.fileids()作为字符串返回,例如

['R_v_Cole_2007.txt', 'R_v_Sellick_2005.txt'] 

.sents()返回为list(list(str)),例如

[[u'1', u'.'], [u'The', u'Registrar', u'has', u'referred', u'to', u'this', u'Court', u'two', u'applications', u'for', u'permission', u'to', u'appeal', u'against', u'conviction', u'to', u'be', u'heard', u'together', u'.'], ...]

我尝试了一系列的方法,主要是从this question到类似的问题,但每次尝试都会导致以下错误:

TypeError: unhashable type: 'list'

我哪里出错了?你知道吗

我正在使用的代码为字典获取所需的内容如下:

corpus_root = '/Users/danielhoadley/Documents/Python/NLTK/text/'
wordlists = PlaintextCorpusReader(corpus_root, '.*')

dictionary = {}

values = wordlists.fileids()
keys = wordlists.sents()

## How do I get the keys and values into a dictionary from here?

Tags: totxtdictionary字典rootcorpusthislist