Search in sources :

Example 6 with AltTransBean

use of net.heartsome.cat.ts.core.bean.AltTransBean in project translationstudio8 by heartsome.

the class XLFHandler method getAltTrans.

// public Vector<NoteBean> get
private Vector<AltTransBean> getAltTrans(VTDUtils vu) throws XPathParseException, XPathEvalException, NavException {
    VTDNav vn = vu.getVTDNav();
    Vector<AltTransBean> result = new Vector<AltTransBean>();
    AutoPilot apAltTrans = new AutoPilot(vn);
    String xpath = "./alt-trans";
    AutoPilot ap = new AutoPilot(vn);
    ap.selectXPath(xpath);
    while (ap.evalXPath() != -1) {
        AltTransBean atb = new AltTransBean();
        // 获取当前匹配节点全部属性。
        atb.setMatchProps(vu.getCurrentElementAttributs());
        // 获取源节点内容、属性及纯文本
        vn.push();
        if (vu.pilot(apAltTrans, "./source") != -1) {
            atb.setSrcContent(vu.getElementContent());
            atb.setSrcProps(vu.getCurrentElementAttributs());
            atb.setSrcText(getTUPureText(vu.getVTDNav()));
        }
        apAltTrans.resetXPath();
        vn.pop();
        // 获取目标节点内容、属性及纯文本
        vn.push();
        if (vu.pilot(apAltTrans, "./target") != -1) {
            atb.setTgtContent(vu.getElementContent());
            atb.setTgtProps(vu.getCurrentElementAttributs());
            atb.setTgtText(getTUPureText(vu.getVTDNav()));
        }
        apAltTrans.resetXPath();
        vn.pop();
        // 如果 Source 和 Target 的内容都不为 null。
        if (atb.getSrcContent() != null && atb.getTgtContent() != null) {
            // 获取匹配节点的属性组集合
            vn.push();
            atb.setPropGroups(getPrpoGroups(vu));
            vn.pop();
            result.add(atb);
        }
    }
    if (result.isEmpty()) {
        return null;
    } else {
        // 排序
        ArrayList<AltTransBean> list = new ArrayList<AltTransBean>(result);
        Collections.sort(list, new Comparator<AltTransBean>() {

            public int compare(AltTransBean o1, AltTransBean o2) {
                if (o1 == null && o2 == null) {
                    return 0;
                } else if (o1 == null) {
                    return 1;
                } else if (o2 == null) {
                    return -1;
                }
                if (o1.getMatchProps() == null && o2.getMatchProps() == null) {
                    return 0;
                } else if (o1.getMatchProps() == null) {
                    return 1;
                } else if (o2.getMatchProps() == null) {
                    return -1;
                }
                if (o1.getMatchProps().get("match-quality") == null && o2.getMatchProps().get("match-quality") == null) {
                    return 0;
                } else if (o1.getMatchProps().get("match-quality") == null) {
                    return 1;
                } else if (o2.getMatchProps().get("match-quality") == null) {
                    return -1;
                }
                int mq1 = Integer.parseInt(o1.getMatchProps().get("match-quality").replace("%", ""));
                int mq2 = Integer.parseInt(o2.getMatchProps().get("match-quality").replace("%", ""));
                return mq2 - mq1;
            }
        });
        return new Vector<AltTransBean>(list);
    }
}
Also used : AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList) VTDNav(com.ximpleware.VTDNav) Vector(java.util.Vector)

Example 7 with AltTransBean

use of net.heartsome.cat.ts.core.bean.AltTransBean in project translationstudio8 by heartsome.

the class PreTranslation method executeSimpleMatch.

// private String defaultSimplematcher;
private String executeSimpleMatch(VTDUtils vu, TransUnitInfo2TranslationBean tuInfo, XMLModifier xm) throws /*
																										 * , boolean
																										 * updateTarget
																										 */
XPathParseException, XPathEvalException, NavException {
    List<ISimpleMatcher> simpleMatchers = SimpleMatcherFactory.getInstance().getCuurentMatcher();
    StringBuffer bf = new StringBuffer();
    for (ISimpleMatcher matcher : simpleMatchers) {
        if (!matcher.isSuportPreTrans()) {
            continue;
        }
        String toolId = matcher.getMathcerToolId();
        boolean isOverwrite = matcher.isOverwriteMatch();
        boolean needClear = false;
        vu.getVTDNav().push();
        AutoPilot ap = new AutoPilot(vu.getVTDNav());
        ap.selectXPath("./alt-trans[@tool-id='" + toolId + "']");
        if (ap.evalXPath() != -1) {
            if (!isOverwrite) {
                vu.getVTDNav().pop();
                continue;
            } else {
                needClear = true;
            }
        }
        vu.getVTDNav().pop();
        if (needClear) {
            vu.delete(new AutoPilot(vu.getVTDNav()), xm, "./alt-trans[@tool-id='" + toolId + "']", VTDUtils.PILOT_TO_END);
        }
        String tgtText = matcher.executeMatch(tuInfo);
        if (tgtText.equals("")) {
            continue;
        }
        /*
			 * if (updateTarget && defaultSimplematcher.equals(toolId)) { vu.delete(new AutoPilot(vu.getVTDNav()), xm,
			 * "./target", VTDUtils.PILOT_TO_END); bf.append("<target xml:lang=\"" + tuInfo.getTgtLangugage() +
			 * "\" state=\"new\"  hs:matchType=\"" + matcher.getMatcherType() + "\" hs:quality=\"100\">");
			 * bf.append(tgtText); bf.append("</target>");
			 * 
			 * currentCounter.countTransTu(); }
			 */
        AltTransBean bean = new AltTransBean(tuInfo.getSrcPureText(), tgtText, tuInfo.getSrcLanguage(), tuInfo.getTgtLangugage(), matcher.getMathcerOrigin(), matcher.getMathcerToolId());
        bean.getMatchProps().put("match-quality", "100");
        bean.setSrcContent(tuInfo.getSrcPureText());
        bean.setTgtContent(tgtText);
        bean.getMatchProps().put("hs:matchType", matcher.getMatcherType());
        bf.append(bean.toXMLString());
    }
    return bf.toString();
}
Also used : AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) ISimpleMatcher(net.heartsome.cat.ts.tm.simpleMatch.ISimpleMatcher) AutoPilot(com.ximpleware.AutoPilot)

Example 8 with AltTransBean

use of net.heartsome.cat.ts.core.bean.AltTransBean in project translationstudio8 by heartsome.

the class PreMachineTranslation method executeSimpleMatch.

/**
	 * 访问机器翻译API
	 * @param vu
	 * @param tuInfo
	 * @param xm
	 * @return
	 * @throws XPathParseException
	 * @throws XPathEvalException
	 * @throws NavException
	 *             ;
	 */
private String executeSimpleMatch(VTDUtils vu, TransUnitInfo2TranslationBean tuInfo, XMLModifier xm) throws XPathParseException, XPathEvalException, NavException {
    StringBuffer bf = new StringBuffer();
    for (ISimpleMatcher matcher : simpleMatchers) {
        // 1、是否支持预存机器翻译
        if (!matcher.isSuportPreTrans()) {
            continue;
        }
        String toolId = matcher.getMathcerToolId();
        vu.getVTDNav().push();
        AutoPilot ap = new AutoPilot(vu.getVTDNav());
        ap.selectXPath("./alt-trans[@tool-id='" + toolId + "']");
        // 3、 是否有预存翻译,有预存的机器翻译,不进行预存
        if (ap.evalXPath() != -1) {
            vu.getVTDNav().pop();
            continue;
        }
        vu.getVTDNav().pop();
        String tgtText = matcher.executeMatch(tuInfo);
        if (tgtText.equals("")) {
            continue;
        }
        AltTransBean bean = new AltTransBean(tuInfo.getSrcPureText(), tgtText, tuInfo.getSrcLanguage(), tuInfo.getTgtLangugage(), matcher.getMathcerOrigin(), matcher.getMathcerToolId());
        bean.getMatchProps().put("match-quality", "100");
        bean.setSrcContent(tuInfo.getSrcPureText());
        bean.setTgtContent(tgtText);
        bean.getMatchProps().put("hs:matchType", matcher.getMatcherType());
        bf.append(bean.toXMLString());
    }
    return bf.toString();
}
Also used : AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) ISimpleMatcher(net.heartsome.cat.ts.tm.simpleMatch.ISimpleMatcher) AutoPilot(com.ximpleware.AutoPilot)

Example 9 with AltTransBean

use of net.heartsome.cat.ts.core.bean.AltTransBean in project translationstudio8 by heartsome.

the class XLFHandler method updateAltTrans.

private void updateAltTrans(VTDUtils vu, XMLModifier xm, String rowId, List<AltTransBean> newAltTrans, List<String> oldAltTransToolId) {
    String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
    AutoPilot ap = new AutoPilot(vu.getVTDNav());
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    try {
        ap.selectXPath(tuXPath);
        if (ap.evalXPath() != -1) {
            StringBuffer bf = new StringBuffer();
            for (String toolId : oldAltTransToolId) {
                bf.append("@tool-id='").append(toolId).append("' | ");
            }
            if (bf.length() > 0) {
                String toolId = bf.substring(0, bf.lastIndexOf("|"));
                String deleteXpath = "./alt-trans[" + toolId + "]";
                AutoPilot _ap = new AutoPilot(vu.getVTDNav());
                _ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
                _ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
                vu.delete(_ap, xm, deleteXpath, VTDUtils.PILOT_TO_END);
            }
            StringBuffer xmlBf = new StringBuffer();
            for (AltTransBean altTran : newAltTrans) {
                xmlBf.append(altTran.toXMLString());
            }
            if (xmlBf.length() > 0) {
                xm.insertBeforeTail(xmlBf.toString());
            }
        }
    } catch (XPathParseException e) {
        LOGGER.error("", e);
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
    } catch (ModifyException e) {
        LOGGER.error("", e);
    } catch (UnsupportedEncodingException e) {
        LOGGER.error("", e);
    } catch (NavException e) {
        LOGGER.error("", e);
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with AltTransBean

use of net.heartsome.cat.ts.core.bean.AltTransBean in project translationstudio8 by heartsome.

the class TSFileHandler method getAltTrans.

private Vector<AltTransBean> getAltTrans(VTDUtils vu) throws XPathParseException, XPathEvalException, NavException {
    VTDNav vn = vu.getVTDNav();
    Vector<AltTransBean> result = new Vector<AltTransBean>();
    String xpath = "./alt-trans";
    AutoPilot ap = new AutoPilot(vn);
    ap.selectXPath(xpath);
    while (ap.evalXPath() != -1) {
        AltTransBean atb = new AltTransBean();
        AutoPilot apAltTrans = new AutoPilot(vn);
        // 获取当前匹配节点全部属性。
        atb.setMatchProps(vu.getCurrentElementAttributs());
        // 获取源节点内容、属性及纯文本
        xpath = "./source";
        apAltTrans.resetXPath();
        apAltTrans.selectXPath(xpath);
        vn.push();
        if (apAltTrans.evalXPath() != -1) {
            atb.setSrcContent(vu.getElementContent());
            atb.setSrcProps(vu.getCurrentElementAttributs());
            atb.setSrcText(vu.getElementPureText());
        }
        vn.pop();
        // 获取目标节点内容、属性及纯文本
        xpath = "./target";
        apAltTrans.resetXPath();
        apAltTrans.selectXPath(xpath);
        vn.push();
        if (apAltTrans.evalXPath() != -1) {
            atb.setTgtContent(vu.getElementContent());
            atb.setTgtProps(vu.getCurrentElementAttributs());
            atb.setTgtText(vu.getElementPureText());
            vn.pop();
        } else {
            vn.pop();
            continue;
        }
        // 获取匹配节点的属性组集合
        vn.push();
        atb.setPropGroups(getPrpoGroups(vu));
        vn.pop();
        result.add(atb);
    }
    if (result.isEmpty()) {
        return null;
    } else {
        return result;
    }
}
Also used : AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) Vector(java.util.Vector)

Aggregations

AltTransBean (net.heartsome.cat.ts.core.bean.AltTransBean)14 AutoPilot (com.ximpleware.AutoPilot)7 Vector (java.util.Vector)7 Hashtable (java.util.Hashtable)5 VTDNav (com.ximpleware.VTDNav)4 NavException (com.ximpleware.NavException)3 XPathEvalException (com.ximpleware.XPathEvalException)3 XPathParseException (com.ximpleware.XPathParseException)3 PropBean (net.heartsome.cat.ts.core.bean.PropBean)3 PropGroupBean (net.heartsome.cat.ts.core.bean.PropGroupBean)3 ModifyException (com.ximpleware.ModifyException)2 FuzzySearchResult (net.heartsome.cat.common.bean.FuzzySearchResult)2 TmxProp (net.heartsome.cat.common.bean.TmxProp)2 TmxTU (net.heartsome.cat.common.bean.TmxTU)2 TransUnitBean (net.heartsome.cat.ts.core.bean.TransUnitBean)2 ISimpleMatcher (net.heartsome.cat.ts.tm.simpleMatch.ISimpleMatcher)2 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)2 TranscodeException (com.ximpleware.TranscodeException)1 VTDGen (com.ximpleware.VTDGen)1 XMLModifier (com.ximpleware.XMLModifier)1