How to get a country's official name from a Java Locale? - java

In ISO-3166, when it comes to the country, there is a difference between "short name" and "full name".
For example, Denmark (link to iso.org reference : https://www.iso.org/obp/ui/#iso:code:3166:DK).
Its country code would be DK and DNK (alpha-2 and alpha-3), its numeric code is 208, its short name is 'Denmark', and its full name is 'the Kingdom of Denmark'.
For my current project, I have all the information I need except full name.
Does anyone know of a way to get that kind of data?

Java's in-built localization doesn't support official country names.
Here's the entry for Denmark in the localization data
DK=Denmark
You can use a REST API like https://restcountries.eu/, or I found this CSV file which seems to contain what you want. You will need to parse it.

Related

How to get list of nationalities in Java

Does someone know a way to retrieve a list of nationalities in Java ? Few precisions : I do not need a list of countries but a list of nationalities. I do not need a list of languages, but a list of nationalities. I've tried to twist Locale API, without result.
And icing on the cake, I need to display nationalities in a specific languages.For example, with Brazil, I need to display a 'brazilian' in english, a 'brésilien' in french and a 'brasilero' in spanish.
Does someone have an idea ?
Java doesn't contain a list of nationalities as far as I'm aware. The Locale class just gives a list of regions as stated in the javadoc. It would be helpful in your situation, but not for a lot.
From what i gather, you're going to have to create your own list of nationalities, and the name of the nationality in each language - not too hard.
To do the language part, internationalization will be helpful. It allows you to get the users region and set a language depending on where the user is from. It also allows formatting of numbers, text and dates.
Look at this stack overflow answer specifically for how to get the names of the nationalities in different languages. Basically, you create a different file for each language and inside the file are key-value pares. So the English file will look a bit like this:
nationality.english = English
nationality.german = German
nationality.russian = Russian
and the German file will be similar to:
nationality.english = Englisch
nationality.german = Deutsche
nationality.russian = Russisch
then depending on what language you want the nationalities displayed in, you just get the text from the language file using the key (e.g. nationality.russian).
To do the nationality part, you can create an enum that contains all the nationalities. For example:
public enum Nationalities{
ENGLISH,
GERMAN;
//And so on
}
See here if you are new to java enums.
You will probably want to add a bit more information in the enum class, such as the county and what the key for the nationality name is.
To pull it all together, you get the region of the user and set the language file for the region. Then for each listed nationality, you get the name of it from the language file.

How to predict correct country name for user provided country name?

I am planning to do some data tuning on my data.
Situation-I have a data which has a field country. It contains user input country names( It might contain spelling mistakes or different country names for same country like US/U.S.A/United States for USA). I have a list of correct country names.
What I want- To predict which closest country it is referring to. For example- If U.S. is given then it will change to USA(correct country name in our list).
Is there any way I can do it using Java or opennlp or any other method?
You can use Getty API . It will give you abbreviations of country name. Just play on this API.
OR
You can also use Levenshtein Distance to get most closest country name.
Try this out. Will help you.
You can try Google's auto complete location api to your text box or select.
if you will use this api then you will get google like auto complete intellisence while typing.
visit link
If you have the city or state information that is sanitized then you could do a look up of the country.
You could also define aliases in your list of country names and point the aliases to the preferred notation. For example, US, United States, USA all are aliases of U.S.A. You could make the program to append to alias database so that it improves as it is being used. You might have do multiple passes over the data and also certain amount of manual work is involved.

Using Stanford NER for extracting Address from a text document?

I was looking Stanford NER and thinking of using JAVA Apis it to extract postal address from a text document. The document may be any document where there is an postal address section e.g. Utility Bills, electricity bills.
So what I am thinking as the approach is,
Define postal address as a named entity using LOCATION and other primitive named entities.
Define segmentation and other sub process.
I am trying to find a example pipeline for the same (what are the steps in details required), anyone has done this before? Suggestions welcome.
To be clear: all credit goes to Raj Vardhan (and John Bauer) who had an interaction on the [java-nlp-user] mailing list.
Raj Vardhan wrote about the plan to work on "finding street address in a sentence":
Here is an approach I have thought of:
Find the event-anchor in a sentence
Select outgoing-edges in the SemanticGraph from that event-node
with relations such as *"prep-in" *or "prep-at".
IF the dependent value in the relation has POS tag as NNP
a) Find outgoing-edges from dependent value's node with relations such
as "nn"
b) Connect all such nodes in increasing order of occurrence in the
sentence.
c) PRINT resulting value as Location where the event occurred
This is obviously with certain assumptions such as direct dependency
between the event-anchor and location in a sentence.
Not sure whether this could help you, but I wanted to mention it just in case. Again, any credit should go to Raj Vardhan (and John Bauer).

Drools Guvnor: How do dates and times work?

I know Java and I know C#.
I am a noobie with the JBoss Drools "Guvnor" and just their DRL rules language in general. I need a little help on how to use dates and times correctly in the LHS of rules.
For example, I have to assure data quality in a health care system. Obviously, each patient has a DOB in their record and it is of type java.util.Date.
Okay, so say I want to ensure that the DOB in the data passed as Facts is indeed before today's date. I know, for instance, in C#, you can get the current date/time by saying DateTime.Now.
I want to add a rule in Guvnor that says (and this is pseudo-code):
WHEN
patient DOB is before NOW
THEN
mark patient as valid
My Patient fact is:
declare Patient
patientID: integer
firstName: text
lastName: text
dateOfBirth: java.util.Date
end
I also have a Dose fact, with a patientID field to link it to what patient got what dose:
declare Dose
doseID: integer
patientID: integer
administeredDate: java.util.Date
amount: integer
end
Also, say a medication is released on a certain year, 1995, and I want to also check that a dose for a given patient is not administered prior to that year.
How do I do these two? I've tried Googling and Googling but all I get is links to the (utterly-useless) Guvnor User Guide which is pretty badly-written in that it doesn't contain this basic information, i.e., how dates and times work and are written in DRL syntax.
Thank you.
As always there are more than one way to do this. Here is one.
function Date currentTime(){
// The content of this method is Java
return new Date();
}
RULE "my rule"
WHEN
patient : Patient( dateOfBirth < currentTime() )
THEN
// Everything in THEN part is Java
patient.setValid(true); // Notice I added a valid field into the fact type
update(patient);
END
The other rule you asked for
Also, say a medication is released on a certain year, 1995, and I want to also check that a dose for a given patient is not administered prior to that year.
RULE "Second Rule"
WHEN
patient : Patient()
not Dose( patientId == patient.patientId, administeredDate < 01-Jan-1995 ) // The date format can be changed if you want to.
WHEN
System.out.println( "Patient " + patient.getFirstName + " " +patient.getLastName() + " was not given a dose before." );
END
The Guvnor user guide does not cover the language basics. You can find them from here. Guvnor is meant to provide you guided editors for writing the DRL or storing the files containing DRL. You check what the guided rule would look like if it was written in DRL by pressing the "View Source" button that is in each asset editor that produces DRL.

Localization design pattern in android

In my database I have a table containing localized cities.
Cities
_id |name_en |name_de |name_it
0 |Rome |Rom |Roma
1 |Munich |München |Monaco
...
Now I want show a ListView where each line exists of all names started by the name in the users language. Also the whole list should be sorted by the city in the users language.
Which is the right design-pattern for this kind of problem?
This is quite a broad question, but here's one general approach:
Get the user's current language ( Get the current language in device )
Query your database with this language code
Bind the returned Cursor to your ListView
Please post your relevant code if you want specific help.
Obviously you need to decide what column to use for SQL request (for both stating which column to retrieve and by which column to sort). So your column names should be public constants. And you need to have a method to return a column name (one of the constants) depending on the current device locale. Use one of the constants as a fallback if the locale does not match any known for the application locales.

Categories