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 result would support the particle theory of light?
andriy [413]

This question is related to the emission of electrons when light shines on an metal. (Photoelectric effect)

With the classical theory, it was thought that increasing or decreasing the intensity of light would increase or decrease the kinetic energy of the electron.,

Another expected result was that time gap between when light strikes the metal and when electrons are ejected, would depend on the intensity of such light.

However, the experimental results were not consistent with the particle theory of light. So your answer is A. 

Later on this effect was correctly explained by Einstein based on Planck's findings.


8 0
3 years ago
Read 2 more answers
You are an IT technician for your company. One of your employees has a computer that continually reboots when it is powered on.
Elan Coil [88]

Answer:

D: Use a multimeter to test the power supply

Explanation:

We want to determine whether the power supply is causing the reboots. Now, the most ideal thing to do will be to connect a multimeter to help in testing the source of power supply. This is because a multimeter measures the major factors in power which are voltage, current and resistance and as such it is therefore a standard diagnostic tool used widely by technicians in the electrical & electronic industries.

5 0
3 years ago
What type of organizational structure would you want to use for this company (by function, by process, by product, and so on)? E
zhuklara [117]
I would prefer a cotton company because we're all can make cloths.
3 0
3 years ago
What did research conducted in 2009 at Carnegie University Mellon predict?
riadik2000 [5.3K]

A.Information on social networking sites can give most or all digits of a person’s social security number.


5 0
3 years ago
Vẽ sơ đồ lắp đặt gồm 2 cầu chì, 1 ổ điện, 2 cực điều khiển 2 đèn mắc song song
Nookie1986 [14]

Answer:

okokikhjhghjjjkkkkkokoloo

7 0
3 years ago
Other questions:
  • What are the 7 basic components found in a computer tower
    5·2 answers
  • Ethan wants to change the font in his document. He should _____.
    8·2 answers
  • WHERE WAS THE CHEESEBURGER INVENTED?
    9·1 answer
  • Who is responsible for ensuring the security of business systems and developing strategies and safeguards against attacks by hac
    9·1 answer
  • Now tell me how be rich like Bill Gates
    6·1 answer
  • Write a c++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the
    13·1 answer
  • Suppose the fixed width of an aside element in a fixed layout is 300 pixels, and the fixed width of its parent element (body) is
    15·1 answer
  • Use the drop-down menus to complete each statement. Two main versions of Outlook are the desktop app and the app. The has limite
    15·2 answers
  • Help me to solve please​
    8·2 answers
  • assuming the default gateway is connected to the internet, what type of internet access would this server have?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!