Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1612144

Browse files
Add files via upload
1 parent 08b7bdd commit 1612144

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)

AltStyle によって変換されたページ (->オリジナル) /