[Python] Python NLTK: Bigrams trigrams fourgrams

Discussão em 'Python' iniciado por Stack, Agosto 25, 2025.

  1. Stack

    Stack Membro Participativo

    I have this example and i want to know how to get this result. I have text and I tokenize it then I collect the bigram and trigram and fourgram like that

    import nltk
    from nltk import word_tokenize
    from nltk.util import ngrams
    text = "Hi How are you? i am fine and you"
    token=nltk.word_tokenize(text)
    bigrams=ngrams(token,2)


    bigrams: [('Hi', 'How'), ('How', 'are'), ('are', 'you'), ('you', '?'), ('?', 'i'), ('i', 'am'), ('am', 'fine'), ('fine', 'and'), ('and', 'you')]

    trigrams=ngrams(token,3)


    trigrams: [('Hi', 'How', 'are'), ('How', 'are', 'you'), ('are', 'you', '?'), ('you', '?', 'i'), ('?', 'i', 'am'), ('i', 'am', 'fine'), ('am', 'fine', 'and'), ('fine', 'and', 'you')]

    bigram [(a,b) (b,c) (c,d)]
    trigram [(a,b,c) (b,c,d) (c,d,f)]
    i want the new trigram should be [(c,d,f)]
    which mean
    newtrigram = [('are', 'you', '?'),('?', 'i','am'),...etc


    any idea will be helpful

    Continue reading...

Compartilhe esta Página