pythonpython-3.xpython-2.7stanford-nlpnltk-trainer

How to add a label to all words in a file?


I have a file containing words, I want to read this file and add a label in front of all words. The label should be added on the right side of the words. eg. book - "O", Berlin - "O". How to do it in python? I have tried this code but not given my answer.

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()

Solution

  • If I understand the correct output format word-O, you can try something like this:

    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))