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
Which technology can allow a single ground-based telescope to achieve images as sharp as those from the Hubble Space Telescope?
Ganezh [65]

Answer:

The correct answer to the following question will be "Adaptive Optics".

Explanation:

  • AO (Adaptive Optics) is a technique used to enhance optical system performance by reducing the impact of incoming gravitational wave distortions by compressing a mirror to compensate for the distortion.
  • It operates by calculating and compensating for defects in a wave-front with a system that corrects these errors as a deformable mirror or even a liquid crystal collection.
  • It is a technique that can make it possible for a single ground-based telescope to get images as clear as that of the Hubble Space Telescope.
  • Certain methods can achieve power resolution that exceeds the limit set by atmospheric distortion, for example, Aperture synthesis, Lucky imaging, and Speckle imaging.

Therefore, Adaptive Optics is the right answer.

7 0
4 years ago
Which of the following are considerations in e-commerce and e-government Internet sites? Check all of the boxes that apply.
vovangra [49]

Answer:

protection of sensitive information

Explanation:

6 0
3 years ago
How to get the level 100 tier in fortnite please help <br><br><br><br> pssss. just a troll m8 ;)
inysia [295]

Answer:

Do missions : )

Explanation:

Fortnite is trash !

5 0
4 years ago
Read 2 more answers
Or operator of boolean algebra is denoted by:<br>*<br>+<br>-<br>.​
OlgaM077 [116]

Answer:

or operator is denoted by + sign

7 0
3 years ago
Why does the font­family css specification take a list rather than a single font name?
azamat
You can't guarantee that the computer displaying the page has all the fonts that you want. Generally for font-family (note the hyphen!) you list:

the font you REALLY want
a font that's similar and more common (you HOPE it has)
the family (like serif or san-serif)
5 0
4 years ago
Other questions:
  • Andy designed a web page for a product-oriented industry using CSS properties. He used several HTML tags to create the web page.
    6·1 answer
  • How have computers changed overtime
    13·2 answers
  • What important Apple computer (with a GUI OS) was introduced in 1984? Apple I?
    13·1 answer
  • With a C program (memory map), data in _____ is initialized by the kernel to arithmetic 0 or null pointers before the program st
    7·1 answer
  • Define
    15·1 answer
  • Match the items with their respective descriptions. organizes files make files easily searchable keeps track of file creation an
    14·1 answer
  • Using complete sentences post a detailed response to the following.
    13·1 answer
  • Who is the founded the java computer language?​
    5·1 answer
  • Which of the following is a software tool used to manage data for a project in a logical and hierarchical order?
    13·1 answer
  • Write a static method named isSorted that takes an array of real numbers as a parameter and that returns true if the list is in
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!