1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Trava [24]
4 years ago
6

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-g

ram 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')]
Computers and Technology
1 answer:
ki77a [65]4 years ago
6 0

Answer:

Explanation:

Assuming input is a string contains space separated words,

like x = "a b c d" we can use the following function

def ngrams(input, n):

input = input.split(' ')

output = []

for i in range(len(input)-n+1):

output.append(input[i:i+n])

return output

ngrams('a b c d', 2) # [['a', 'b'], ['b', 'c'], ['c', 'd']]

If you want those joined back into strings, you might call something like:

[' '.join(x) for x in ngrams('a b c d', 2)] # ['a b', 'b c', 'c d']

Lastly, that doesn't summarize things into totals, so if your input was 'a a a a', you need to count them up into a dict:

for g in (' '.join(x) for x in ngrams(input, 2)):

grams.setdefault(g, 0)

grams[g] += 1

Putting all together

def ngrams(input, n):

input = input.split(' ')

output = {}

for i in range(len(input)-n+1):

g = ' '.join(input[i:i+n])

output.setdefault(g, 0)

output[g] += 1

return output

ngrams('a a a a', 2) # {'a a': 3}

You might be interested in
What is typeface
Mademuasel [1]
I’m so bored and hungry answer is c 3566
4 0
3 years ago
Read 2 more answers
State five fonction of THE operating system​
I am Lyosha [343]

Answer:

An operating system has three main functions: (1) manage the computer's resources, such as the central processing unit, memory, disk drives, and printers, (2) establish a user interface, and (3) execute and provide services for applications software.

Memory Management.

Processor Management.

Device Management.

File Management.

Security.

Control over system performance.

Job accounting.

Error detecting aids.

7 0
3 years ago
Edhesive coding practice 3.4​
allsm [11]

I'm sorry i have to go to sleep.. Can we continue tm?

In the morning

8 0
4 years ago
Read 2 more answers
Feistel proposed that we can approximate the ideal block cipher by utilizing the concept of a __________ cipher, which is the ex
sweet-ann [11.9K]

Answer:

product cipher Is the answer

5 0
2 years ago
Read 2 more answers
Write a program to print sum on first 10 natural numbers.
Kamila [148]

sum of 10 natural number is 55

4 0
3 years ago
Other questions:
  • Suppose that cells B1 through B100 of an Excel spreadsheet contain the quantity of units ordered on each of 100 different days.
    13·1 answer
  • What format must a document be saved in to share it between two different word processors
    11·1 answer
  • Why do you need to back up important data?
    5·2 answers
  • What are the biggest problems facing the criminal justice system in the area of computer crime
    10·2 answers
  • What is software? what are two types of software?​
    6·1 answer
  • What has happened (or is still happening) to make this speech occur? armageddon
    15·1 answer
  • Full meaning of LAN and WAN
    5·2 answers
  • How is video compression accomplished?
    14·1 answer
  • A major retailer wants to enhance their customer experience and reduce losses
    6·1 answer
  • Choose the term to complete the sentence.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!