1

sorry if it is a stupid question, but I am new to programming and python. I have such code:

dic = {}

for word in text:
    if word in dict:
        dict[word] += 1
    else:
        dict[word] = 1
counter = 0        
for key in sorted(dict, key = lambda x: dict[x]):
    counter += 1
    print(counter, key, dict[key])

I want this code to give me the frequency of the word tokens in the text in ascending order and it works. However, it is case-sensitive. I would like it to be case-insensitive. I was trying to use .lower() and unfortunately, it does not work. Can anyone help me, or suggest any solutions? Thank you for any suggestions,

White
  • 51
  • 6

3 Answers3

1

Does this work for you?

from collections import defaultdict
counter = defaultdict(0)
for word in text8:
    word = word.lower()
    counter[word] += 1

Ideally, you should use a Counter which does something similar but more efficiently.

from collections import Counter
counter = Counter(word.lower() for word in text8)
Resley Rodrigues
  • 2,218
  • 17
  • 17
0

Are you looking for this:

text8 = "sadadsad"
countdict = {}
for word in text8:
    countdict[word] = text8.count(word)
print(countdict)

This will give you char as a key and value as count of char in text8

Vaibhav Jadhav
  • 2,020
  • 1
  • 7
  • 20
0

collection.Counter is very common class for BOW(bag of words).
Plus, you can use str.lower method for ignoring cases.
But note that you will lose original case by this way.

from collections import Counter

text8 = [
    'ABC', 'abc', 'abc', 'def', 'de', 'fg', 'fg'
]

count = Counter(word.lower() for word in text8)
for i, (k, v) in enumerate(count.items()):
    print(i + 1, k, v)

output:

1 abc 3
2 def 1
3 de 1
4 fg 2
Boseong Choi
  • 2,566
  • 9
  • 22