Count tag per word per POS how many occurrences

Data is like this{DT The NNP Fulton NNP County NNP Grand NNP Jury VBD said NNP Friday 0 DT an NN investigation IN of NNP Atlanta POS 's JJ recent JJ primary NN election VBD produced DT no NN evidence ‘’ ‘’ IN that DT any NNS irregularities VBD took NN place . . DT The NN jury RB further VBD said IN in JJ term-end NNS presentments IN that DT the NNP City NNP Executive }

fname = open('/content/try list.txt', "r")

counts = dict()

for line in fname:

    words = line.split()

    for word in words:

        if word not in counts:

            counts[word] = 1

        else:

            counts[word] += 1

print(counts)
My output : {'DT': 10, 'The': 2, 'NNP': 11, 'Fulton': 1, 'County': 1, 'Grand': 1, 'Jury': 1, 'VBD': 5, 'said': 2, 'Friday': 1, '0': 1, 'an': 1, 'NN': 9, 'investigation': 1, 'IN': 8, 'of': 4, 'Atlanta': 2, 'POS': 1, "'s": 1, 'JJ': 4, 'recent': 1,}

It’s counting the occurrence of each word and stage but how can I do words wise?
Expected output should be: The–>DT:48, Fulton–> NNP:28

We can’t debug it as we are missing the file, I suggest you add a lot of print statements to see what’s going on, for example a print(line). Is it printing all the lines?

Hey divya,

i assume that your mistake is in the following line:

Try either:

    for word in words:
        if word not in counts:
            counts[word] = 1
    
        else:
            counts[word] = counts[word] + 1

or another method:

    for word in words:
        counts[word] = counts.get(word, 0) + 1

counts.get is an idiom, or a built in function, to add new keys to a dictonary and to add a value for a key already in the dict.

Let me know if this helped you.

Greets

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.