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]
3 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]3 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
Does anyone know 7.1.3: Firework karel?
evablogger [386]

Answer:

Yess its from freelancer

6 0
2 years ago
Describe the different
____ [38]

Answer:

left thinks about girls and right is school

Explanation:

yes

8 0
2 years ago
Physical Geography of the Middle East
vagabundo [1.1K]

Answer:

Explanation:

He’s wright SS7G5 The student will locate selected features in

Southwestern Asia (Middle East).

a. Locate on a world and regional political-physical map:

Euphrates River, Jordan River, Tigris River, Suez Canal,

Persian Gulf, Strait of Hormuz, Arabian Sea, Red Sea, and

Gaza Strip.

b. Locate on a world and regional political-physical map the

nations of Afghanistan, Iran, Iraq, Israel, Saudi Arabia, and

Turkey

3 0
2 years ago
I have a bag of trail mix.half of the bag contains peanuts.1/4of the bag is chocolates candies, and 1/4of the bag is dried fruit
nika2105 [10]
A.) 1/2 Half the bags are peanuts so you have 1/2 a chance of selecting s peanut.
6 0
2 years ago
Read 2 more answers
What would be the most popular piece of technology on Earth?
spin [16.1K]

Answer:

the smart phone

Explanation:

It is the most widley used and accepted

8 0
3 years ago
Read 2 more answers
Other questions:
  • a computer that no longer works after having minor repair work done to it may have been damaged by ______.
    6·1 answer
  • How many unique ip addresses can be made in a fixed-length ip address system using 6 bits?
    8·1 answer
  • The accessibility of a website refers to which of
    6·1 answer
  • In C#Write the program SubscriptExceptionTest in which you use an array of 10 doubles. Write a try block in which you place a lo
    5·1 answer
  • A swimming pool has a length of 28 feet, a width of 17 feet, and a depth of 6 feet. How much water can the swimming pool hold?
    10·1 answer
  • Laptop computers use PCMCIA cards, another type of ________
    15·1 answer
  • PLEASE HELP WILL GIVE BRAINLIEST!!!’
    15·1 answer
  • A network technician attempts to ping www.example.net from a customer computer, but the ping fails. access to mapped network dri
    5·1 answer
  • processing C. have only one function, e.g. control traffic lights in a town, games machines 7 To create a formula, you first: A.
    13·1 answer
  • Is a device used to test the network connection.
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!