Ask Java Expert


Home >> Java

Problem: k-Grams

In computational linguistics, a k-gram is a series of k consecutive words taken out of a piece of text. For example, in the text

                                         Mr. Owl ate my metal worm
there are four 3-grams, each of which is given below:

Mr. Owl ate

Owl ate my

ate my metal

my metal worm

k-grams are used extensively in computer science - especially in artificial intelligence and computational linguistics - and are at the heart of many techniques in machine translation.

Your job in this problem is to write a method

private ArrayList kGramsIn(ArrayList text, int k)

This method accepts two parameters - an ArrayList representing a piece of text and a num¬ber k. It should then return an Ar ray List (that is, an ArrayList of arrays of Strings) con¬taining all the k-grams in the text.

The text parameter to this method consists of an ArrayList containing all the words in the source text in the order in which they appear. For example, if the input text was "Mr. Owl ate my metal worm," the text parameter would be this ArrayList:

Mr. Owl ate my metal worm

Calling the above method on this text with k= 2 would produce an ArrayList containing the following 2-grams, each of which would be represented by a St ring [ ] of length two:

Mr. Owl
Owl ate
ate my
my metal
metal worm

Calling this method with k = 4 would produce an ArrayList containing these 4-grams, each of which would be represented by a St ring [] of length four:

Mr. Owl ate my
Owl ate my metal
ate my metal worm

In implementing this method, you can assume that the value of k will be greater than zero and no greater than the number of words in text.

Problem: Andy Warhol Paintings

The artist Andy Warhol produced a series of famous portraits made with only a small number of colors. Even with a limited color palette, the portraits are immediately recognizable and have since become iconic. In this problem, we'd like you to write a method that transforms a source image in the style of these prints.

Write a method

private Glmage andyWarholtze(GImage source, int[] palette) that accepts as input two parameters. The first parameter, source, will be an ordinary GImage. The sec¬ond parameter, palette, is an array of ints, each of which represents a color value (in the same way that each pixel in a G Image has its color represented by an int value).

This method should work as follows. For each pixel in the source image, you will change the color of that pixel to the color from palette that is "closest" to the original color. To help you determine which palette color is closest to a given pixel color, you can assume you have access to the following method:

private double stmtlarity0f(int colorl, int color2)

This method accepts as input two int values representing colors and returns a double between 0 and 1 that determines how similar those colors are. The higher the value returned, the more similar the colors are. When you're trying to figure out which color from the palette you should use to replace some pixel from the original image, you'll want to use the palette color that's most similar to the original pixel. (If two or more colors are tied for highest similarity, you can use any one of them.)

In implementing this method, you can assume that the palette has at least one color in it, but you should make no other assumptions about the image or the palette colors. There is no special significance to the order in which the colon appear in the palette array.

Write your answer to this problem on the next page, and feel free to tear out this page as a reference. Just for fim, here's the output of the method given as input a picture of a puppy:

Problem: Finding Coworkers

Suppose you have the organizational hierarchies of various companies showing who reports to who. For example, across three different companies, you might have this hierarchy information:

307_Artificial intelligence and computational linguistics.png

In this diagram, person e reports directly to person b, who reports directly to person a, who is the CEO of her company and therefore doesn't report to anyone.

This hierarchical information can be represented as a HashMapcSt ring , St ring> associating each per¬son with their direct superior (that is, the person one level above them in the hierarchy). As an excep¬tion, the heads of each organization are not present in the HashMap, since they don't report to anyone. For example, in the above picture, the key h would have value g, the key g would have value d, the key d would have value a, and a would not be a key in the map.

Given two different people you can tell whether they work for the same company by tracing from those people all the way up to their companies' CEOs, then seeing whether those CEOs are the same person. For example, in the above diagram, person e and person j work in the same company because both of them report (indirectly) to a. Similarly, person n and person k are in the same organization, as are per¬son c and person d. However, person j and person in are not in the same company, since person fs com¬pany is headed by person a and person m's company is headed by person k. Along similar lines, person and person b are in different companies, since person ar runs her own company (she doesn't report to anyone) and person b works at a company headed by person a.

Your job is to write a method

private boolean areAtSameCompany(String p1, String p2,
HashMap bosses)

that accepts as input two people and a HashMap associating each person with their boss, then reports whether pl and p2 work at the same company. You can assume the following:

• For simplicity, assume that everyone has just one boss, that each company has just one CEO, and that no person works at two or more companies.

• Everyone, except for the heads of companies, appear as keys in the HashMap. Therefore, if someone doesn't appear in the HashMap, you can assume that they are the head of a company.

• Names are case-sensitive, so "Karel" and "KAREL" are considered different people.

• pl and p2 may be the same person.

• There can be any number of levels of management between pl and her CEO and between p2 and his CEO, including 0, and that number of levels doesn't have to be the same.

Java, Programming

  • Category:- Java
  • Reference No.:- M91221537
  • Price:- $70

Priced at Now at $70, Verified Solution

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As