malcolmgreaves / language-detection

Automatically exported from code.google.com/p/language-detection . Some after-the-fact modifications to get this working within sbt.
Apache License 2.0
5 stars 5 forks source link

Ability to bundle the profiles into a jar file #24

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I would like to bundle the language profiles in a jar file to reduce clutter.  
However, the DetectorFactory API is quite restrictive in what it allows you to 
pass as the location of the profiles.

If it were possible to pass in a URL, then I think this would be a lot more 
convenient.

Original issue reported on code.google.com by dan...@nuix.com on 12 Sep 2011 at 2:22

GoogleCodeExporter commented 9 years ago
DetectorFactory.loadProfile can be used as the below.
See also http://code.google.com/p/language-detection/issues/detail?id=9#c4

    DetectorFactory.loadProfile(new File(MyClass.class.getResource("profiles").toURI()));

I'll write it in a suitable document. Thanks.

Original comment by nakatani.shuyo on 13 Sep 2011 at 9:52

GoogleCodeExporter commented 9 years ago
This code gives me the following error when the profiles are in a jar:

java.lang.IllegalArgumentException: URI is not hierarchical

Original comment by dan...@nuix.com on 13 Sep 2011 at 11:10

GoogleCodeExporter commented 9 years ago
Where does your "MyClass" put? 
I am probably afraid that Java class can only load resouces in the jar which 
itself is belonging.

Original comment by nakatani.shuyo on 14 Sep 2011 at 2:36

GoogleCodeExporter commented 9 years ago
Actually it doesn't matter whether it is in the same jar or a different one.  
You just can't convert a jar URL/URI into a file. Here's a short demonstration:

grater:uri tx$ cat UriToFile.java 

import java.io.File;
import java.net.URI;
import java.net.URL;

class UriToFile {
    public static void main(String[] args) throws Exception {
        URL url = UriToFile.class.getResource("UriToFile.class");
        System.out.println("URL: " + url);
        URI uri = url.toURI();
        System.out.println("URI: " + uri);
        File file = new File(uri);
        System.out.println("File: " + file);
    }
}

grater:uri tx$ javac UriToFile.java
grater:uri tx$ java UriToFile
URL: file:/Users/tx/test/uri/UriToFile.class
URI: file:/Users/tx/test/uri/UriToFile.class
File: /Users/tx/test/uri/UriToFile.class

grater:uri tx$ jar cf uritofile.jar UriToFile.class
grater:uri tx$ java -classpath uritofile.jar UriToFile
URL: jar:file:/Users/tx/test/uri/uritofile.jar!/UriToFile.class
URI: jar:file:/Users/tx/test/uri/uritofile.jar!/UriToFile.class
Exception in thread "main" java.lang.IllegalArgumentException: URI is not 
hierarchical
    at java.io.File.<init>(File.java:363)
    at UriToFile.main(UriToFile.java:12)
grater:uri tx$ 

Original comment by dan...@nuix.com on 14 Sep 2011 at 6:07

GoogleCodeExporter commented 9 years ago
Umm, I see. You are right.
Then I guess it is the best solution that loadProfiles receive a array of 
profile data(JSON strings). How do you think?

Original comment by nakatani.shuyo on 15 Sep 2011 at 3:52

GoogleCodeExporter commented 9 years ago
That would be ideal, I think.  Then we could load it any way we want (including 
putting them all in one file even, perhaps.)

Original comment by dan...@nuix.com on 15 Sep 2011 at 4:55

GoogleCodeExporter commented 9 years ago
I've supported loadProfiles(List<String>) at trunk of the repository.

http://code.google.com/p/language-detection/source/browse/#svn%2Ftrunk%2Flib

Original comment by nakatani.shuyo on 20 Sep 2011 at 10:57