Quantcast
Viewing latest article 6
Browse Latest Browse All 18

Answer by Joe Zhow for n-grams in python, four, five, six grams?

You can get all 4-6gram using the code without other package below:

from itertools import chaindef get_m_2_ngrams(input_list, min, max):    for s in chain(*[get_ngrams(input_list, k) for k in range(min, max+1)]):        yield ''.join(s)def get_ngrams(input_list, n):    return zip(*[input_list[i:] for i in range(n)])if __name__ == '__main__':    input_list = ['I', 'am', 'aware', 'that', 'nltk', 'only', 'offers', 'bigrams', 'and', 'trigrams', ',', 'but', 'is', 'there', 'a', 'way', 'to', 'split', 'my', 'text', 'in', 'four-grams', ',', 'five-grams', 'or', 'even', 'hundred-grams']    for s in get_m_2_ngrams(input_list, 4, 6):        print(s)

the output is below:

I am aware thatam aware that nltkaware that nltk onlythat nltk only offersnltk only offers bigramsonly offers bigrams andoffers bigrams and trigramsbigrams and trigrams ,and trigrams , buttrigrams , but is, but is therebut is there ais there a waythere a way toa way to splitway to split myto split my textsplit my text inmy text in four-gramstext in four-grams ,in four-grams , five-gramsfour-grams , five-grams or, five-grams or evenfive-grams or even hundred-gramsI am aware that nltkam aware that nltk onlyaware that nltk only offersthat nltk only offers bigramsnltk only offers bigrams andonly offers bigrams and trigramsoffers bigrams and trigrams ,bigrams and trigrams , butand trigrams , but istrigrams , but is there, but is there abut is there a wayis there a way tothere a way to splita way to split myway to split my textto split my text insplit my text in four-gramsmy text in four-grams ,text in four-grams , five-gramsin four-grams , five-grams orfour-grams , five-grams or even, five-grams or even hundred-gramsI am aware that nltk onlyam aware that nltk only offersaware that nltk only offers bigramsthat nltk only offers bigrams andnltk only offers bigrams and trigramsonly offers bigrams and trigrams ,offers bigrams and trigrams , butbigrams and trigrams , but isand trigrams , but is theretrigrams , but is there a, but is there a waybut is there a way tois there a way to splitthere a way to split mya way to split my textway to split my text into split my text in four-gramssplit my text in four-grams ,my text in four-grams , five-gramstext in four-grams , five-grams orin four-grams , five-grams or evenfour-grams , five-grams or even hundred-grams

you can find more detail on this blog


Viewing latest article 6
Browse Latest Browse All 18

Trending Articles



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