subject

Write a function ngrams(n, tokens) that produces a list of all n-grams of the specified size from the input token list. Each n-gram should consist of a 2-element tuple (context, token), where the context is itself an (n-1)-element tuple comprised of the n-1 words preceding the current token. The sentence should be padded with n-1 "" tokens at the beginning and a single "" token at the end. If n = 1, all contexts should be empty tuples. You may assume that n ≥ 1.

>>> ngrams(1, 'abc')
[('~', 'a'), ('a', 'b'), ('b', 'c')]
>>> ngrams(2, 'abc')
[('~~', 'a'), ('~a', 'b'), ('ab', 'c')]

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 24.06.2019 07:00
You are most likely to automatically encode information about
Answers: 1
question
Computers and Technology, 24.06.2019 19:50
How to unblock on chrome book? ?
Answers: 1
question
Computers and Technology, 25.06.2019 08:20
E-commerce builds on traditional commerce by adding the flexibility that networks offer and the availability of the internet. - true or false
Answers: 1
question
Computers and Technology, 25.06.2019 17:20
Athird of the sum of a number and ten is thirty
Answers: 1
You know the right answer?
Write a function ngrams(n, tokens) that produces a list of all n-grams of the specified size from th...
Questions
question
Mathematics, 05.04.2021 03:30
Questions on the website: 13722367