-3
我有一个阅读的文档,作为元组列表的列表。 对于每个元组中的每个元素是(word, label)
对。如何创建元组列表的列表?
基本上,文档是一个句子列表,其中每个句子是一个元组列表。
我试图忽略比10更少出现的单词,并建立一个尊重以前格式的新文档。 对于我使用这个代码:
i=0;
j=0;
dictWords=dict()
for sentence in ldata:
for word in sentence:
j=j+1
if word[0] not in dictWords:
dictWords[word[0]]=1
i=i+1
else:
dictWords[word[0]]=1+dictWords[word[0]]
ldata=[[("the","det"),("boy","noun"),("is",'verb'),("ugly","adj")], [("I","Pronoun"), ("am","verb") ("here" ,"Place")]
lnewdata = []
i = 0
for sentence in ldata:
newSent = []
for word in sentence:
if dictWords[word[0]] < 10:
newSent.append(("unk","unk"))
#dictWords is a dictionnary containing each word's occurences
else:
newSent.append(word)
i = i + 1
lnewdata.extend(newSent)
我的问题是lnewdata下此格式下能变成:
[["the" "det" "boy" "noun" "is" "verb" "ugly" "adj"] ["I" "noun" "am" "verb" "here" "Place" ]
你有什么建议来解决这个问题?
不清楚你在问什么,缩进被打破,你的代码由于未定义的变量而不可执行。如果你想有人回答你的问题,请改善。 – Guillaume