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
When listing columns in the select list, what should you use to separate the columns?
Gala2k [10]
The answer is commas.  <span>When listing columns in the select list, commas should be used to separate the columns.</span>
6 0
4 years ago
Puung.<br>f. Differentiate between second and third Generation Computer<br>IG ANSWER QUESTIONS​
Papessa [141]

Answer:

Second generation computers were based on transistors, essentially the same as first generation computers, but with the transistors replacing the vacuum tubes / thermionic valves.

Third generation computers used printed circuit boards to replace much of the wiring between the transistors. This had the advantage that components could be mass produced, reducing the number of errors because of incorrect wiring and making the component boards replacable.

6 0
3 years ago
Many mobile devices can perform internet searches and other tasks via
OleMash [197]
Cellular Network?
Browser?

there are a lot of things that allow mobile devices to do internet searches.
7 0
3 years ago
Ben wants to create a Book class that has an instance variable serialNumber. Where should he declare the instance variable
MrMuchimi

Answer:

within the Book class but needs to also be outside of any methods.

Explanation:

If Ben is creating an entire Book class then the instance variable needs to be within the Book class but needs to also be outside of any methods. If Ben places the variable inside a method it can only be used by that method and therefore becomes an instance variable of that method and not the class. By creating it inside the class and outside the methods it can be used every single time a Book object is created. Therefore, creating an instance variable of serialNumber every time.

4 0
3 years ago
By what date must federal taxes typically be filed and sent to the IRS?
marishachu [46]
April 15th of each annual year
6 0
4 years ago
Other questions:
  • Press ____ to select the entire worksheet.<br> F1<br> F4<br> ALT+A<br> CTRL+A
    8·1 answer
  • To change a selected shape’s height or width to a specific value, type the value in the Height or Width text boxes on the
    14·1 answer
  • What command prompts should be used to assign an IP address to:
    9·1 answer
  • Anyone wanna play roblox?? My user is Tsvetok_luis
    11·2 answers
  • Please Help!
    14·2 answers
  • The function below takes a single argument: data_list, a list containing a mix of strings and numbers. The function tries to use
    6·1 answer
  • Please tell fast plzzzzzz.​
    7·1 answer
  • Does technology shape society or does society shape technology?
    14·1 answer
  • Is it a way to get robuc 4 free
    6·2 answers
  • What does the following code print? time_of_day = ["morning", "afternoon", "evening"] for word in time_of_day: print "Good " + w
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!