【发布时间】:2021-07-01 13:30:04
【问题描述】:
我正在做我的大学项目,我必须从 NLTK 语料库 (SemCor) 中随机阅读 50 个句子。
目前我只能阅读前 50 句如下:
from nltk.corpus import semcor as corpus
def get_sentence_from_semcor(sentence_num):
sentence = " ".join(corpus.sents()[sentence_num])
tags = corpus.tagged_sents(tag="sem")[sentence_num]
for curr_word in range(len(tags)):
if isinstance(tags[curr_word], nltk.Tree) and isinstance(tags[curr_word][0], str) and isinstance(tags[curr_word].label(), nltk.corpus.reader.wordnet.Lemma):
word = tags[curr_word][0]
target = tags[curr_word].label().synset()
sentence_no_word = sentence.replace(word, "")
return word, sentence_no_word, target
corpus_sentences = [get_sentence_from_semcor(i) for i in range(50)]
关于如何随机选择语料库中的 50 个句子有任何帮助吗?
【问题讨论】:
-
在随机模块中调查
random.sample。您提供corpus_sentences和您想要返回的 random.samples 的数量。