Search in sources :

Example 1 with PlaceHolderEditModeBuilder

use of net.heartsome.cat.common.innertag.factory.PlaceHolderEditModeBuilder in project translationstudio8 by heartsome.

the class IntelligentTagPrcessor method intelligentAppendTag.

public static String intelligentAppendTag(String srcFullText, String targetFullText) {
    PlaceHolderEditModeBuilder plb = new PlaceHolderEditModeBuilder();
    XliffInnerTagFactory innerTagFactory = new XliffInnerTagFactory(plb);
    String targetParsedText = innerTagFactory.parseInnerTag(targetFullText);
    List<InnerTagBean> targetTags = innerTagFactory.getInnerTagBeans();
    String targetPureText = PATTERN.matcher(targetParsedText).replaceAll("");
    innerTagFactory.reset();
    innerTagFactory.parseInnerTag(srcFullText);
    List<InnerTagBean> srcTags = innerTagFactory.getInnerTagBeans();
    innerTagFactory.reset();
    if (targetTags.size() == srcTags.size()) {
        Matcher matcher = PlaceHolderEditModeBuilder.PATTERN.matcher(targetParsedText);
        int offset = 0;
        StringBuffer sb = new StringBuffer(targetParsedText);
        while (matcher.find()) {
            String placeHolder = matcher.group();
            int index = plb.getIndex(null, placeHolder);
            if (index > -1 && index < srcTags.size()) {
                InnerTagBean b = srcTags.get(index);
                String tagContent = b.getContent();
                int start = matcher.start() + offset;
                int end = matcher.end() + offset;
                sb.replace(start, end, tagContent);
                offset += tagContent.length() - 1;
            } else {
                sb.delete(0, sb.length());
                sb.append(targetFullText);
                break;
            }
        }
        targetPureText = sb.toString();
    } else if (srcTags.size() > 0) {
        if (srcTags.size() == 1) {
            // 只有一个标记
            InnerTagBean tagFirst = srcTags.get(0);
            String tagContent = tagFirst.getContent();
            if (srcFullText.indexOf(tagContent) == 0) {
                // header
                targetPureText = tagContent + targetPureText;
            } else if (srcFullText.indexOf(tagContent) + tagContent.length() == srcFullText.length()) {
                // tail
                targetPureText = targetPureText + tagContent;
            }
        } else {
            InnerTagBean tagFirst = srcTags.get(0);
            InnerTagBean tagLast = srcTags.get(srcTags.size() - 1);
            String firstTagCnt = tagFirst.getContent();
            String lastTagCnt = tagLast.getContent();
            if (srcFullText.indexOf(firstTagCnt) == 0) {
                // 句首标记是一个独立标记
                if (tagFirst.getType() == TagType.STANDALONE) {
                    StringBuilder sb = processNextStandloneTag(srcFullText, srcTags, 0);
                    sb.insert(0, firstTagCnt);
                    targetPureText = sb.append(targetPureText).toString();
                    if (tagLast.getType() == TagType.STANDALONE && targetPureText.indexOf(lastTagCnt) == -1 && srcFullText.indexOf(lastTagCnt) + lastTagCnt.length() == srcFullText.length()) {
                        // 最后标记是一个独立标记,且在句末
                        sb = processPreStandloneTag(targetPureText, srcFullText, srcTags, srcTags.size() - 1);
                        sb.insert(0, targetPureText).append(lastTagCnt);
                        targetPureText = sb.toString();
                    }
                } else if (tagFirst.getType() == TagType.START && tagLast.getType() == TagType.END && tagFirst.getIndex() == tagLast.getIndex()) {
                    // 句首是一个开始 且最后一个标记是结束
                    if (srcFullText.lastIndexOf(lastTagCnt) + lastTagCnt.length() == srcFullText.length()) {
                        // 最后一个标记在句尾,满足环绕条件
                        if (srcTags.size() > 2) {
                            // 有其他标记
                            // header process
                            StringBuilder sb = processNextStandloneTag(srcFullText, srcTags, 0);
                            sb.insert(0, firstTagCnt);
                            targetPureText = sb.append(targetPureText).toString();
                            sb = processPreStandloneTag(targetPureText, srcFullText, srcTags, srcTags.size() - 1);
                            sb.insert(0, targetPureText).append(lastTagCnt);
                            targetPureText = sb.toString();
                        } else {
                            targetPureText = firstTagCnt + targetPureText + lastTagCnt;
                        }
                    }
                } else if (tagLast.getType() == TagType.STANDALONE && srcFullText.indexOf(lastTagCnt) + lastTagCnt.length() == srcFullText.length()) {
                    // 最后标记是一个独立标记,且在句末
                    StringBuilder sb = processPreStandloneTag(targetPureText, srcFullText, srcTags, srcTags.size() - 1);
                    sb.insert(0, targetPureText).append(lastTagCnt);
                    targetPureText = sb.toString();
                }
            } else if (tagLast.getType() == TagType.STANDALONE && srcFullText.indexOf(lastTagCnt) + lastTagCnt.length() == srcFullText.length()) {
                // 最后一个标记独立标记且在句末
                StringBuilder sb = processPreStandloneTag(targetPureText, srcFullText, srcTags, srcTags.size() - 1);
                sb.insert(0, targetPureText).append(lastTagCnt);
                targetPureText = sb.toString();
            }
        }
    }
    return targetPureText;
}
Also used : XliffInnerTagFactory(net.heartsome.cat.common.innertag.factory.XliffInnerTagFactory) Matcher(java.util.regex.Matcher) PlaceHolderEditModeBuilder(net.heartsome.cat.common.innertag.factory.PlaceHolderEditModeBuilder) InnerTagBean(net.heartsome.cat.common.innertag.InnerTagBean)

Aggregations

Matcher (java.util.regex.Matcher)1 InnerTagBean (net.heartsome.cat.common.innertag.InnerTagBean)1 PlaceHolderEditModeBuilder (net.heartsome.cat.common.innertag.factory.PlaceHolderEditModeBuilder)1 XliffInnerTagFactory (net.heartsome.cat.common.innertag.factory.XliffInnerTagFactory)1