为python中的字符串列表中的每个字符串添加特定字符
我写了一个脚本,基本上把一个句子中的所有字符串分成几个部分 比如,为python中的字符串列表中的每个字符串添加特定字符,python,string,Python,String,我写了一个脚本,基本上把一个句子中的所有字符串分成几个部分 比如, "geldigim" -> "gel" "di" "g" "i" "m" 有些字符串可以按上述方式拆分,有些字符串可以按以下方式拆分: "bildi" > "bil" "di" 或者有些句子可能根本就不分开 "kos" -> "kos" 这完全是由一个函数决定的,该函数将字符串拆分为多个部分 我想做的是: geldigim -> /gel* *di* *g* *i* *m/ bildi ->
"geldigim" -> "gel" "di" "g" "i" "m"
有些字符串可以按上述方式拆分,有些字符串可以按以下方式拆分:
"bildi" > "bil" "di"
或者有些句子可能根本就不分开
"kos" -> "kos"
这完全是由一个函数决定的,该函数将字符串拆分为多个部分
我想做的是:
geldigim -> /gel* *di* *g* *i* *m/
bildi -> /bil* *di/
kos -> /kos/
我所做的是
我有一个语料库,有37251512个句子。我写了以下脚本
if __name__ == "__main__":
io = morfessor.MorfessorIO()
print "Importing corpus ..."
f = codecs.open("corpus/corpus_tr_en/corpus.tr", encoding="utf-8").readlines()
print "Importing morphology model ..."
model = io.read_binary_model_file('seg/tr/model.bin')
corpus = open('dataset/dataset_tr_en/full_segmented.tr', 'w')
for a in range(len(f)):
print str(a) + ' : ' + str(len(f))
words = f[a].replace('\n', '').split()
line_str = ''
for word in words:
segmentation = model.viterbi_segment(word)[0]
if len(segmentation) == 1:
line_str = '/' + segmentation[0] + '/'
if len(segmentation) == 2:
line_str = '/' + segmentation[0] + '* *' + segmentation[1] + '/'
if len(segmentation) > 2:
line_str = ''
for b in range(len(segmentation)):
if (b == 0):
line_str = line_str + '/' + segmentation[b] + '*'
if (b != 0) and (b != (len(segmentation) - 1)):
line_str = line_str + ' *' + segmentation[b] + '* '
if (b == (len(segmentation) - 1)):
line_str = line_str + ' *' + segmentation[b] + '/'
line_str = line_str + ' '
corpus.write(line_str.encode('utf-8'))
corpus.write('\n')
corpus.close()
此脚本循环遍历每个句子和句子中的每个单词,并使用io.read\u binary\u model\u file
函数将其拆分为多个部分
但对我来说太贵了,速度太慢了
你能给我建议一个使过程非常快的方法吗
谢谢,可能会减慢很多速度的是使用多个字符串连接的
行的合成,如果您想要性能,不建议使用多个字符串连接(对于filename=base+“.txt”
之类的内容可以,但对于密集处理则不建议使用
将行
创建为列表
,然后使用str.join
创建最终字符串,将其写入磁盘。添加到列表
要快得多
正如Maximilian刚才所建议的,您可以将您的条件转换为elif
,因为它们是互斥的(x2)。还添加了一些微优化,以提高可读性
我建议您的内部循环应该是什么样子:
for word in words:
segmentation = model.viterbi_segment(word)[0]
lenseg = len(segmentation)
if lenseg == 1:
line = ['/',segmentation[0],'/']
elif lenseg == 2:
line = ['/',segmentation[0],'* *',segmentation[1],'/']
elif lenseg > 2:
line = []
for b in range(lenseg):
if b == 0:
line += ['/',segmentation[0],'*']
elif b != (lenseg - 1):
line += [' *',segmentation[b],'* ']
else:
line+= [' *',segmentation[b],'/']
line.append(" ")
corpus.write("".join(line).encode('utf-8'))
备选方案:
- 每次都将每个字符串写入输出文件
- 将数据写入
io.StringIO
对象,并检索数据以写入输出文件
可能会减慢很多速度的是使用多个字符串连接的行的合成,如果您想要性能,不建议使用多个字符串连接(对于filename=base+“.txt”
之类的东西也可以,但不适合密集处理
将行
创建为列表
,然后使用str.join
创建最终字符串,将其写入磁盘。添加到列表
要快得多
正如Maximilian刚才所建议的,您可以将您的条件转换为elif
,因为它们是互斥的(x2)。还添加了一些微优化,以提高可读性
我建议您的内部循环应该是什么样子:
for word in words:
segmentation = model.viterbi_segment(word)[0]
lenseg = len(segmentation)
if lenseg == 1:
line = ['/',segmentation[0],'/']
elif lenseg == 2:
line = ['/',segmentation[0],'* *',segmentation[1],'/']
elif lenseg > 2:
line = []
for b in range(lenseg):
if b == 0:
line += ['/',segmentation[0],'*']
elif b != (lenseg - 1):
line += [' *',segmentation[b],'* ']
else:
line+= [' *',segmentation[b],'/']
line.append(" ")
corpus.write("".join(line).encode('utf-8'))
备选方案:
- 每次都将每个字符串写入输出文件
- 将数据写入
io.StringIO
对象,并检索数据以写入输出文件
像这样的内环怎么样:
line = '* *'.join(segmentation)
corpus.write(("/%s/ " % line).encode('utf-8'))
lines = []
for a in range(len(f)):
print str(a) + ' : ' + str(len(f))
words = f[a].replace('\n', '').split()
for word in words:
line = '* *'.join(segmentation)
lines.append("/%s/ " % line)
corpus.write("\n".join(lines).encode('utf-8')
那么,既然你可以同时将输入保存在内存中,我也会尝试将输出保存在内存中,然后一次性将其写出来,可能是这样的:
line = '* *'.join(segmentation)
corpus.write(("/%s/ " % line).encode('utf-8'))
lines = []
for a in range(len(f)):
print str(a) + ' : ' + str(len(f))
words = f[a].replace('\n', '').split()
for word in words:
line = '* *'.join(segmentation)
lines.append("/%s/ " % line)
corpus.write("\n".join(lines).encode('utf-8')
像这样的内部循环如何:
line = '* *'.join(segmentation)
corpus.write(("/%s/ " % line).encode('utf-8'))
lines = []
for a in range(len(f)):
print str(a) + ' : ' + str(len(f))
words = f[a].replace('\n', '').split()
for word in words:
line = '* *'.join(segmentation)
lines.append("/%s/ " % line)
corpus.write("\n".join(lines).encode('utf-8')
那么,既然你可以同时将输入保存在内存中,我也会尝试将输出保存在内存中,然后一次性将其写出来,可能是这样的:
line = '* *'.join(segmentation)
corpus.write(("/%s/ " % line).encode('utf-8'))
lines = []
for a in range(len(f)):
print str(a) + ' : ' + str(len(f))
words = f[a].replace('\n', '').split()
for word in words:
line = '* *'.join(segmentation)
lines.append("/%s/ " % line)
corpus.write("\n".join(lines).encode('utf-8')
- 让·弗朗索瓦·法布很好地报道了这场比赛
- 另一个要点是对37251512个句子使用
readlines()
。只需对f中的a使用,有关详细说明,请参阅
- 根据数据中有多少重复项以及model.viterbi_segment函数的性能,使用一组单词而不是对重复的单词进行重复可能是有益的
- 看起来您使用的是python 2.#,在这种情况下,使用而不是
range
.replace('\n','').split()
速度较慢,因为当您只想删除最后一个换行符时,它必须在整行上循环(在您的情况下不能有多个换行符)。您可以使用`
- 您的代码中存在一些冗余,例如,每行需要以
/
结尾,但您有3个位置
- 所有这些更改可能都很小,但它们会累加起来,您的代码也会变得更易于阅读
- 让·弗朗索瓦·法布很好地报道了这场比赛
- 另一个要点是对37251512个句子使用
readlines()
。只需对f中的a使用,有关详细说明,请参阅
- 根据数据中有多少重复项以及model.viterbi_segment函数的性能,使用一组单词而不是对重复的单词进行重复可能是有益的
- 看起来您使用的是python 2.#,在这种情况下,使用而不是
range
.replace('\n','').split()
速度较慢,因为当您只想删除最后一个换行符时,它必须在整行上循环(在您的情况下不能有多个换行符)。您可以使用`
- 您的代码中存在一些冗余,例如,每行需要以
/
结尾,但您有3个位置
- 所有这些更改可能都很小,但它们会累加起来,您的代码也会变得更易于阅读
维特比_段()是做什么的?请你发布这个函数的代码好吗?/Teşekkür**ler/它基本上是一个函数,它基本上把字符串放进了“摩弗森”创建的机器学习模型中。我问的是,如果你给我们看代码,也许有办法加快它的速度。维特比_段()是做什么的是吗?请你把这个函数的代码贴出来好吗?/Teşekkür**ler/它基本上是一个函数,它基本上把字符串放进了由“摩弗瑟”创建的机器学习模型中.我这么问是因为如果你给我们看代码,也许有办法加快速度。使用elif而不是多余的ifs可能会使整个过程加快一点。对!我太关注字符串了,以至于我没有看到这一点。这是有道理的,尽管与字符串问题相比,这可能是一个微观优化。也许问题在于他也有维特比函数,但我们没有,无论如何