【发布时间】:2019-08-22 11:40:30
【问题描述】:
我正在尝试从语料库创建依赖解析器。语料库是 conll 格式,所以我有一个函数可以读取文件并返回一个列表列表,其中每个列表都是一个解析的句子(我正在使用的语料库已经解析,我的工作是在这个解析)。我的教授要求在这个语料库中只随机选择 5% 的句子,因为它太大了。
我尝试过创建一个空列表并使用 append 函数,但我不知道如何通过索引来指定我想要语料库的每 100 个句子中的 5 个
我为转换conll文件所做的功能如下:
import os, nltk, glob
def read_files(path):
"""
Function to load Ancora Dependency corpora (GLICOM style)
path = full path to the files
returns de corpus in sentences
each sentence is a list of tuples
each tuple is a token with the follwoing info:
index of the token in the sentence
token
lemma
POS /es pot eliminar
POS
FEAT /es pot eliminar
head
DepRelation
"""
corpus = []
for f in glob.glob(path):
sents1 = open(f).read()[185:-2].split('\n\n')
sents2 = []
for n in range(len(sents1)):
sents2.append(sents1[n].split('\n'))
sents3 = []
for s in sents2:
sent = []
for t in s:
sent.append(tuple(t.split('\t')))
sents3.append(sent)
corpus.extend(sents3)
return corpus
我想要一种从语料库中每 100 个句子中选择 5 个句子的方法,这样我就可以得到一个仅包含这些的列表。 提前致谢!
【问题讨论】:
-
你能显示示例输入数据和预期结果吗?
-
random.sample(the_list, 5)