Add a language to my search engine, arabic letters in Eclipse - java

I want to do a search engine in arabic, and i have already a code for searching in english I had just to change the Analyzer but when i wrote in arabic in the console, I change to UTF-8 and i get 0 found so I think that eclipse give the arabic word to the query in a code , and the query doesn't recognize this code, my question is how can I do to make the arabic word readable to the query?
QueryParser parser = new QueryParser(Version.LUCENE_30,
"contents", new ArabicAnalyzer(Version.LUCENE_30));

Try looking in project properties, in the "Resource" section. Set your text file encoding to UTF-8 & see if that fixes the problem. I am assuming you have the right fonts already installed.

I believe you are reading characters like this:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
String token = reader.readLine();
System.out.println(token);
} catch (IOException e) {
e.printStackTrace();
}
In that case character encoding is exactly the same as current system code page (at least in Windows). The problem is, Eclipse will allow you to paste Arabic letters to its console window but will lose information during the process. I am not sure if setting System code page (in OS Regional options) to windows-1256 will help but it could. I have tried to pass Charset.forName("windows-1256") as a second parameter to InputStreamReader and then input something with Arabic keyboard but it does not work.
OK, but we are not so helpless after all. Since that is meant for testing (right?), you can follow one of two approaches to fix the problem:
Use some basic Swing UI (JFrame + JTextField + JLabel and maybe some button)
Provide unescaping mechanism and enter characters as code points (i.e. \u0629)
The best fix would be to fix Eclipse (which is broken) and for example implement Console (System.console()) but I am not so sure if they would accept such patch.

You can try to give Unicode symbols in the console instead of Arabic characters.
Use a converter like this one to convert your Arabic text to Unicode symbols.

Related

Reading UTF-8 encoded text from InputStream

I'm having problems reading all Japanese/Chinese characters from an input stream.
Basically, I'm retrieving a JSON object from an API.
Below is my code:
try {
URL url = new URL(string);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),StandardCharsets.UTF_8));
result = br.readLine();
br.close();
} catch(Exception e) {
}
For some reason, not all characters are read by the input stream. What could be the problem?
To be specific, some characters appear when I print them out in the console, while some appear as black boxes with question marks. Also, there are no black boxes with questions marks when I check the actual JSON object through a browser.
What you see when "printing to a console" really has nothing to do with whether data was read or not, but has everything to do with the capabilities of your console.
If you are fetching data from a URL, and you know for sure that the bytes you have fetched represent UTF-8 encoded text, and the entire data fits on one line of text, then there is no reason why your code should not work.
It sounds like you are not sure things work because you are trying to print text to your console. Perhaps your console is not set to render UTF-8 encoded text? Perhaps your console font does not have enough glyphs to cover the font?
Here are two things you can try:
Instead of writing the text to your console, save it to a file. Then use a command like hexdump -C (on a *nix system, I have no idea how to do that in Windows) and look at the binary representation to make sure all your expected characters are there.
Save your data to a text file, then open it in a web browser, since browsers probably have much richer font support than a console.
If you still suspect you've read the remote data incorrectly, you can run your retrieved text through a JSON validator, just to make sure.
Try this instead: "ISO-8859-1".

Java adds spaces when reading in a line?

So I'm at my wit's end with this program. I'm reading in from a text file in Java. Barring everything that I do with the string once I have it, this is the bare minimum code to be shown.
while ((lineIn = myReader.readLine()) != null) {
System.out.println("LineIn: \""+lineIn+"\"");
System.out.println("Length: "+lineIn.length());
}
What it prints out, however, is very strange indeed. The line should read:
001 2014/06/09 09:40:24 0.000
But this is what I get:
LineIn: "�2�6�1�8� �2�0�1�4�/�0�7�/�1�0� �2�3�:�1�5�:�0�3� �0�.�0�0�0�"
Length: 61
On Stack Overflow it actually shows up fine. You may be able to copy and paste the "LineIn: etc" into your address bar and see there are little invisible spaces in the numbering. I have no idea why those are there, what they are, and where Java is getting them from. Opening the document it's sourced from in a simple text editor shows no such spacing, and copy+pasting from the text editor into the browser address bar has no superfluous spacing either. It's very peculiar and I hope someone can offer insight. I'm pulling out my hair here.
It looks like you're reading UTF-16 data as if it had an 8-bit encoding.
If you construct a java.io.InputStreamReader, you can specify the input text charset such as "UTF-16".
It could be due to the formatting and encoding that your reader is using, try using Scanner instead.
Java certainly is not doing that, it might be UTF-16 encoded file. Can you upload the file or a small part of it somewhere?

Unicode is not displaying Greek characters

I'm new to java programming and for our second assignment we were asked to make a chart showing Greek characters and their English equivalents.
When I enter them, for example System.out.println ("\u0391") a question mark is displayed when i run the program.
How do I fix this?
The problem isn't that your use of unicode is wrong, but that the output device you are using (maybe windows console) isn't capable to display the characters.
Try running it in an IDE, after making sure you configure it to use a font that contains greek letters.
Or write the list to a file and open it in some reasonable texteditor (even word should work)
What your problem?
If u put:
System.out.println("\u0391 A");
System.out.println("\u0392 B");
you will get... you want to make an list to compare? is it?

Trying to parse ical file with ical4j problems with newlines after description property

im trying to parse the ical here: http://www.dsek.se/kalender/ical.php?person=&dsek&tlth
with this code:
URL url=new URL("http://www.dsek.se/kalender/ical.php?person=&dsek&tlth");
calendar=Calendars.load(url);
well, that is basicly the gist of the calendar code.
But im getting problems, I think somehow the "DEDSCRIPTION: text" gets transformed into "DESCRIPTION:
newLine text" before getting parsed and thus the parser wont work I think.
The problem only appears on the rows where after DESCRIPTION: there is a whitespace, the rows that look like "DESCRIPTION:text" work fine. I've also tested another file that don't have these newlines and that file works fine.
So im guessing that maybe it is some kind of character encoding problem? that the URL object changes the encoding of the file? the character encoding on the file is ISO-8859-15
Or is it just that they have written the file with newlines after "DESCRIPTION:"? And if that is the case how do I solve this? :S
if it matters somehow the the app is running on android :)
The issue is that the DESCRIPTION field does not follow proper line folding. See https://www.rfc-editor.org/rfc/rfc5545#section-3.1
So wherever you have something like
DESCRIPTION:
some text
you should have instead
DESCRIPTION:
some text
(please note the space after the linefeed and before the text) or simply
DESCRIPTION:some text
You might be able to get away with a simple Regex to fix that.
Then the file is also missing line folding for those DESCRIPTION that have a length greater than 75 characters. iCal4j should be fine with that.
Finally, regarding the character encoding, UTF-8 is the default (other encoding are actually deprecated. see https://www.rfc-editor.org/rfc/rfc5545#section-6) so the Calendars.load() method just assumes UTF-8.
So, you will have to
Reader r = new InputStreamReader(url.openStream(), "ISO-8859-15");
CalendarBuilder builder = new CalendarBuilder();
Calendar calendar = builder.build(r);
Of course, the best solution would be for the authors of those ics files to fix those issues (line folding AND content encoding) on their side.

Print arabic string in java

I'm trying to display arabic text in java but it shows junk characters(Example : ¤[ï߯[î) or sometimes only question marks when i print. How do i make it to print arabic. I heard that its something related to unicode and UTF-8. This is the first time i'm working with languages so no idea. I'm using Eclipse Indigo IDE.
EDIT:
If i use UTF-8 encoding then "¤[ï߯[î" characters are becoming "????????" characters.
For starters you could take a look here. This should allow you to make Eclipse print unicode in its console (which I do not know if it is something which Eclipse supports out of the box without any extra tweaks)
If that does not solve your problem you most likely have an issue with the encoding your program is using, so you might want to create strings in some manner similar to this:
String str = new String("تعطي يونيكود رقما فريدا لكل حرف".getBytes(), "UTF-8");
This at least works for me.
If you embed the text literally in the code make sure you set the encoding for your project correctly.
This is for Java SE, Java EE, or Java ME?
If this is for Java ME, you have to make custom GlyphUtils if you use LWUIT.
Download this file:
http://dl.dropbox.com/u/55295133/U0600.pdf
Look list of unicode encoding..
And look at this thread:
https://stackoverflow.com/a/9172732/1061371
in the answer (post) of Mohamed Nazar that edited by bernama Alex Kliuchnikau,
"The below code can be use for displaying arabic text in J2ME String s=new String("\u0628\u06A9".getBytes(), "UTF-8"); where \u0628\u06A9 is the unicode of two arabic letters"
Look at U0600.pdf file, so we can see that Mohamed Nazar and Alex Kliuchnikau give example to create "ba" and "kaf" character in arabic.
Then the last point that you must consider is: "Make sure your UI support unicode(I mean arabic) character."
Like LWUIT not support yet unicode (I mean arabic) character.
You should make your custom code if you mean your app is using LWUIT.

Categories

Resources