use of xtc.tree.Location in project languagetool by languagetool-org.
the class AtomFeedChecker method toWikipediaRuleMatches.
private List<WikipediaRuleMatch> toWikipediaRuleMatches(String content, PlainTextMapping filteredContent, List<RuleMatch> ruleMatches, AtomFeedItem item) {
List<WikipediaRuleMatch> result = new ArrayList<>();
for (RuleMatch ruleMatch : ruleMatches) {
Location fromPos = filteredContent.getOriginalTextPositionFor(ruleMatch.getFromPos() + 1);
Location toPos = filteredContent.getOriginalTextPositionFor(ruleMatch.getToPos() + 1);
int origFrom = LocationHelper.absolutePositionFor(fromPos, content);
int origTo = LocationHelper.absolutePositionFor(toPos, content);
String errorContext = contextTools.getContext(origFrom, origTo, content);
result.add(new WikipediaRuleMatch(language, ruleMatch, errorContext, item));
}
return result;
}
use of xtc.tree.Location in project languagetool by languagetool-org.
the class PlainTextMapping method getOriginalTextPositionFor.
/**
* @param plainTextPosition not zero-based - smallest value is 1!
*/
public Location getOriginalTextPositionFor(int plainTextPosition) {
if (plainTextPosition < 1) {
throw new RuntimeException("plainTextPosition must be > 0 - its value starts at 1");
}
Location origPosition = mapping.get(plainTextPosition);
if (origPosition != null) {
//System.out.println("mapping " + plainTextPosition + " to " + origPosition + " [direct]");
return origPosition;
}
int minDiff = Integer.MAX_VALUE;
Location bestMatch = null;
// algorithm: find the closest lower position
for (Map.Entry<Integer, Location> entry : mapping.entrySet()) {
int maybeClosePosition = entry.getKey();
if (plainTextPosition > maybeClosePosition) {
int diff = plainTextPosition - maybeClosePosition;
if (diff >= 0 && diff < minDiff) {
bestMatch = entry.getValue();
//bestMaybeClosePosition = maybeClosePosition;
minDiff = diff;
}
}
}
if (bestMatch == null) {
throw new RuntimeException("Could not map " + plainTextPosition + " to original position. Mapping: " + mapping);
}
// bestMatch.column + "+" + minDiff + ", bestMatch was: " + bestMaybeClosePosition +"=>"+ bestMatch);
return new Location(bestMatch.file, bestMatch.line, bestMatch.column + minDiff);
}
use of xtc.tree.Location in project languagetool by languagetool-org.
the class SuggestionReplacer method applySuggestionsToOriginalText.
/**
* Applies the suggestions from the rule to the original text. For rules that
* have no suggestion, a pseudo-correction is generated that contains the same
* text as before.
*/
public List<RuleMatchApplication> applySuggestionsToOriginalText(RuleMatch match) {
List<String> replacements = new ArrayList<>(match.getSuggestedReplacements());
boolean hasRealReplacements = replacements.size() > 0;
if (!hasRealReplacements) {
// create a pseudo replacement with the error text itself
String plainText = textMapping.getPlainText();
replacements.add(plainText.substring(match.getFromPos(), match.getToPos()));
}
List<RuleMatchApplication> ruleMatchApplications = new ArrayList<>();
// not zero-based!
Location fromPosLocation = textMapping.getOriginalTextPositionFor(match.getFromPos() + 1);
Location toPosLocation = textMapping.getOriginalTextPositionFor(match.getToPos() + 1);
/*System.out.println("=========");
System.out.println(textMapping.getMapping());
System.out.println("=========");
System.out.println(textMapping.getPlainText());
System.out.println("=========");
System.out.println(originalText);
System.out.println("=========");*/
int fromPos = LocationHelper.absolutePositionFor(fromPosLocation, originalText);
int toPos = LocationHelper.absolutePositionFor(toPosLocation, originalText);
for (String replacement : replacements) {
String errorText = textMapping.getPlainText().substring(match.getFromPos(), match.getToPos());
// the algorithm is off a bit sometimes due to the complex syntax, so consider the next whitespace:
int contextFrom = findNextWhitespaceToTheLeft(originalText, fromPos);
int contextTo = findNextWhitespaceToTheRight(originalText, toPos);
/*System.out.println(match + ":");
System.out.println("match.getFrom/ToPos(): " + match.getFromPos() + "/" + match.getToPos());
System.out.println("from/toPosLocation: " + fromPosLocation + "/" + toPosLocation);
System.out.println("from/toPos: " + fromPos + "/" + toPos);
System.out.println("contextFrom/To: " + contextFrom + "/" + contextTo);*/
String context = originalText.substring(contextFrom, contextTo);
String text = originalText.substring(0, contextFrom) + errorMarker.getStartMarker() + context + errorMarker.getEndMarker() + originalText.substring(contextTo);
String newContext;
if (StringUtils.countMatches(context, errorText) == 1) {
newContext = context.replace(errorText, replacement);
} else {
// This may happen especially for very short strings. As this is an
// error, we don't claim to have real replacements:
newContext = context;
hasRealReplacements = false;
}
String newText = originalText.substring(0, contextFrom) + // we do a simple string replacement as that works even if our mapping is off a bit:
errorMarker.getStartMarker() + newContext + errorMarker.getEndMarker() + originalText.substring(contextTo);
RuleMatchApplication application;
if (hasRealReplacements) {
application = RuleMatchApplication.forMatchWithReplacement(match, text, newText, errorMarker);
} else {
application = RuleMatchApplication.forMatchWithoutReplacement(match, text, newText, errorMarker);
}
ruleMatchApplications.add(application);
}
return ruleMatchApplications;
}
use of xtc.tree.Location in project languagetool by languagetool-org.
the class TextConverter method addMapping.
private void addMapping(Locatable loc, int columnCorrection) {
if (!enableMapping) {
// this is surprisingly resource intensive, so it can be disabled
return;
}
String contentSoFar = sb.toString() + line;
int textPos = contentSoFar.length() + needNewlines + 1;
if (loc.hasLocation()) {
Location location = loc.getLocation();
mapping.put(textPos, new Location(location.file, location.line, location.column + columnCorrection));
//System.out.println("PUT " + textPos + " -> " + loc.getLocation());
}
}
Aggregations