Skip to content

Commit 6842a19

Browse files
Merge pull request avinashkranjan#2921 from smty2018/nlp3
N-Grams Generation Script using NLTK(NLP)
2 parents 2e09f85 + 1612144 commit 6842a19

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

N-Grams NLP/ReadMe.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
N-Grams Generation Script using NLTK
2+
3+
This Python script allows you to generate N-Grams from a given sentence using the `nltk` library. N-Grams are contiguous sequences of n items from a given text.
4+
5+
Requirements:
6+
7+
pip install nltk

N-Grams NLP/script.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from nltk import ngrams
2+
from nltk.tokenize import word_tokenize
3+
import nltk
4+
nltk.download('punkt')
5+
6+
def gen_ngrams(txt, n):
7+
tokens = word_tokenize(txt)
8+
ngs = list(ngrams(tokens, n))
9+
return ngs
10+
11+
def main():
12+
u = input("Enter a sentence: ")
13+
n = int(input("Enter the value of n for n-grams: "))
14+
15+
ngs = gen_ngrams(u, n)
16+
17+
print(f"Input Sentence: {u}")
18+
print(f"{n}-grams: {ngs}")
19+
20+
if __name__ == "__main__":
21+
main()

0 commit comments

Comments
 (0)