Quantcast
Viewing latest article 2
Browse Latest Browse All 18

Answer by 柯鴻儀 for n-grams in python, four, five, six grams?

It's quite easy to do n gram in python, for example:

def n_gram(list,n):     return [ list[i:i+n] for i in range(len(list)-n+1) ]

and if you do :

str = "I really like python, it's pretty awesome."n_gram(str.split(" "),4)

You will get

[['I', 'really', 'like', 'python,'], ['really', 'like', 'python,', "it's"], ['like', 'python,', "it's", 'pretty'], ['python,', "it's", 'pretty', 'awesome.']]

Viewing latest article 2
Browse Latest Browse All 18

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>