Python NLTK: Bigrams trigrams fourgrams
Asked Answered
F

4

25

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

Footy answered 22/6, 2014 at 0:16 Comment(3)
I don't understand; it seems like you've already generated the ngrams?Hyperboloid
@Hyperboloid my question how to get the newtrigram i trying to find a function which can search inside the element of bigram and compare it with the element of trigram and take only the differentFooty
There's an everygrams implementation now =)Counterstatement
D
10

If you apply some set theory (if I'm interpreting your question correctly), you'll see that the trigrams you want are simply elements [2:5], [4:7], [6:8], etc. of the token list.

You could generate them like this:

>>> new_trigrams = []
>>> c = 2
>>> while c < len(token) - 2:
...     new_trigrams.append((token[c], token[c+1], token[c+2]))
...     c += 2
>>> print new_trigrams
[('are', 'you', '?'), ('?', 'i', 'am'), ('am', 'fine', 'and')]
Detonator answered 22/6, 2014 at 0:54 Comment(1)
actually my question is related to another question if you can take a look at this question maybe you will get the whole idea #24290053Footy
C
8

Try everygrams:

from nltk import everygrams
list(everygrams('hello', 1, 5))

[out]:

[('h',),
 ('e',),
 ('l',),
 ('l',),
 ('o',),
 ('h', 'e'),
 ('e', 'l'),
 ('l', 'l'),
 ('l', 'o'),
 ('h', 'e', 'l'),
 ('e', 'l', 'l'),
 ('l', 'l', 'o'),
 ('h', 'e', 'l', 'l'),
 ('e', 'l', 'l', 'o'),
 ('h', 'e', 'l', 'l', 'o')]

Word tokens:

from nltk import everygrams

list(everygrams('hello word is a fun program'.split(), 1, 5))

[out]:

[('hello',),
 ('word',),
 ('is',),
 ('a',),
 ('fun',),
 ('program',),
 ('hello', 'word'),
 ('word', 'is'),
 ('is', 'a'),
 ('a', 'fun'),
 ('fun', 'program'),
 ('hello', 'word', 'is'),
 ('word', 'is', 'a'),
 ('is', 'a', 'fun'),
 ('a', 'fun', 'program'),
 ('hello', 'word', 'is', 'a'),
 ('word', 'is', 'a', 'fun'),
 ('is', 'a', 'fun', 'program'),
 ('hello', 'word', 'is', 'a', 'fun'),
 ('word', 'is', 'a', 'fun', 'program')]
Counterstatement answered 14/1, 2019 at 8:13 Comment(0)
A
3

I do it like this:

def words_to_ngrams(words, n, sep=" "):
    return [sep.join(words[i:i+n]) for i in range(len(words)-n+1)]

This takes a list of words as input and returns a list of ngrams (for given n), separated by sep (in this case a space).

Antisana answered 17/1, 2018 at 15:43 Comment(0)
C
3
from nltk.util import ngrams

text = "Hi How are you? i am fine and you"

n = int(input("ngram value = "))

n_grams = ngrams(text.split(), n)

for grams in n_grams :

   print(grams)
Caslon answered 17/7, 2019 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.