Search in sources :

Example 1 with AltTransBean

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

the class TmUtils method fuzzyResult2Alttransbean.

public static Vector<AltTransBean> fuzzyResult2Alttransbean(List<FuzzySearchResult> fuzzyResults) {
    Vector<AltTransBean> altTrans = new Vector<AltTransBean>();
    for (FuzzySearchResult result : fuzzyResults) {
        AltTransBean atb = new AltTransBean();
        //			Map<String, String> match = tu.getTuInfo();
        TmxTU tu = result.getTu();
        // 获取源节点内容、属性及纯文本
        atb.setSrcText(tu.getSource().getPureText());
        atb.setTgtText(tu.getTarget().getPureText());
        Hashtable<String, String> matchProps = new Hashtable<String, String>();
        matchProps.put("match-quality", result.getSimilarity() + "");
        matchProps.put("origin", result.getDbName());
        matchProps.put("tool-id", "Translation Memory");
        matchProps.put("hs:matchType", "TM");
        matchProps.put("xml:space", "default");
        atb.setMatchProps(matchProps);
        Hashtable<String, String> srcProps = new Hashtable<String, String>();
        srcProps.put("xml:lang", tu.getSource().getLangCode());
        atb.setSrcProps(srcProps);
        atb.setSrcContent(tu.getSource().getFullText());
        Hashtable<String, String> tgtProps = new Hashtable<String, String>();
        tgtProps.put("xml:lang", tu.getTarget().getLangCode());
        atb.setTgtProps(tgtProps);
        atb.setTgtContent(tu.getTarget().getFullText());
        Vector<PropGroupBean> pgs = new Vector<PropGroupBean>();
        Vector<PropBean> props = new Vector<PropBean>();
        PropBean pb = new PropBean("creationId", tu.getCreationUser());
        props.add(pb);
        pb = new PropBean("creationDate", tu.getCreationDate());
        props.add(pb);
        pb = new PropBean("changeId", tu.getChangeUser());
        props.add(pb);
        pb = new PropBean("changeDate", tu.getChangeDate());
        props.add(pb);
        List<TmxProp> attrValList = tu.getProps();
        for (TmxProp attr : attrValList) {
            String name = attr.getName();
            if (name == null || name.equals("")) {
                continue;
            }
            String value = attr.getValue();
            if (value == null || value.equals("")) {
                continue;
            }
            PropBean prop = new PropBean(name, value);
            props.add(prop);
        }
        PropGroupBean pg = new PropGroupBean(props);
        // 获取属性组名称。
        pg.setName("hs:prop-group");
        pgs.add(pg);
        atb.setPropGroups(pgs);
        atb.setFuzzyResult(result);
        altTrans.add(atb);
    }
    return altTrans;
}
Also used : Hashtable(java.util.Hashtable) PropBean(net.heartsome.cat.ts.core.bean.PropBean) TmxProp(net.heartsome.cat.common.bean.TmxProp) PropGroupBean(net.heartsome.cat.ts.core.bean.PropGroupBean) AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) FuzzySearchResult(net.heartsome.cat.common.bean.FuzzySearchResult) TmxTU(net.heartsome.cat.common.bean.TmxTU) Vector(java.util.Vector)

Example 2 with AltTransBean

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

the class PreTranslation method generateAltTransUnitNodeXML.

/**
	 * 根据匹配结果生成alt-trans节点
	 * @param altMatchs
	 *             所有匹配信息
	 * @return 生成alt-trans节点;
	 */
private String generateAltTransUnitNodeXML(List<FuzzySearchResult> fuzzyResults) {
    List<AltTransBean> altTransBeans = TmUtils.fuzzyResult2Alttransbean(fuzzyResults);
    StringBuffer bf = new StringBuffer();
    for (AltTransBean bean : altTransBeans) {
        bf.append(bean.toXMLString());
    }
    return bf.toString();
}
Also used : AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean)

Example 3 with AltTransBean

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

the class QuickTranslationImpl method executeTranslation.

/**
	 * (non-Javadoc)
	 * @see net.heartsome.cat.ts.tm.complexMatch.AbstractComplexMatch#executeTranslation()
	 */
public Vector<AltTransBean> executeTranslation(TransUnitBean transUnitBean, IProject currentProject) {
    Vector<AltTransBean> result = new Vector<AltTransBean>();
    // 100%以上的记忆库匹配不做快译
    if (transUnitBean == null) {
        return result;
    }
    Vector<AltTransBean> tmalt = transUnitBean.getMatchesByToolId(Constants.TM_TOOLID);
    if (tmalt.size() < 1) {
        return result;
    }
    AltTransBean tmMatches = tmalt.get(0);
    if (tmMatches == null) {
        return result;
    }
    String tmQuality = tmMatches.getMatchProps().get("match-quality");
    if (tmQuality.endsWith("%")) {
        tmQuality = tmQuality.substring(0, tmQuality.length() - 1);
    }
    int tmQualityInt = Integer.parseInt(tmQuality);
    if (tmQualityInt >= 100) {
        return result;
    }
    String srcLang = tmMatches.getSrcLang();
    String tgtLang = tmMatches.getTgtLang();
    if (srcLang == null || srcLang.equals("")) {
        return result;
    }
    if (tgtLang == null || tgtLang.equals("")) {
        return result;
    }
    String tmSrcText = tmMatches.getSrcText();
    if (tmSrcText == null || tmSrcText.equals("")) {
        return result;
    }
    String tmTgtText = tmMatches.getTgtText();
    if (tmTgtText == null || tmTgtText.equals("")) {
        return result;
    }
    String tuSrcText = transUnitBean.getSrcText();
    if (tuSrcText == null || tuSrcText.equals("")) {
        return result;
    }
    tbMatcher.setCurrentProject(currentProject);
    // 获取翻译单源文中的术语
    Vector<Hashtable<String, String>> tuTerms = findTerms(tuSrcText, srcLang, tgtLang);
    if (tuTerms.size() == 0) {
        return result;
    }
    // 获取记忆库匹配中的术语
    Vector<Hashtable<String, String>> tmTerms = findTerms(tmSrcText, srcLang, tgtLang);
    if (tmTerms.size() == 0) {
        return result;
    }
    int tuTermSize = tuTerms.size();
    int tmTermSize = tmTerms.size();
    if (tuTermSize > tmTermSize) {
        int j = 0;
        while (j < tuTermSize) {
            Hashtable<String, String> tempTuTerm = tuTerms.get(j);
            String tmpTuSrcWord = tempTuTerm.get(CON_SRCWORD);
            for (Hashtable<String, String> tempTmTerm : tmTerms) {
                if (tempTmTerm.get(CON_SRCWORD).equals(tmpTuSrcWord)) {
                    tuTerms.remove(j);
                    tmTerms.remove(tempTmTerm);
                    break;
                }
            }
            tuTermSize = tuTerms.size();
            j++;
        }
    } else {
        int j = 0;
        while (j < tmTermSize) {
            Hashtable<String, String> tempTmTerm = tmTerms.get(j);
            String tmpTmSrcWord = tempTmTerm.get(CON_SRCWORD);
            for (Hashtable<String, String> tempTuTerm : tuTerms) {
                if (tempTuTerm.get(CON_SRCWORD).equals(tmpTmSrcWord)) {
                    tmTerms.remove(j);
                    tuTerms.remove(tempTuTerm);
                    break;
                }
            }
            tmTermSize = tmTerms.size();
            j++;
        }
    }
    tuTermSize = tuTerms.size();
    tmTermSize = tmTerms.size();
    if (tuTermSize == 0 || tmTermSize == 0) {
        return result;
    }
    int replaceSize = tuTermSize < tmTermSize ? tuTermSize : tmTermSize;
    for (int i = 0; i < replaceSize; i++) {
        Hashtable<String, String> tmTerm = tmTerms.get(i);
        String tmTermSrc = tmTerm.get(CON_SRCWORD);
        String tmTermTgt = tmTerm.get(CON_TGTWORD);
        Hashtable<String, String> tuTerm = tuTerms.get(i);
        String tuTermSrc = tuTerm.get(CON_SRCWORD);
        String tuTermTgt = tuTerm.get(CON_TGTWORD);
        tmSrcText = tmSrcText.replace(tmTermSrc, tuTermSrc);
        tmTgtText = tmTgtText.replace(tmTermTgt, tuTermTgt);
    }
    int quality = MatchQuality.similarity(tuSrcText, tmSrcText);
    AltTransBean bean = new AltTransBean(tmSrcText, tmTgtText, srcLang, tgtLang, getMathcerOrigin(), getToolId());
    bean.getMatchProps().put("match-quality", quality + "");
    bean.setSrcContent(tmSrcText);
    bean.setTgtContent(tmTgtText);
    bean.getMatchProps().put("hs:matchType", getMatcherType());
    result.add(bean);
    return result;
}
Also used : AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) Hashtable(java.util.Hashtable) Vector(java.util.Vector)

Example 4 with AltTransBean

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

the class MatchViewPart method loadData.

private synchronized void loadData(List<AltTransBean> matches) {
    if (matches == null) {
        return;
    }
    IEditorReference[] editorReferences = getSite().getPage().getEditorReferences();
    if (editorReferences.length == 0) {
        return;
    }
    for (AltTransBean altTransBean : matches) {
        String type = altTransBean.getMatchProps().get("hs:matchType");
        if (type == null || type.equals("")) {
            type = "others";
        }
        String orgin = altTransBean.getMatchOrigin();
        if (orgin == null) {
            // Fixed bug 2258 by Jason 翻译匹配面板不支持非 HS 工具匹配信息的显示
            orgin = "";
        }
        String srcContent = altTransBean.getSrcContent();
        if (srcContent == null || srcContent.equals("")) {
            continue;
        }
        String tgtContent = altTransBean.getTgtContent();
        if (tgtContent == null || tgtContent.equals("")) {
            continue;
        }
        String quality = altTransBean.getMatchProps().get("match-quality").trim();
        if (quality == null) {
            quality = "";
        } else {
            if (!quality.endsWith("%")) {
                quality += "%";
            }
        }
        String changeDate = null;
        String changeid = null;
        String creationDate = null;
        String creationid = null;
        Vector<PropGroupBean> propGroups = altTransBean.getPropGroups();
        StringBuffer toolTipBfTemp = new StringBuffer();
        if (propGroups != null) {
            for (PropGroupBean propGroupBean : propGroups) {
                List<PropBean> propBeans = propGroupBean.getProps();
                for (PropBean bean : propBeans) {
                    String ptype = bean.getProptype();
                    String pVal = bean.getValue();
                    if (ptype.equals("changeDate")) {
                        if (pVal != null && !pVal.equals("")) {
                            changeDate = DateUtils.formatDateFromUTC(pVal);
                        }
                    } else if (ptype.equals("changeId")) {
                        changeid = pVal;
                    } else if (ptype.equals("creationId")) {
                        creationid = pVal;
                    } else if (ptype.equals("creationDate")) {
                        if (pVal != null && !pVal.equals("")) {
                            creationDate = DateUtils.formatDateFromUTC(pVal);
                        }
                    } else {
                        toolTipBfTemp.append(ptype).append(" : ").append(pVal).append("\n");
                    }
                }
            }
        }
        StringBuffer toolTipBf = new StringBuffer();
        if (creationid != null && !creationid.equals("")) {
            toolTipBf.append(Messages.getString("view.MatchViewPart.info.tooltip.creationId")).append(creationid).append("\n");
        }
        if (creationDate != null && !creationDate.equals("")) {
            toolTipBf.append(Messages.getString("view.MatchViewPart.info.tooltip.creationDate")).append(creationDate).append("\n");
        }
        toolTipBf.append(toolTipBfTemp);
        StringBuffer msgBf = new StringBuffer();
        if (changeDate != null && !changeDate.equals("")) {
            msgBf.append(changeDate);
        }
        if (changeid != null && !changeid.equals("")) {
            if (msgBf.length() != 0) {
                msgBf.append("  |  ");
            }
            msgBf.append(changeid);
        }
        if (orgin != null && !orgin.equals("")) {
            if (msgBf.length() != 0) {
                msgBf.append("  |  ");
            }
            msgBf.append(orgin);
        }
        if (gridTable.isDisposed()) {
            return;
        }
        String toolId = altTransBean.getMatchProps().get("tool-id");
        GridItem gridItem = new GridItem(gridTable, SWT.NONE);
        gridItem.setText(0, srcContent);
        gridItem.setText(1, quality);
        gridItem.setText(2, tgtContent);
        gridItem.setToolTipText(0, toolId);
        gridItem.setToolTipText(1, toolId);
        gridItem.setToolTipText(2, toolId);
        // 保存信息
        gridItem.setData("info", resetSpecialString(msgBf.toString()));
        gridItem.setData("infoTooltip", resetSpecialString(toolTipBf.toString()));
        // 保存目标纯文本
        gridItem.setData("tgtText", altTransBean.getTgtText());
        // 保存目标纯文本
        gridItem.setData("tgtContent", tgtContent);
        gridItem.setData("matchType", type);
        gridItem.setData("quality", quality.substring(0, quality.lastIndexOf('%')));
        gridItem.setData("typeImage", getImageByType(type));
        gridItem.setData("tmFuzzyInfo", altTransBean.getFuzzyResult());
    }
}
Also used : PropGroupBean(net.heartsome.cat.ts.core.bean.PropGroupBean) AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) IEditorReference(org.eclipse.ui.IEditorReference) GridItem(org.eclipse.nebula.widgets.grid.GridItem) PropBean(net.heartsome.cat.ts.core.bean.PropBean)

Example 5 with AltTransBean

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

the class TSFileHandler method openFile.

@Override
public Map<String, Object> openFile(File file, int tuCount) {
    long start = System.currentTimeMillis();
    // 验证文件是否存在
    if (file == null || !file.exists()) {
        String errorMsg = Messages.getString("file.TSFileHandler.logger5");
        logger.error(errorMsg);
        return getErrorResult(errorMsg, null);
    }
    // 判断是否还有缓存空间。
    boolean canCache = tuCount < TU_CACHE_SIZE;
    // 当前文件中解析并缓存翻译单元计数器。
    int parsedTuCount = 0;
    // 当前文件未解析缓存的翻译单元计数器。
    int noParseTuCount = 0;
    String filename = file.getAbsolutePath();
    int fileIndex = 1;
    // 解析文件并获取索引
    VTDGen vgRead = new VTDGen();
    if (vgRead.parseFile(filename, true)) {
        VTDNav vnRead = vgRead.getNav();
        VTDUtils vu = null;
        try {
            vu = new VTDUtils(vnRead);
            // 创建临时文件
            File tmpFile = createTmpFile();
            XMLModifier xm = new XMLModifier(vnRead);
            FileOutputStream fos = new FileOutputStream(tmpFile);
            xm.output(fos);
            fos.close();
            tmpFileMap.put(filename, tmpFile.getAbsolutePath());
            filesChangeStatus.put(filename, false);
        } catch (ModifyException e) {
            String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger6"), filename);
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (TranscodeException e) {
            String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger7"), filename);
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (IOException e) {
            String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger8"), filename);
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (NavException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger9");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        }
        // 创建翻译单元集合缓存。
        Vector<TransUnitBean> tusCache = new Vector<TransUnitBean>();
        // 创建翻译单元索引集合缓存。
        Vector<String> tuIndexCache = new Vector<String>();
        // 初始化文件节点属性集合。
        fileAttrs = new Hashtable<Integer, Hashtable<String, String>>();
        AutoPilot apFile = new AutoPilot(vnRead);
        String fileNode = "/xliff/file";
        try {
            apFile.selectXPath(fileNode);
            while (apFile.evalXPath() != -1) {
                fileAttrs.put(fileIndex, vu.getCurrentElementAttributs());
                AutoPilot apTU = new AutoPilot(vnRead);
                apTU.selectXPath("body//trans-unit");
                vnRead.push();
                while (apTU.evalXPath() != -1) {
                    // 如果缓冲区未满,则解析文件内容并缓存,否则只计数,不解析内容。
                    if (canCache) {
                        String tuid = "";
                        String srcText = "";
                        String srcContent = "";
                        String tgtText = "";
                        String tgtContent = "";
                        Hashtable<String, String> srcProps = null;
                        Hashtable<String, String> tgtProps = null;
                        // 取翻译单元所有属性
                        String tmpNode = "";
                        vnRead.push();
                        Hashtable<String, String> tuProps = vu.getCurrentElementAttributs();
                        vnRead.pop();
                        tuid = tuProps.get("id");
                        // 取翻译单元源节点完整文本,含内部标记。
                        vnRead.push();
                        tmpNode = "./source";
                        srcContent = vu.getElementContent(tmpNode);
                        // vnRead.pop();
                        // 取翻译单元源文本。
                        // vnRead.push();
                        srcText = vu.getElementPureText();
                        // 取翻译单元源节点属性。
                        srcProps = vu.getCurrentElementAttributs();
                        vnRead.pop();
                        // 取翻译单元目标节点完整文本,含内部标记。
                        vnRead.push();
                        tmpNode = "./target";
                        tgtContent = vu.getElementContent(tmpNode);
                        // vnRead.pop();
                        // 取翻译单元目标文本。
                        // vnRead.push();
                        tgtText = vu.getElementPureText();
                        // 取翻译单元目标节点属性。
                        tgtProps = vu.getCurrentElementAttributs();
                        vnRead.pop();
                        // 获取所有的 alttrans 匹配节点。
                        vnRead.push();
                        Vector<AltTransBean> matches = getAltTrans(vu);
                        vnRead.pop();
                        // 构建翻译单元对象,存储节点信息
                        TransUnitBean tub = new TransUnitBean(tuid, srcContent, srcText);
                        tub.setTuProps(tuProps);
                        tub.setSrcProps(srcProps);
                        tub.setTgtContent(tgtContent);
                        tub.setTgtText(tgtText);
                        tub.setTgtProps(tgtProps);
                        tub.setMatches(matches);
                        vnRead.push();
                        tub.setNotes(getNotes(vu));
                        vnRead.pop();
                        vnRead.push();
                        tub.setPropgroups(getPrpoGroups(vu));
                        vnRead.pop();
                        tusCache.add(tub);
                        tuIndexCache.add(filename + ";" + fileIndex + ";" + tuid);
                        // 解析的翻译单元节点计数
                        parsedTuCount++;
                        if (tuCount + parsedTuCount == TU_CACHE_SIZE) {
                            canCache = false;
                        }
                    } else {
                        // 未解析的翻译单元节点计数
                        noParseTuCount++;
                    }
                }
                vnRead.pop();
                // 文件节点索引计数
                fileIndex++;
            }
            transunits.put(filename, tusCache);
            tuIndexs.put(filename, tuIndexCache);
            actualTuCount.put(filename, parsedTuCount + noParseTuCount);
            accessHistory.put(filename, "");
        } catch (XPathEvalException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger10");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (NavException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger11");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (XPathParseException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger12");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        }
    } else {
        String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger13"), filename);
        logger.error(errorMsg);
        return getErrorResult(errorMsg, null);
    }
    long end = System.currentTimeMillis();
    // 输出结果
    long resultMS = end - start;
    long resultS = resultMS / 1000;
    long resultM = resultMS / (1000 * 60);
    System.gc();
    logger.info(Messages.getString("file.TSFileHandler.logger14"), new Object[] { resultM, resultS, resultMS });
    Map<String, Object> result = getSuccessResult();
    result.put("CurCachedTuCount", Integer.valueOf(parsedTuCount));
    result.put("TotalCachedTuCount", Integer.valueOf(parsedTuCount + tuCount));
    return result;
}
Also used : XMLModifier(com.ximpleware.XMLModifier) XPathParseException(com.ximpleware.XPathParseException) TransUnitBean(net.heartsome.cat.ts.core.bean.TransUnitBean) AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) ModifyException(com.ximpleware.ModifyException) Vector(java.util.Vector) Hashtable(java.util.Hashtable) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) VTDNav(com.ximpleware.VTDNav) File(java.io.File)

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