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...
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...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
Write a Java program to do the following: Ask the user to enter 10 first names...
Write a Java program to do the following: Ask the user to enter 10 first names (one word - no hyphen or apostrophe) using the keyboard. 1) Display the list of names one per line on the Console. 2) Display the names again, after eliminating duplicates using a HashSet (Your code MUST use HashSet).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT