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
g Assignment 8: Driving costs Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon as input, a
zavuch27 [327]

Answer:

miles_gallon = float(input("Enter car's miles/gallon: "))

dollars_gallon = float(input("Enter gas dollars/gallon: "))

print("Gas cost for 20 miles is $", (20 / miles_gallon) * dollars_gallon)

print("Gas cost for 75 miles is $", (75 / miles_gallon) * dollars_gallon)

print("Gas cost for 500 miles is $", (500 / miles_gallon) * dollars_gallon)

Explanation:

*The code is in Python.

Ask the user to enter the car's miles/gallon and gas dollars/gallon

Calculate the gas cost for 20 miles, divide 20 by miles_gallon and multiply the result by dollars_gallon, then print it

Calculate the gas cost for 75 miles, divide 75 by miles_gallon and multiply the result by dollars_gallon, then print it

Calculate the gas cost for 500 miles, divide 500 by miles_gallon and multiply the result by dollars_gallon, then print it

5 0
2 years ago
How does design influence the product's function?
r-ruslan [8.4K]
I think the answer is
8 0
3 years ago
Which technique records a signal with a video that uniquely identifies every frame of the tape using a time stamp in hours, minu
Nookie1986 [14]

Answer:

NTSC, Timecode

Explanation:

NTSC was used for television broadcasting in the USA, and some other countries. And it archives a video signal that exceptionally deciphers every frame of the tape with the assistance of the time stamp in unit time and frames. We have other options as well, which are PAL and SECAM. However, now and since 2013, these are being converted into the digital. The frame rate of the NTSC in the US was 29.97 fps. And this defines how good video you are going to receive.

And timecode is the signal that is recorded with the video that can quite exclusively figure out each frame of the tape with the help of the time stamp in unit time and frames.

3 0
3 years ago
What are the purpose of Keyframes,
Kobotan [32]

Answer:

In media production, a key frame or keyframe is a location on a timeline which marks the beginning or end of a transition. It holds special information that defines where a transition should start or stop. The intermediate frames are interpolated over time between those definitions to create the illusion of motion.

Explanation:

7 0
3 years ago
Screen reader software is an example of a(n) ____.
s2008m [1.1K]
<span>It is an example of an assistive output device. These are an example of assistive technology, which is any technology optimized to help the user mitigate the aspects of a disability. This may enable them to use the technology as intended, or it may also assist them in functioning in their everyday lives.</span>
3 0
3 years ago
Other questions:
  • Define the following term: - hue
    11·2 answers
  • NEED HELP NOW 25 POINTS WILL MARK BRAINLIEST!!!
    7·2 answers
  • Yellow and blue light are projected on a white screen. what color will the screen appear to be?
    6·2 answers
  • Where does the CPU store its computations
    6·2 answers
  • A. True
    8·1 answer
  • A ____ typically rests on the desk or other flat surface close to the user’s computer, and it is moved across the surface with
    13·1 answer
  • Typing which capitals and exclamation points in an email is an example of
    14·1 answer
  • Ian kno da answer tell me
    7·2 answers
  • Which is a linear presentation?
    10·1 answer
  • What is the most common knowledge computer programmers need in order
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!