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
How do ethics affect people?
Mnenie [13.5K]
People are affected because ethics are suppose to be moral
5 0
2 years ago
If you wish to include a header or footer on all pages in a publication, you will need to insert this by navigating to the _____
Marina86 [1]
If you wish to include a header or footer on all pages in a publication, you will need to insert this by navigation to the master page.
7 0
3 years ago
Read 2 more answers
What is the output of the following code:
Zielflug [23.3K]

Answer:

Explanation:

print(list1)

['C++', 'JAVA', 'ASP.PHP']

[2:-2]

Gets 2 position(C++) to -2 position(ASP.PHP), JAVA is in the Middle

print(list2)

[]

Gets middle of C++(nothing)

print(list3)

['Python', 'JAVA', 'MySQL']

Get first, jump 3, get first...

'get', 'any', 'any', 'get', 'any', 'any', 'get', ...

7 0
2 years ago
Video Fundamentals
Tresset [83]

Answer:

1) a link, original, a pointer.

2) The media browser can help you by providing the very quick accessibility to all of your assets like iTunes songs, your movies in the movie folder, and this makes your file browsing experience simple while you edit the files.

You can leave the browser open as well as docked quite in the same manner as the other panel.

3) You need to display the Voice-over Record button. Now select your track in the timeline where you want to add the voice-over.

Now you need to go to the Timeline, and click on the Settings button, and finally select the Customize Audio Header.

Now you will see the Button Editor dialog box appearing, and now you need to drag and drop the microphone button to the required audio track, and finally, you need to click on OK.

4) While you are importing the audio or video to the Premiere Pro, it computes versions of such files, which it can readily use for quicker performance. And these are being termed as the Media cache files. And they are being saved in the Media cache files folder.

Explanation:

Please check the answer.

6 0
3 years ago
Alonzo collects bugs. He has created a fascinating display of large insects. Collecting bugs is a(n) _____ to Alonzo.
-Dominant- [34]
The answer is hobby. He does it because he enjoys it. There is no information of him being told or paid to collect bugs, so it is a hobby.
3 0
3 years ago
Read 2 more answers
Other questions:
  • Write an expression to print each price in stock_prices. sample output for the given program: $ 34.62 $ 76.30 $ 85.05
    6·1 answer
  • What was the first browser that allowed for graphics to be viewed on the web?
    14·2 answers
  • On other questions, how do I write my own question, without commenting on others questions? It is only allowed two, I think, the
    13·1 answer
  • Write the definition of a function named printstarbucks that receives a non-negative integer n and prints a line consisting of n
    6·1 answer
  • Consider a channel that can lose packets but has a maximum delay that is known. Modify Protocol rdt2.1 to include sender timeout
    11·1 answer
  • Fre.....ee p......oint....s
    15·2 answers
  • Using range(1,101), make two list, one containing all even numbers and other containing all odd numbers. How can I do this on Py
    15·1 answer
  • Create a mobile app plan using PowerPoint slides to show mock-ups of screens,
    6·1 answer
  • A(n) ____________________ key is a key that is not reused, but rather is only used once, thus improving security by reducing the
    5·1 answer
  • what option can be used to create vpn connections that can be distributed to users' computers so that vpn clients do not have to
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!