Search in sources :

Example 6 with TmxTU

use of net.heartsome.cat.common.bean.TmxTU in project translationstudio8 by heartsome.

the class Excel2007Writer method witerTmxTU.

public void witerTmxTU(List<TmxTU> tus, boolean writeFullText) throws IOException {
    if (null == tus || tus.isEmpty()) {
        return;
    }
    TmxTU tu = null;
    List<TmxSegement> temp = new ArrayList<TmxSegement>(5);
    int i = 0;
    for (; i < tus.size(); i++) {
        temp.clear();
        tu = tus.get(i);
        if (tu.getSource() != null) {
            temp.add(tu.getSource());
        }
        if (tu.getSegments() != null) {
            temp.addAll(tu.getSegments());
        }
        Row row = sh.createRow((lastIndex + i));
        // 为每一行添加数据
        for (TmxSegement segment : temp) {
            int cellIndex = getLangIndex(rowHeader, segment);
            if (-1 == cellIndex) {
                cellIndex = addLangCell(rowHeader, segment);
            }
            RichTextString createRichTextString = null;
            if (writeFullText) {
                createRichTextString = createHelper.createRichTextString(segment.getFullText());
            } else {
                createRichTextString = createHelper.createRichTextString(segment.getPureText());
            }
            Cell createCell = row.createCell(cellIndex);
            createCell.setCellStyle(getWrapedCell());
            createCell.setCellValue(createRichTextString);
        }
    }
    lastIndex = lastIndex + i;
    // :使用固定的列宽
    // 设置宽度:此处比较耗时
    // for(Cell cell : rowHeader){
    // sheet.autoSizeColumn(cell.getColumnIndex());
    // }
    tus.clear();
}
Also used : TmxTU(net.heartsome.cat.common.bean.TmxTU) ArrayList(java.util.ArrayList) Row(org.apache.poi.ss.usermodel.Row) RichTextString(org.apache.poi.ss.usermodel.RichTextString) TmxSegement(net.heartsome.cat.common.bean.TmxSegement) Cell(org.apache.poi.ss.usermodel.Cell)

Example 7 with TmxTU

use of net.heartsome.cat.common.bean.TmxTU in project translationstudio8 by heartsome.

the class Tmx2xlsx method doCovnerter.

public void doCovnerter(String tmxFile, File targetFile, IProgressMonitor monitor) throws Exception {
    writer = new Excel2007Writer(targetFile, size);
    try {
        tmxReader = new TmxReader(new File(tmxFile));
    } catch (TmxReadException e) {
        LOGGER.error("", e);
        throw new Exception(Messages.getString("tmxdata.TmxFileContainer.parseTmxFileError"));
    }
    TmxTU tuTemp = null;
    List<TmxTU> tus = new ArrayList<TmxTU>(100);
    int total = tmxReader.getTotalTu() / size;
    monitor.beginTask("", total == 0 ? 1 : total);
    try {
        while (true) {
            tuTemp = tmxReader.read().getTu();
            if (null == tuTemp) {
                break;
            }
            if (tus.size() < size) {
                tus.add(tuTemp);
                continue;
            }
            try {
                if (monitor.isCanceled()) {
                    return;
                }
                writer.witerTmxTU(tus, false);
                monitor.worked(1);
                tus.clear();
            } catch (IOException e) {
                LOGGER.error("写入文件出错", e);
                throw new Exception(Messages.getString("converter.tmx2xlsx.writeTmx.error"));
            }
        }
        if (!tus.isEmpty()) {
            try {
                if (monitor.isCanceled()) {
                    return;
                }
                writer.witerTmxTU(tus, false);
                monitor.done();
                tus.clear();
            } catch (IOException e) {
                LOGGER.error("写入文件出错", e);
                throw new Exception(Messages.getString("converter.tmx2xlsx.writeTmx.error"));
            }
        }
    } catch (Exception e) {
        throw e;
    } finally {
        writer.outZip();
        monitor.done();
    }
}
Also used : TmxTU(net.heartsome.cat.common.bean.TmxTU) TmxReader(net.heartsome.cat.document.TmxReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AbstractTmx2File(net.heartsome.cat.tmx.converter.AbstractTmx2File) File(java.io.File) TmxReadException(net.heartsome.cat.document.TmxReadException) IOException(java.io.IOException) TmxReadException(net.heartsome.cat.document.TmxReadException)

Example 8 with TmxTU

use of net.heartsome.cat.common.bean.TmxTU in project translationstudio8 by heartsome.

the class ImportTmx method filterSrcSameTu.

/**
	 * @param cache
	 * @param importStrategy
	 * @return ;
	 */
private List<TmxTU> filterSrcSameTu(List<TmxTU> cache, int importStrategy) {
    if (null == cache || cache.isEmpty()) {
        return cache;
    }
    Map<String, List<TmxTU>> map = new HashMap<String, List<TmxTU>>();
    String srcText = "";
    for (TmxTU tu : cache) {
        srcText = tu.getSource().getPureText();
        if (map.get(srcText) != null) {
            map.get(srcText).add(tu);
        } else {
            List<TmxTU> temp = new ArrayList<TmxTU>();
            map.put(srcText, temp);
            temp.add(tu);
        }
    }
    List<TmxTU> rs = new ArrayList<TmxTU>();
    Iterator<List<TmxTU>> iterator = map.values().iterator();
    while (iterator.hasNext()) {
        List<TmxTU> next = iterator.next();
        if (importStrategy == Constants.IMPORT_MODEL_IGNORE) {
            rs.add(next.get(0));
        } else {
            rs.add(next.get(next.size() - 1));
        }
    }
    return rs;
}
Also used : TmxTU(net.heartsome.cat.common.bean.TmxTU) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with TmxTU

use of net.heartsome.cat.common.bean.TmxTU in project translationstudio8 by heartsome.

the class TmxReader method read.

public TmxReaderEvent read() {
    TmxTU tu = null;
    try {
        if (tuAp.evalXPath() != -1) {
            tu = new TmxTU();
            readTuElementAttribute(tu);
            readTuNoteElement(tu);
            readTuPropElement(tu);
            readTuTuvElement(tu);
        } else {
            return new TmxReaderEvent(null, TmxReaderEvent.END_FILE);
        }
    } catch (VTDException e) {
        return new TmxReaderEvent(null, TmxReaderEvent.READ_EXCEPTION);
    }
    return new TmxReaderEvent(tu, TmxReaderEvent.NORMAL_READ);
}
Also used : TmxTU(net.heartsome.cat.common.bean.TmxTU) VTDException(com.ximpleware.VTDException)

Example 10 with TmxTU

use of net.heartsome.cat.common.bean.TmxTU in project translationstudio8 by heartsome.

the class TMDatabaseImpl method fuzzySearch.

@Override
public void fuzzySearch(String pureText, String fullText, String srcLang, String tgtLang, int minSimilarity, boolean caseSensitive, int matchUpperLimit, int contextSize, String preHash, String nextHash, boolean isIngoreTarget, FuzzySearchResults searchResults, int tagPelanty) throws SQLException {
    int[] ngrams = generateNgrams(srcLang, pureText);
    int size = ngrams.length;
    if (size == 0) {
        return;
    }
    // long l1 = System.currentTimeMillis();
    int min = size * minSimilarity / 100;
    int max = size * 100 / minSimilarity;
    Map<String, Integer> tpkids = getCandidatesTextDataPks(srcLang, min, max, ngrams);
    // System.out.println("查MATEX_LANG表:"+(System.currentTimeMillis() - l1));
    // 构建SQL
    Iterator<Entry<String, Integer>> it = tpkids.entrySet().iterator();
    StringBuffer bf = new StringBuffer();
    // long l = System.currentTimeMillis();
    while (it.hasNext()) {
        Entry<String, Integer> entry = it.next();
        String tpkid = entry.getKey();
        float c = entry.getValue();
        if (c >= min && c <= max) {
            bf.append(",");
            bf.append(tpkid);
        }
    }
    if (bf.toString().equals("")) {
        return;
    }
    String tag = TranslationMemoryTools.getInnerTagContent(fullText);
    String textDataSql = dbConfig.getOperateDbSQL("fuzzySearch");
    textDataSql = textDataSql.replace("__SET__", bf.toString().substring(1));
    Statement stm = null;
    ResultSet rs = null;
    Statement tmpStm = null;
    try {
        stm = conn.createStatement();
        tmpStm = conn.createStatement();
        rs = stm.executeQuery(textDataSql);
        // SELECT TPKID, GROUPID, PURE, CONTENT, PRECONTEXT, NEXTCONTEXT FROM TEXTDATA WHERE TPKID IN (__SET__)
        String targetSql = dbConfig.getOperateDbSQL("fuzzySearch-target").replace("__LANG__", tgtLang);
        String dbName = getMetaData().getDatabaseName();
        while (rs.next()) {
            String _pureText = rs.getString(3);
            String _fullText = rs.getString(4);
            int similarity = 0;
            if (caseSensitive) {
                similarity = similarity(pureText, _pureText);
            } else {
                similarity = similarity(pureText.toLowerCase(), _pureText.toLowerCase());
            }
            String _tag = TranslationMemoryTools.getInnerTagContent(_fullText);
            if (!isIngoreTarget && !tag.equals(_tag)) {
                // 标记内容不相等,则执行罚分
                similarity -= tagPelanty;
            }
            if (similarity < minSimilarity) {
                continue;
            }
            int tuId = rs.getInt(2);
            String temptargetSql = targetSql.replace("__GROUPID__", tuId + "");
            // PURE, CONTENT, CREATIONID, CREATIONDATE, CHANGEID, CHANGEDATE ,PROJECTREF
            ResultSet rs1 = null;
            try {
                rs1 = tmpStm.executeQuery(temptargetSql);
                if (rs1.next()) {
                    TmxSegement source = new TmxSegement(_pureText, _fullText, srcLang);
                    source.setDbPk(rs.getInt(1));
                    _pureText = rs1.getString(2);
                    _fullText = rs1.getString(3);
                    if (_pureText == null || _pureText.equals("") || _fullText == null || _fullText.equals("")) {
                        continue;
                    }
                    TmxSegement target = new TmxSegement(_pureText, _fullText, tgtLang);
                    target.setDbPk(rs1.getInt(1));
                    TmxTU tu = new TmxTU(source, target);
                    FuzzySearchResult searchRs = new FuzzySearchResult(tu);
                    if (searchResults.contains(searchRs)) {
                        continue;
                    }
                    String creationId = rs1.getString(4);
                    creationId = creationId == null ? "" : creationId;
                    String creationDate = "";
                    Timestamp tempCdate = rs1.getTimestamp(5);
                    if (tempCdate != null) {
                        creationDate = DateUtils.formatToUTC(tempCdate.getTime());
                    }
                    String changeid = rs1.getString(6);
                    changeid = changeid == null ? "" : changeid;
                    String changeDate = "";
                    Timestamp tempChangeDate = rs1.getTimestamp(7);
                    if (tempChangeDate != null) {
                        changeDate = DateUtils.formatToUTC(tempChangeDate.getTime());
                    }
                    String projectRef = rs1.getString(8);
                    projectRef = projectRef == null ? "" : projectRef;
                    tu.setCreationDate(creationDate);
                    tu.setCreationUser(creationId);
                    tu.setChangeDate(changeDate);
                    tu.setChangeUser(changeid);
                    List<TmxProp> attrs = getTuMprops(tuId, "TU");
                    tu.setProps(attrs);
                    String preContext = rs.getString(5);
                    String nextContext = rs.getString(6);
                    tu.appendContext(TmxContexts.PRE_CONTEXT_NAME, preContext);
                    tu.appendContext(TmxContexts.NEXT_CONTEXT_NAME, nextContext);
                    if (similarity == 100 && CommonFunction.checkEdition("U")) {
                        if (preContext != null && nextContext != null) {
                            String[] preContexts = preContext.split(",");
                            String[] nextContexts = nextContext.split(",");
                            if (preContexts.length > contextSize) {
                                //$NON-NLS-1$
                                preContext = "";
                                for (int i = 0; i < contextSize; i++) {
                                    //$NON-NLS-1$
                                    preContext += "," + preContexts[i];
                                }
                                if (!"".equals(preContext)) {
                                    //$NON-NLS-1$
                                    preContext = preContext.substring(1);
                                }
                            }
                            if (nextContexts.length > contextSize) {
                                //$NON-NLS-1$
                                nextContext = "";
                                for (int i = 0; i < contextSize; i++) {
                                    //$NON-NLS-1$
                                    nextContext += "," + nextContexts[i];
                                }
                                if (!"".equals(nextContext)) {
                                    //$NON-NLS-1$
                                    nextContext = nextContext.substring(1);
                                }
                            }
                            if (preHash.equals(preContext) && nextHash.equals(nextContext)) {
                                similarity = 101;
                            }
                        }
                    }
                    searchRs.setDbName(dbName);
                    searchRs.setSimilarity(similarity);
                    searchRs.setDbOp(this);
                    searchRs.getTu().setTmId(tuId);
                    searchResults.add(searchRs);
                }
            } finally {
                if (rs1 != null) {
                    rs1.close();
                }
            }
        }
    } finally {
        if (rs != null) {
            rs.close();
        }
        if (stm != null) {
            stm.close();
        }
        if (tmpStm != null) {
            tmpStm.close();
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) TmxSegement(net.heartsome.cat.common.bean.TmxSegement) Timestamp(java.sql.Timestamp) TmxProp(net.heartsome.cat.common.bean.TmxProp) Entry(java.util.Map.Entry) FuzzySearchResult(net.heartsome.cat.common.bean.FuzzySearchResult) TmxTU(net.heartsome.cat.common.bean.TmxTU) ResultSet(java.sql.ResultSet)

Aggregations

TmxTU (net.heartsome.cat.common.bean.TmxTU)12 TmxSegement (net.heartsome.cat.common.bean.TmxSegement)7 TmxProp (net.heartsome.cat.common.bean.TmxProp)6 ArrayList (java.util.ArrayList)5 FuzzySearchResult (net.heartsome.cat.common.bean.FuzzySearchResult)5 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 Timestamp (java.sql.Timestamp)3 HashMap (java.util.HashMap)2 Hashtable (java.util.Hashtable)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 Vector (java.util.Vector)2 AltTransBean (net.heartsome.cat.ts.core.bean.AltTransBean)2 PropBean (net.heartsome.cat.ts.core.bean.PropBean)2 PropGroupBean (net.heartsome.cat.ts.core.bean.PropGroupBean)2 VTDException (com.ximpleware.VTDException)1 File (java.io.File)1 IOException (java.io.IOException)1