use of java.text.BreakIterator in project jdk8u_jdk by JetBrains.
the class TextLayoutStrategy method sync.
/**
* Synchronize the strategy with its FlowView. Allows the strategy
* to update its state to account for changes in that portion of the
* model represented by the FlowView. Also allows the strategy
* to update the FlowView in response to these changes.
*/
void sync(FlowView fv) {
View lv = getLogicalView(fv);
text.setView(lv);
Container container = fv.getContainer();
FontRenderContext frc = sun.swing.SwingUtilities2.getFontRenderContext(container);
frcTx = frc.getTransform();
BreakIterator iter;
Container c = fv.getContainer();
if (c != null) {
iter = BreakIterator.getLineInstance(c.getLocale());
} else {
iter = BreakIterator.getLineInstance();
}
Object shaper = null;
if (c instanceof JComponent) {
shaper = ((JComponent) c).getClientProperty(TextAttribute.NUMERIC_SHAPING);
}
text.setShaper(shaper);
measurer = new LineBreakMeasurer(text, iter, frc);
// If the children of the FlowView's logical view are GlyphViews, they
// need to have their painters updated.
int n = lv.getViewCount();
for (int i = 0; i < n; i++) {
View child = lv.getView(i);
if (child instanceof GlyphView) {
int p0 = child.getStartOffset();
int p1 = child.getEndOffset();
measurer.setPosition(text.toIteratorIndex(p0));
TextLayout layout = measurer.nextLayout(Float.MAX_VALUE, text.toIteratorIndex(p1), false);
((GlyphView) child).setGlyphPainter(new GlyphPainter2(layout));
}
}
// Reset measurer.
measurer.setPosition(text.getBeginIndex());
}
use of java.text.BreakIterator in project cogcomp-nlp by CogComp.
the class ThaiTokenizer method getTextAnnotation.
public TextAnnotation getTextAnnotation(String text) {
List<IntPair> offsets = new ArrayList<>();
List<String> surfaces = new ArrayList<>();
List<Integer> sen_ends = new ArrayList<>();
BreakIterator boundary = BreakIterator.getWordInstance(new Locale("th", "TH", "TH"));
boundary.setText(text);
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
// System.out.println(start+" "+end+" "+text.length());
String sur = text.substring(start, end);
if (sur.trim().isEmpty()) {
// sen_ends.add(surfaces.size());
continue;
}
surfaces.add(sur);
offsets.add(new IntPair(start, end));
}
if (surfaces.size() > 0 && (sen_ends.size() == 0 || sen_ends.get(sen_ends.size() - 1) != surfaces.size()))
sen_ends.add(surfaces.size());
IntPair[] offs = new IntPair[offsets.size()];
offs = offsets.toArray(offs);
String[] surfs = new String[surfaces.size()];
surfs = surfaces.toArray(surfs);
int[] ends = new int[sen_ends.size()];
for (int i = 0; i < sen_ends.size(); i++) ends[i] = sen_ends.get(i);
// System.out.println(text);
// System.out.println(offsets);
// System.out.println(sen_ends);
TextAnnotation ta = new TextAnnotation("", "", text, offs, surfs, ends);
return ta;
}
use of java.text.BreakIterator in project chefly_android by chef-ly.
the class GetCookingActivity method onStart.
// TODO - add onPause and onResume where the speech and recognizer are stopped
@Override
protected void onStart() {
super.onStart();
Intent i = getIntent();
// Get ingredient list for popup
ArrayList<String> ingredients = i.getStringArrayListExtra("ingredients");
if (ingredients != null) {
ingredientsPopup = DialogPopUp.newInstance(getResources().getString(R.string.ingredients), ingredients);
ingredientsPopup.showTitle(false);
} else {
ingredientsPopup = DialogPopUp.newInstance(getResources().getString(R.string.ingredients), new ArrayList<String>());
}
// Split up the directions to more "digestible" (wink wink) pieces
String[] rawDirections = i.getStringArrayExtra("directions");
directions = new ArrayList<>();
for (int x = 0; x < rawDirections.length; x++) {
BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.US);
String source = rawDirections[x];
iterator.setText(source);
int start = iterator.first();
for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) {
directions.add(source.substring(start, end));
}
}
numberSteps = directions.size();
if (numberSteps > 0) {
directionsPopup = DialogPopUp.newInstance(getResources().getString(R.string.directions), directions);
directionsPopup.showTitle(false);
hasDirections = true;
updateStepText();
ArrayList<GetCookingFragment> frags = new ArrayList<>();
for (int j = 0; j < numberSteps; j++) {
frags.add(GetCookingFragment.newInstance(String.valueOf(j), directions.get(j)));
}
pager.setAdapter(new CookingPagerAdapter(getSupportFragmentManager(), frags));
Log.d(TAG, "Step ----> " + step);
pager.setCurrentItem(step);
}
// register with EventBus to get events from VoiceRec
EventBus.getDefault().register(this);
}
use of java.text.BreakIterator in project hive by apache.
the class GenericUDFSentences method evaluate.
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
assert (arguments.length >= 1 && arguments.length <= 3);
if (arguments[0].get() == null) {
return null;
}
// if there is more than 1 argument specified, a different natural language
// locale is being specified
Locale locale = null;
if (arguments.length > 1 && arguments[1].get() != null) {
Text language = (Text) converters[1].convert(arguments[1].get());
Text country = null;
if (arguments.length > 2 && arguments[2].get() != null) {
country = (Text) converters[2].convert(arguments[2].get());
}
if (country != null) {
locale = new Locale(language.toString().toLowerCase(), country.toString().toUpperCase());
} else {
locale = new Locale(language.toString().toLowerCase());
}
} else {
locale = Locale.getDefault();
}
// get the input and prepare the output
Text chunk = (Text) converters[0].convert(arguments[0].get());
String text = chunk.toString();
ArrayList<ArrayList<Text>> result = new ArrayList<ArrayList<Text>>();
// Parse out sentences using Java's text-handling API
BreakIterator bi = BreakIterator.getSentenceInstance(locale);
bi.setText(text);
int idx = 0;
while (bi.next() != BreakIterator.DONE) {
String sentence = text.substring(idx, bi.current());
idx = bi.current();
result.add(new ArrayList<Text>());
// Parse out words in the sentence
BreakIterator wi = BreakIterator.getWordInstance(locale);
wi.setText(sentence);
int widx = 0;
ArrayList<Text> sent_array = result.get(result.size() - 1);
while (wi.next() != BreakIterator.DONE) {
String word = sentence.substring(widx, wi.current());
widx = wi.current();
if (Character.isLetterOrDigit(word.charAt(0))) {
sent_array.add(new Text(word));
}
}
}
return result;
}
use of java.text.BreakIterator in project elasticsearch by elastic.
the class CustomSeparatorBreakIteratorTests method testSliceMiddle.
public void testSliceMiddle() throws Exception {
BreakIterator expected = BreakIterator.getSentenceInstance(Locale.ROOT);
BreakIterator actual = new CustomSeparatorBreakIterator(randomSeparator());
assertSameBreaks("000a000", 3, 1, expected, actual);
assertSameBreaks("000ab000", 3, 2, expected, actual);
assertSameBreaks("000abc000", 3, 3, expected, actual);
assertSameBreaks("000000", 3, 0, expected, actual);
}
Aggregations