Github user takuti commented on a diff in the pull request:

    https://github.com/apache/incubator-hivemall/pull/97#discussion_r127118661
  
    --- Diff: nlp/src/main/java/hivemall/nlp/tokenizer/KuromojiUDF.java ---
    @@ -170,6 +192,100 @@ private static CharArraySet stopWords(@Nonnull final 
String[] array)
             return results;
         }
     
    +    @Nullable
    +    private static UserDictionary userDictionary(@Nonnull final 
ObjectInspector oi)
    +            throws UDFArgumentException {
    +        if (HiveUtils.isConstListOI(oi)) {
    +            return userDictionary(HiveUtils.getConstStringArray(oi));
    +        } else if (HiveUtils.isConstString(oi)) {
    +            return userDictionary(HiveUtils.getConstString(oi));
    +        } else {
    +            throw new UDFArgumentException(
    +                "User dictionary MUST be given as an array of constant 
string or constant string (URL)");
    +        }
    +    }
    +
    +    @Nullable
    +    private static UserDictionary userDictionary(@Nullable final String[] 
userDictArray)
    +            throws UDFArgumentException {
    +        if (userDictArray == null) {
    +            return null;
    +        }
    +
    +        StringBuilder builder = new StringBuilder();
    +        for (String row : userDictArray) {
    +            builder.append(row);
    +            builder.append("\r\n");
    +        }
    +        InputStream is = new 
ByteArrayInputStream(builder.toString().getBytes());
    +        final Reader reader = new InputStreamReader(is);
    +        try {
    +            return UserDictionary.open(reader); // return null if empty
    +        } catch (Throwable e) {
    +            throw new UDFArgumentException(
    +                "Failed to create user dictionary based on the given 
array<string>: " + e);
    +        }
    +    }
    +
    +    @Nullable
    +    private static UserDictionary userDictionary(@Nullable final String 
userDictURL)
    +            throws UDFArgumentException {
    +        if (userDictURL == null) {
    +            return null;
    +        }
    +
    +        final HttpURLConnection conn;
    +        try {
    +            conn = HttpUtils.getHttpURLConnection(userDictURL);
    +        } catch (Throwable e) {
    +            throw new UDFArgumentException("Failed to create HTTP 
connection to the URL: " + e);
    +        }
    +
    +        // allow to read as a compressed GZIP file for efficiency
    +        conn.setRequestProperty("Accept-Encoding", "gzip");
    +
    +        conn.setConnectTimeout(CONNECT_TIMEOUT_MS); // throw exception 
from connect()
    +        conn.setReadTimeout(READ_TIMEOUT_MS); // throw exception from 
getXXX() methods
    +
    +        final int responseCode;
    +        try {
    +            responseCode = conn.getResponseCode();
    +        } catch (Throwable e) {
    +            throw new UDFArgumentException("Failed to get response code: " 
+ e);
    +        }
    +        if (responseCode != 200) {
    +            throw new UDFArgumentException("God invalid response code: " + 
responseCode);
    +        }
    +
    +        final String contentType = conn.getContentType();
    +        boolean textContent = contentType.startsWith("text/plain");
    +        boolean binaryContent = 
contentType.startsWith("application/octet-stream");
    +        if (!textContent && !binaryContent) {
    +            throw new UDFArgumentException(
    +                "User dictionary URL indicates unexpected content type: " 
+ contentType);
    +        }
    +
    +        InputStream is;
    +        try {
    +            is = HttpUtils.getBoundedInputStream(conn, 
MAX_INPUT_STREAM_SIZE);
    +            if ((textContent && "gzip".equals(conn.getContentEncoding())) 
|| binaryContent) {
    +                is = new GZIPInputStream(is);
    --- End diff --
    
    Thank you for the comments. Ah-hah, S3! It's true. I'm re-thinking here.
    
    > And, the above code works for shortened URLs.
    
    What do u mean? If URL is shortened, file extension is disappeared, and 
`urlString.endWith` does not work.
    
    > Dictionary format should support ".txt", ".dict", and etc. I'm not sure 
plain/text is enough.
    
    For non-gzipped files, `UserDictionary.open(reader)` throws an exception if 
the file is not in CSV format as a Kuromoji dictionary. So, we can skip 
checking `text/plain` and their file extension as you suggested in the above 
snippet; everything `.txt`, `.dic`, `.csv` is acceptable as long as its content 
is readable as a CSV file.
    
    > Special handling is only required for .gz and throw exception for 
malformed dictionary is enough.
    
    Yep. `new GZIPInputStream()` internally checks header magic number. We can 
utilize it somehow. I'm working on here under your suggestion.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to