2013-01-20 17 views
5

मैं एंड्रॉइड में एक शब्द गेम लिख रहा हूं। यह मेरा पहला ऐप है इसलिए मेरा ज्ञान लगभग अस्तित्व में नहीं है।एंड्रॉइड ऐप में WordNet dict फ़ाइलों को एक्सेस करें

मैं वर्डनेट शब्दकोश तक पहुंचने के लिए जेडब्ल्यूआई का उपयोग करना चाहता हूं। इसके लिए वर्डनेट शब्दकोश के फ़ाइल पथ को निर्दिष्ट करने की आवश्यकता है।

जो मैंने पढ़ा है, उससे एंड्रॉइड "संपत्ति" एक साधारण फ़ाइल पथ के माध्यम से उपलब्ध नहीं हैं, लेकिन जेडब्ल्यूआई को वर्डनेट शब्दकोश एपीआई शुरू करने की आवश्यकता है जो शब्दकोश फ़ाइलों के डिस्क स्थान पर एक यूआरएल है।

तो, कार्रवाई का सबसे अच्छा तरीका क्या है? क्या मुझे एंड्रॉइड डिवाइस पर एक ज्ञात फ़ोल्डर में स्टार्टअप-टाइम पर संपत्तियों की प्रतिलिपि बनाना चाहिए? मैं एक बेहतर तरीके से नहीं सोच सकता लेकिन यह मेरे लिए पूरी तरह बेवकूफ लगता है।

कोई भी मदद कृतज्ञता से प्राप्त हुई।

+1

JWI केवल फ़ाइल पथ को इनपुट के रूप में स्वीकार करता है, न कि जावा स्ट्रीम? वाह। यह वास्तव में मामला है अगर यह एक बहुत खराब डिजाइन लाइब्रेरी है। –

+0

जेडब्ल्यूआई एक निर्देशिका की तलाश करता है और फिर फाइलों को खोलता है, मुझे विश्वास है। – sanbikinoraion

+0

मैं इसके बजाय जेडब्ल्यूआई मेलिंग सूची पर एक प्रश्न पोस्ट करूंगा कि क्या उनके पास स्ट्रीम इंटरफ़ेस है (या केवल एपीआई देखें)। यदि नहीं तो मुझे लगता है कि आपको इसे कहीं भी भंडारण में कॉपी करना होगा। –

उत्तर

0

मैं हालांकि असफल (हालांकि और नहीं एंड्रॉयड एक घाट webapp के लिए) एक ही समस्या है और उन दो दृष्टिकोण की कोशिश की,:

JWNL.initialize(this.getClass().getClassLoader().getResourceAsStream("wordnet_properties.xml"); 
dict = Dictionary.getInstance(); 

यहाँ यह सफलतापूर्वक wordnet_properties.xml लोड करता है, लेकिन यह शब्दकोश है जो उपयोग नहीं कर सकते गुण फ़ाइल द्वारा इंगित किया गया।

सीधे फ़ोल्डर शब्दकोश का उपयोग करना:

String dictPath = "models/en/wordnet/dict/"; 
URL url = this.getClass().getClassLoader().getResource(dictPath); 
System.out.println("loading wordnet from "+url); 
dict = new RAMDictionary(url, ILoadPolicy.NO_LOAD); 

यहाँ मैं प्राप्त शब्दकोश यूआरएल jar:file:/home/myusername/.m2/repository/package/1.0-SNAPSHOT/commons-1.0-SNAPSHOT.jar!/models/en/wordnet/dict/ किया जाना है। हालांकि जार प्रोटोकॉल को स्वीकार नहीं करता WordNet और मुझे त्रुटि देता है:

java.lang.IllegalArgumentException: URL source must use 'file' protocol 
    at edu.mit.jwi.data.FileProvider.toFile(FileProvider.java:693) 
    at edu.mit.jwi.data.FileProvider.open(FileProvider.java:304) 
    at edu.mit.jwi.DataSourceDictionary.open(DataSourceDictionary.java:92) 
    at edu.mit.jwi.RAMDictionary.open(RAMDictionary.java:216) 

मेरा अगला जांच RAMDictionary करने के लिए एक उपवर्ग या कुछ इसी तरह बनाने के लिए किया जाएगा, कृपया मुझे बताओ अगर तुम इस बीच में एक समाधान मिल गया है।

पीएस: मैंने अभी डेवलपर को मेल के लिए फाइलप्रोवाइडर को फिर से लिखने की कोशिश करने के बाद एक मेल मांगने के बाद लिखा है, लेकिन एक या दो घंटे बाद मैंने छोड़ दिया क्योंकि कोड इतना अन्य कोड कहता है जो केवल फाइलों के साथ काम करता है । मैं तुम्हें सूचित करते रहूंगा!

पीपीएस .: मुझे डेवलपर से एक जवाब मिला है कि यह धाराओं के साथ मुख्य रूप से संभव नहीं है क्योंकि वे आवश्यक यादृच्छिक पहुंच प्रदान नहीं करते हैं। हालांकि, उन्होंने वास्तव में आवश्यक होने पर रैम में इसे लोड करने के लिए एक समाधान को लागू करने की पेशकश की, लेकिन यह लगभग 500 एमबी का उपयोग करेगा और मुझे लगता है कि एंड्रॉइड ऐप्स के लिए बहुत अधिक है, इसलिए मुझे लगता है कि इसे कहीं भी अनपैक करना अभी भी सर्वोत्तम है।

पुनश्च: यहाँ मेरी unpacking समाधान (आप लकड़हारा बयानों के साथ System.out.println बयानों की जगह ले सकता है अगर आप प्रवेश का उपयोग करें या यदि आप उन्हें पसंद नहीं है उन्हें निकालने के लिए) है:

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URISyntaxException; 
import java.util.Enumeration; 
import java.util.jar.JarEntry; 
import java.util.jar.JarFile; 

/** Allows WordNet to be run from within a jar file by unpacking it to a temporary directory.**/ 
public class WordNetUnpacker 
{ 
    static final String ID = "178558556719"; // minimize the chance of interfering with an existing directory 
    static final String jarDir = "models/en/wordnet/dict"; 

    /**If running from within a jar, unpack wordnet from the jar to a temp directory (if not already done) and return that. 
    * If not running from a jar, just return the existing wordnet directory. 
    * @see getUnpackedWordNetDir(Class)*/ 
    static File getUnpackedWordNetDir() throws IOException 
    {return getUnpackedWordNetDir(WordNetUnpacker.class);} 

    /**If running from within a jar, unpack wordnet from the jar to a temp directory (if not already done) and return that. 
    * If not running from a jar, just return the existing wordnet directory. 
    * @param clazz the class in whose classloader the wordnet resources are found. 
    * @see getUnpackedWordNetDir()**/ 

    static File getUnpackedWordNetDir(Class clazz) throws IOException 
    { 
     String codeSource = clazz.getProtectionDomain().getCodeSource().getLocation().getPath(); 
     System.out.println("getUnpackedWordNetDir: using code source "+codeSource); 
     if(!codeSource.endsWith(".jar")) 
     { 
      System.out.println("not running from jar, no unpacking necessary"); 
      try{return new File(WordNetUnpacker.class.getClassLoader().getResource(jarDir).toURI());} 
      catch (URISyntaxException e) {throw new IOException(e);} 
     } 
     try(JarFile jarFile = new JarFile(codeSource)) 
     { 
      String tempDirString = System.getProperty("java.io.tmpdir"); 
      if(tempDirString==null) {throw new IOException("java.io.tmpdir not set");} 
      File tempDir = new File(tempDirString); 
      if(!tempDir.exists()) {throw new IOException("temporary directory does not exist");} 
      if(!tempDir.isDirectory()) {throw new IOException("temporary directory is a file, not a directory ");} 
      File wordNetDir = new File(tempDirString+'/'+"wordnet"+ID); 
      wordNetDir.mkdir(); 
      System.out.println("unpacking jarfile "+jarFile.getName()); 
      copyResourcesToDirectory(jarFile, jarDir, wordNetDir.getAbsolutePath()); 
      return wordNetDir; 
     }  
    } 
    /** Copies a directory from a jar file to an external directory. Copied from <a href="http://stackoverflow.com/a/19859453/398963">Stack Overflow</a>. */ 
    public static void copyResourcesToDirectory(JarFile fromJar, String jarDir, String destDir) throws IOException 
    { 
     int copyCount = 0; 
     for (Enumeration<JarEntry> entries = fromJar.entries(); entries.hasMoreElements();) 
     { 
      JarEntry entry = entries.nextElement(); 
      if(!entry.getName().contains("models")) continue; 
      if (entry.getName().startsWith(jarDir) && !entry.isDirectory()) { 
       copyCount++; 
       File dest = new File(destDir + "/" + entry.getName().substring(jarDir.length() + 1)); 
       File parent = dest.getParentFile(); 
       if (parent != null) { 
        parent.mkdirs(); 
       } 

       FileOutputStream out = new FileOutputStream(dest); 
       InputStream in = fromJar.getInputStream(entry); 

       try { 
        byte[] buffer = new byte[8 * 1024]; 

        int s = 0; 
        while ((s = in.read(buffer)) > 0) { 
         out.write(buffer, 0, s); 
        } 
       } catch (IOException e) { 
        throw new IOException("Could not copy asset from jar file", e); 
       } finally { 
        try { 
         in.close(); 
        } catch (IOException ignored) {} 
        try { 
         out.close(); 
        } catch (IOException ignored) {} 
       } 
      } 
     } 
     if(copyCount==0) System.out.println("Warning: No files copied!"); 
    } 
} 
संबंधित मुद्दे