Question

In: Computer Science

Write a simple JAVA program to understand natural language. The user will enter the input following...

Write a simple JAVA program to understand natural language.

The user will enter the input following the format:

Name came to City, Country in Year.

For example:

Chris came to Bangkok, Thailand in 2009.

The user will follow the exactly the same formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format:

Name stay in City for X year(s). City is in Country.

For example

Chris stay in Bangkok for 11 years. Bangkok is in Thailand.

*  note X = 2020-Year.

Example of the output to illustrate the expected behavior of your program.

Hint: you may use Integer.parseInt() to convert a string to an integer.

Translator Program

---------------------------------------------------------

Please enter the input sentence (press q to exit):

Chris came to Bangkok, Thailand in 2009.

Chris stays in Bangkok for 11 years. Bangkok is in thailand.

Please enter the input sentence (press q to exit):

Lee came to Paris, France in 2000.

Lee stay in Paris for 20 years. Paris is in France.

Please enter the numbers along operation (press q to exit):

q

Thank you

Solutions

Expert Solution

Here is the answer for your question in Java Programming Language.

Kindly upvote if you find the answer helpful.

#######################################################################

CODE :


import java.util.Scanner;

public class NatiralLanguage {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String sentence,name = "",city = "",country = "";
int year,currentYear = 2020,i;
String words[];
do{
System.out.println("Please enter the input sentence (press q to exit):");
sentence = s.nextLine();
words = sentence.split(" ");
if(words.length > 1){
name = words[0];
city = words[3].substring(0,words[3].length()-1);
country = words[4];
year = Integer.parseInt(words[6].substring(0,words[6].length()-1));
System.out.println(name + " stay in " + city + " for " + (currentYear - year) + " years. " + city + " is in " + country + ".");
}
}while(sentence.length() != 1 && !sentence.equalsIgnoreCase("q"));
}
}

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

########################################################################

OUTPUT :

Any doubts regarding this can be explained with pleasure :)


Related Solutions

In this question, you are asked to write a simple java program to understand natural language....
In this question, you are asked to write a simple java program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Robin came to Montreal, Canada in 2009. Assume a perfect user will follow the exactly above formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a java program that does the following: a) The user will enter data such as...
Write a java program that does the following: a) The user will enter data such as client name and client balance. The user can stop inputting data by entering "stop". The program should store the user inputs as a data member of an array. The type of this array is a class named ClientData. Below the output should be displayed by the program. The parts in bold are inputs from the user and not hard coded in the program Client...
Write a java simple document retrieval program that first asks the user to enter a single...
Write a java simple document retrieval program that first asks the user to enter a single term query, then goes through two docuements named doc1.txt and doc2.txt (provided with assignment7) to find which document is more relevant by calculating the frequency of the query term in each document. Output the conclusion to a file named asmnt7output.txt in the following format (as an example for query “java” and “problem”). The percentages in parenthese are respective supporting frequencies. java: doc1(6.37%) is more...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Write a simple MIPS program that asks the user to input a string and then a...
Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT