forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngram.py
23 lines (17 loc) · 625 Bytes
/
ngram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
https://en.wikipedia.org/wiki/N-gram
"""
defcreate_ngram(sentence: str, ngram_size: int) ->list[str]:
"""
Create ngrams from a sentence
>>> create_ngram("I am a sentence", 2)
['I ', ' a', 'am', 'm ', ' a', 'a ', ' s', 'se', 'en', 'nt', 'te', 'en', 'nc', 'ce']
>>> create_ngram("I am an NLPer", 2)
['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er']
>>> create_ngram("This is short", 50)
[]
"""
return [sentence[i : i+ngram_size] foriinrange(len(sentence) -ngram_size+1)]
if__name__=="__main__":
fromdoctestimporttestmod
testmod()