从 nltk 语料库中随机读取句子
Read randomly sentences from nltk corpus
我正在做我的大学项目,我必须从 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)]
任何关于我如何 select 随机语料库中的 50 个句子的帮助?
好吧,你想要随机性,所以让我们导入 random
库:
import random
然后我们需要知道我们的约束是什么。显然,我们可以select最早的earliest1是句子1,或者索引0的句子,但是要知道max;我们需要统计句子的个数,然后减1得到最后一个句子的索引。
max_sentence = len(corpus.sents())-1
我们将创建一个空列表来存储我们的[伪]随机数:
list_of_random_indexes = []
然后在其中获取一些数字(在本例中为 50 个):
for i in range(50):
list_of_random_indexes.append(random.randint(0, max_sentence))
然后以修改后的最后一行结束,它现在引用我们的随机数列表而不是范围:
corpus_sentences = [get_sentence_from_semcor(i) for i in list_of_random_indexes]
总之:
import random
max_sentence = len(corpus.sents())-1
list_of_random_indexes = []
for i in range(50):
list_of_random_indexes.append(random.randint(0, max_sentence))
corpus_sentences = [get_sentence_from_semcor(i) for i in list_of_random_indexes]
或者让它更简洁一点:
import random
max_sentence = len(corpus.sents())-1
list_of_random_indexes = [random.randint(0, max_sentence) for I in range(50)]
corpus_sentences = [get_sentence_from_semcor(i) for i in list_of_random_indexes]
但是由于您可能不希望有重复的行,因此我还会在添加索引之前检查它是否已经在列表中。
import random
max_sentence = len(corpus.sents())-1
list_of_random_indexes = []
while len(list_of_random_indexes)<50:
test_index = random.randint(0, max_sentence)
if test_index not in list_of_random_indexes:
list_of_random_indexes.append(test_index)
corpus_sentences = [get_sentence_from_semcor(i) for i in list_of_random_indexes]
您可以尝试这样的操作:
import numpy
length = len(nltk.corpus.semcor.sents())-50
for i in range(n_times):
start = np.random.randint(0, length)
corpus_sentences = [get_sentence_from_semcor(i) for i in range(start,(start+50))]
代码将迭代 n_times,每次返回一组 50 个句子。 'start' 是范围(0,长度)内的随机整数。 (假设你知道语料库的总长度)。
我正在做我的大学项目,我必须从 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)]
任何关于我如何 select 随机语料库中的 50 个句子的帮助?
好吧,你想要随机性,所以让我们导入 random
库:
import random
然后我们需要知道我们的约束是什么。显然,我们可以select最早的earliest1是句子1,或者索引0的句子,但是要知道max;我们需要统计句子的个数,然后减1得到最后一个句子的索引。
max_sentence = len(corpus.sents())-1
我们将创建一个空列表来存储我们的[伪]随机数:
list_of_random_indexes = []
然后在其中获取一些数字(在本例中为 50 个):
for i in range(50):
list_of_random_indexes.append(random.randint(0, max_sentence))
然后以修改后的最后一行结束,它现在引用我们的随机数列表而不是范围:
corpus_sentences = [get_sentence_from_semcor(i) for i in list_of_random_indexes]
总之:
import random
max_sentence = len(corpus.sents())-1
list_of_random_indexes = []
for i in range(50):
list_of_random_indexes.append(random.randint(0, max_sentence))
corpus_sentences = [get_sentence_from_semcor(i) for i in list_of_random_indexes]
或者让它更简洁一点:
import random
max_sentence = len(corpus.sents())-1
list_of_random_indexes = [random.randint(0, max_sentence) for I in range(50)]
corpus_sentences = [get_sentence_from_semcor(i) for i in list_of_random_indexes]
但是由于您可能不希望有重复的行,因此我还会在添加索引之前检查它是否已经在列表中。
import random
max_sentence = len(corpus.sents())-1
list_of_random_indexes = []
while len(list_of_random_indexes)<50:
test_index = random.randint(0, max_sentence)
if test_index not in list_of_random_indexes:
list_of_random_indexes.append(test_index)
corpus_sentences = [get_sentence_from_semcor(i) for i in list_of_random_indexes]
您可以尝试这样的操作:
import numpy
length = len(nltk.corpus.semcor.sents())-50
for i in range(n_times):
start = np.random.randint(0, length)
corpus_sentences = [get_sentence_from_semcor(i) for i in range(start,(start+50))]
代码将迭代 n_times,每次返回一组 50 个句子。 'start' 是范围(0,长度)内的随机整数。 (假设你知道语料库的总长度)。