I have a code that performs the Part of speech tagging using nltk and next, I am counting how many tags occurred in each row.
Example: If I have a string input as:
A = [('car', 'NN'), ('bike', 'NN')]
I tried to write my own for loop as below as well:
def count_tags(input_col):
tag_count = {}
for word, tag in input_col:
if tag in tag_count:
tag_count[tag] += 1
else:
tag_count[tag] = 1
return(tag_count)
print('tag count is: ', count_tags(hi))
which output the same as:
tag count is: {'NN': 2}
Question:
I have a CSV file that contains strings in each row as like A example above:
print(df['tags'])
0 [('car', 'NN'), ('bike', 'NN')]
1 [('TV', 'NN'), ('access', 'JJ')
and when I load with panda and then tried to count the tags:
df['tag_count'] = df['tags'].map(count_tags)
I am getting the following error:
ValueError: not enough values to unpack (expected 2, got 1)
My expected output is:
ID tags count
2 [('car', 'NN'), ('bike', 'NN')] {'NN': 2}
4 [('TV', 'NN'), ('access', 'JJ')] {'NN': 1, 'JJ': 1}