/** * Free memory */ public void close() { Hyphen.getLibrary().hnj_hyphen_free(dictionary); }
public int compareTo(Break that) { if (this.getBreakPosition() < that.getBreakPosition()) return 1; else if (this.getBreakPosition() > that.getBreakPosition()) return -1; else if (!this.hasHyphen() && that.hasHyphen()) return 1; else if (this.hasHyphen() && !that.hasHyphen()) return -1; else return 0; } }
private LibhyphenHyphenatorImpl(URI table) throws CompilationException, FileNotFoundException { this.table = table; hyphenator = new Hyphenator(resolveTable(table)); }
return new NoBreak(text); Break lastOkBreak = null; outer: for (String s : splitInclDelimiter(text, Pattern.compile("['\\p{L}]+"))) { if (isWord) { String word = s; byte[] wordBytes = encode(word); int wordSize = wordBytes.length; if (wordSize > wordHyphens.capacity()) PointerByReference posPointer = new PointerByReference(Pointer.NULL); PointerByReference cutPointer = new PointerByReference(Pointer.NULL); Hyphen.getLibrary().hnj_hyphen_hyphenate2(dictionary, wordBytes, wordSize, wordHyphens, null, repPointer, posPointer, cutPointer); Break b; if (rep != null && rep[posInWord - 1] != null) b = new NonStandardBreak( text, wordStart + posInWord - pos[posInWord - 1], cut[posInWord - 1], rep[posInWord - 1]); else b = new StandardBreak(text, wordStart + posInWord); if (b.getBreakPosition() > lineLength) break outer; if (lastOkBreak == null || b.compareTo(lastOkBreak) > 0) lastOkBreak = b; if (g.charAt(g.length() - 1) == '-' && p + s.length() == text.length()) continue;
protected String hyphenate(String text, int lineLength, Character hyphen, Character noHyphen) { Break b = hyphenate(text, lineLength); String hyphenatedText = ""; hyphenatedText += b.getText().substring(0, b.getBreakPosition()); if (b.hasHyphen()) { if (hyphen != null) hyphenatedText += hyphen; } else if (noHyphen != null) hyphenatedText += noHyphen; hyphenatedText += b.getText().substring(b.getBreakPosition()); return hyphenatedText; }
/** * Default constructor * @param dictPath The path to the hyphenation dictionary file, * e.g. /usr/share/hyphen/hyph_de_DE.dic * @throws FileNotFoundException if the dictionary file cannot be found. * @throws CompilationException if the encoding of the file is not supported. */ public Hyphenator(File dictionaryFile) throws CompilationException, FileNotFoundException { if (!dictionaryFile.exists()) throw new FileNotFoundException("Dictionary file at " + dictionaryFile.getAbsolutePath() + " doesn't exist."); try { charset = getCharset(dictionaryFile); } catch(UnsupportedCharsetException e) { throw new CompilationException(e); } dictionary = Hyphen.getLibrary().hnj_hyphen_load(dictionaryFile.getAbsolutePath()); }
byte[] wordBytes = encode(word); int wordSize = wordBytes.length; if (wordSize > wordHyphens.capacity()) PointerByReference posPointer = new PointerByReference(Pointer.NULL); PointerByReference cutPointer = new PointerByReference(Pointer.NULL); Hyphen.getLibrary().hnj_hyphen_hyphenate2(dictionary, wordBytes, wordSize, wordHyphens, null, repPointer, posPointer, cutPointer); if (repPointer.getValue() != Pointer.NULL) throw new StandardHyphenationException("Text contains non-standard hyphenation points.");
/** * Returns the fully hyphenated string. * The given hyphen characters are inserted at all possible hyphenation points. * @param text The string to be hyphenated * @param shy The character to be used as soft hyphen. * @param zwsp The character to be used as zero-width space. * @return The hyphenated string * @throws StandardHyphenationException if the word contains non-standard hyphenation * points, in which case the method {@link #hyphenate(String, int, Character, * Character)} should be used instead. */ public String hyphenate(String text, Character shy, Character zwsp) throws StandardHyphenationException { if (shy == null && zwsp == null) return text; byte[] hyphens = hyphenate(text); StringBuffer hyphenatedText = new StringBuffer(); int i; for (i = 0; i < hyphens.length; i++) { hyphenatedText.append(text.charAt(i)); if (shy != null && hyphens[i] == SHY) hyphenatedText.append(shy); else if (zwsp != null && hyphens[i] == ZWSP) hyphenatedText.append(zwsp); } hyphenatedText.append(text.charAt(i)); return hyphenatedText.toString(); }
private NonStandardBreak(String text, int repStart, int repLength, String repString) { int breakPosInRep = repString.indexOf('='); if (breakPosInRep < 0) throw new HyphenationException("Table error"); this.text = text.substring(0, repStart) + repString.substring(0, breakPosInRep) + repString.substring(breakPosInRep + 1) + text.substring(repStart + repLength); position = repStart + breakPosInRep; hyphen = true; } }
@Reference( name = "LibhyphenLibrary", unbind = "-", service = NativePath.class, target = "(identifier=http://hunspell.sourceforge.net/Hyphen/native/*)", cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.STATIC ) protected void bindLibrary(NativePath path) { URI libraryPath = path.get("libhyphen").iterator().next(); Hyphen.setLibraryPath(asFile(path.resolve(libraryPath))); logger.debug("Registering libhyphen library: " + libraryPath); }
protected Break breakWord(String word, int limit, boolean force) { Hyphenator.Break br = hyphenator.hyphenate(word, limit); if (force && br.getBreakPosition() == 0) return new Break(word, limit, false); else return new Break(br.getText(), br.getBreakPosition(), br.hasHyphen()); } };
private String transform(String text) { try { Tuple2<String,byte[]> t = extractHyphens(text, SHY, ZWSP); byte[] hyphens = hyphenator.hyphenate(t._1); if (t._2 != null) for (int i = 0; i < hyphens.length; i++) hyphens[i] += t._2[i]; return insertHyphens(t._1, hyphens, SHY, ZWSP); } catch (Exception e) { throw new RuntimeException("Error during libhyphen hyphenation", e); } }
else positions = new byte[_text.length() - 1]; byte[] autoHyphens = hyphenator.hyphenate(_text); for (int i = 0; i < autoHyphens.length; i++) positions[i] += autoHyphens[i];