如何为文件中的所有单词添加标签?
How to add a label to all words in a file?
我有一个包含单词的文件,我想读取这个文件并在所有单词前添加一个标签。标签应添加在单词的右侧。例如。 book - "O"
、Berlin - "O"
。如何在 python 中完成?我试过这段代码,但没有给出我的答案。
inp = open('Dari.pos', 'r')
out = open('DariNER.txt', 'w')
for line in iter(inp):
word= line.__add__("O")
out.write(word)
inp.close()
out.close()
如果我理解正确的输出格式word-O,你可以试试这样:
words = open('filename').read().split()
labeled_words = [word+"-O" for word in words]
# And now user your output format, each word a line, separate by tabs, whatever.
# For example new lines
with open('outputfile','w') as output:
output.write("\n".join(labeled_words))
在您更新的问题中,您展示了添加了一些字符的单词示例(我假设您指的是行):
eg. book - "O", Berlin - "O"
对代码的修改应该会产生以下输出:
for line in iter(inp):
word = '{} - "O"'.format(line)
out.write(word)
我 运行 使用以下代码进行测试:
inp = ['This is a book','I bought it in Berlin']
for line in iter(inp):
word = '{} - "O"'.format(line)
print(word)
输出:
This is a book - "O"
I bought it in Berlin - "O"
我有一个包含单词的文件,我想读取这个文件并在所有单词前添加一个标签。标签应添加在单词的右侧。例如。 book - "O"
、Berlin - "O"
。如何在 python 中完成?我试过这段代码,但没有给出我的答案。
inp = open('Dari.pos', 'r')
out = open('DariNER.txt', 'w')
for line in iter(inp):
word= line.__add__("O")
out.write(word)
inp.close()
out.close()
如果我理解正确的输出格式word-O,你可以试试这样:
words = open('filename').read().split()
labeled_words = [word+"-O" for word in words]
# And now user your output format, each word a line, separate by tabs, whatever.
# For example new lines
with open('outputfile','w') as output:
output.write("\n".join(labeled_words))
在您更新的问题中,您展示了添加了一些字符的单词示例(我假设您指的是行):
eg. book - "O", Berlin - "O"
对代码的修改应该会产生以下输出:
for line in iter(inp):
word = '{} - "O"'.format(line)
out.write(word)
我 运行 使用以下代码进行测试:
inp = ['This is a book','I bought it in Berlin']
for line in iter(inp):
word = '{} - "O"'.format(line)
print(word)
输出:
This is a book - "O"
I bought it in Berlin - "O"