Wednesday, February 15, 2017

For Counting words in text(Python)

def count_words_fast(text):
    text=text.replace("\n","")
    text=text.replace("\r","")
    dic={}
   
    for val in text.split(" "):
        if not val in dic:
            dic[val]=1
        else:
            dic[val]+=1
             
    return(dic)
def word_count_distribution(text):
    word_counts={}
    word_counts=count_words_fast(text)

    dict={}
   
    for val in word_counts.values():
        if not val in dict:
            dict[val]=1
        else:
            dict[val]+=1
    return(dict)   

No comments:

Post a Comment