Search in sources :

Example 31 with Entry

use of java.util.Map.Entry in project translationstudio8 by heartsome.

the class TMDatabaseImpl method insertBAttribute.

/**
	 * 写BAttribute的内容
	 * @param attrs
	 * @param parentName
	 * @param parentId
	 * @throws SQLException
	 *             ;
	 */
public void insertBAttribute(Map<String, String> attrs, String parentName, int parentId) throws SQLException {
    if (attrs != null) {
        PreparedStatement stmt = null;
        String sql = dbConfig.getOperateDbSQL("insert-battribute");
        Iterator<Entry<String, String>> iter = attrs.entrySet().iterator();
        try {
            while (iter.hasNext()) {
                Entry<String, String> entry = iter.next();
                String attrName = entry.getKey();
                String attrValue = entry.getValue();
                stmt = conn.prepareStatement(sql);
                stmt.setInt(1, parentId);
                stmt.setString(2, attrName);
                stmt.setString(3, attrValue);
                stmt.setString(4, parentName);
                stmt.addBatch();
            }
            stmt.executeBatch();
            stmt.clearBatch();
        } finally {
            if (stmt != null) {
                stmt.close();
            }
        }
    }
}
Also used : Entry(java.util.Map.Entry) OraclePreparedStatement(oracle.jdbc.OraclePreparedStatement) PreparedStatement(java.sql.PreparedStatement)

Example 32 with Entry

use of java.util.Map.Entry in project translationstudio8 by heartsome.

the class TMDatabaseImpl method translationUnitAnalysis.

@Override
public void translationUnitAnalysis(String pureText, String fullText, String srcLang, String tgtLang, int minSimilarity, boolean caseSensitive, int matchUpperLimit, int contextSize, String preHash, String nextHash, boolean isIngoreTarget, TranslationUnitAnalysisResults analysisResults, 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-wordsFA");
    textDataSql = textDataSql.replace("__SET__", bf.toString().substring(1));
    textDataSql = textDataSql.replace("__TARGETLANG__", tgtLang);
    Statement stm = null;
    ResultSet rs = null;
    Statement tmpStm = null;
    try {
        stm = conn.createStatement();
        tmpStm = conn.createStatement();
        rs = stm.executeQuery(textDataSql);
        // SELECT GROUPID, PURE, CONTENT, PRECONTEXT, NEXTCONTEXT FROM TEXTDATA WHERE TPKID IN (__SET__)
        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;
            }
            if (similarity == 100 && CommonFunction.checkEdition("U")) {
                String preContext = rs.getString(5);
                String nextContext = rs.getString(6);
                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;
                    }
                }
            }
            TranslationUnitAnalysisResult r = new TranslationUnitAnalysisResult(similarity, dbName);
            analysisResults.add(r);
        }
    } 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) TranslationUnitAnalysisResult(net.heartsome.cat.common.bean.TranslationUnitAnalysisResult) Entry(java.util.Map.Entry) ResultSet(java.sql.ResultSet)

Example 33 with Entry

use of java.util.Map.Entry in project translationstudio8 by heartsome.

the class TMDatabaseImpl method findMatch_1.

@Override
public Vector<Hashtable<String, String>> findMatch_1(String puretext, String fullText, String srcLang, String tgtLang, int minSimilarity, boolean caseSensitive, int matchUpperLimit, int contextSize, String preHash, String nextHash, boolean isIngoreTarget) throws SQLException {
    Vector<Hashtable<String, String>> result = new Vector<Hashtable<String, String>>();
    int[] ngrams = generateNgrams(srcLang, puretext);
    int size = ngrams.length;
    if (size == 0) {
        return result;
    }
    // long l1 = System.currentTimeMillis();
    List<String> tpkids = getCandidatesTextDataPks4Oracle(srcLang, minSimilarity, ngrams);
    // System.out.println("查MATEX_LANG表:" + (System.currentTimeMillis() - l1));
    // long l = System.currentTimeMillis();
    // 过虑拆分条件,解决Oracle中where xx in (参数) 参数不越过1000个
    StringBuffer bf = new StringBuffer();
    List<String> tmpTpkids = new ArrayList<String>();
    for (int i = 0; i < tpkids.size(); i++) {
        String tpkid = tpkids.get(i);
        bf.append(",");
        bf.append(tpkid);
        if ((i + 1) % 100 == 0) {
            tmpTpkids.add(bf.toString().substring(1));
            bf = new StringBuffer();
        }
    }
    if (bf.toString().equals("")) {
        return result;
    }
    tmpTpkids.add(bf.toString().substring(1));
    bf = new StringBuffer();
    bf.append("TPKID IN (" + tmpTpkids.get(0) + ")");
    for (int i = 1; i < tmpTpkids.size(); i++) {
        bf.append(" OR TPKID IN (" + tmpTpkids.get(i) + ")");
    }
    String textDataSql = dbConfig.getOperateDbSQL("getTMMatch1");
    textDataSql = textDataSql.replace("__WHERE__", bf.toString());
    PreparedStatement stmt = conn.prepareStatement(textDataSql);
    stmt.setString(1, srcLang);
    stmt.setString(2, tgtLang);
    ResultSet rs = stmt.executeQuery();
    Map<Integer, Map<String, String>> tuSrc = new HashMap<Integer, Map<String, String>>();
    Map<Integer, Map<String, String>> tuTgt = new HashMap<Integer, Map<String, String>>();
    while (rs.next()) {
        Integer groupId = rs.getInt("GROUPID");
        String lang = rs.getString("LANG");
        String pureText = rs.getString("PURE");
        String content = rs.getString("CONTENT");
        String creationId = rs.getString("CREATIONID");
        creationId = creationId == null ? "" : creationId;
        String creationDate = "";
        Timestamp tempCdate = rs.getTimestamp("CREATIONDATE");
        if (tempCdate != null) {
            creationDate = DateUtils.formatToUTC(tempCdate.getTime());
        }
        String changeDate = "";
        Timestamp tempChangeDate = rs.getTimestamp("CHANGEDATE");
        if (tempChangeDate != null) {
            changeDate = DateUtils.formatToUTC(tempChangeDate.getTime());
        }
        String changeid = rs.getString("CHANGEID");
        changeid = changeid == null ? "" : changeid;
        String projectRef = rs.getString("PROJECTREF");
        projectRef = projectRef == null ? "" : projectRef;
        String jobRef = rs.getString("JOBREF");
        jobRef = jobRef == null ? "" : jobRef;
        String client = rs.getString("CLIENT");
        client = client == null ? "" : client;
        if (lang.equalsIgnoreCase(srcLang)) {
            int distance;
            if (caseSensitive) {
                if (isIngoreTarget) {
                    distance = similarity(puretext, pureText);
                } else {
                    distance = similarity(fullText, content);
                }
            } else {
                if (isIngoreTarget) {
                    distance = similarity(puretext.toLowerCase(), pureText.toLowerCase());
                } else {
                    distance = similarity(fullText.toLowerCase(), content.toLowerCase());
                }
            }
            if (distance == 100 && CommonFunction.checkEdition("U")) {
                String preContext = rs.getString("PRECONTEXT");
                preContext = preContext == null ? "" : preContext;
                String nextContext = rs.getString("NEXTCONTEXT");
                nextContext = nextContext == null ? "" : nextContext;
                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)) {
                        distance = 101;
                    }
                }
            }
            if (distance >= minSimilarity) {
                Map<String, String> srcMap = new HashMap<String, String>();
                srcMap.put("srcLang", lang);
                srcMap.put("srcText", pureText);
                srcMap.put("srcContent", content);
                srcMap.put("srcCreationId", creationId);
                srcMap.put("srcCreationDate", creationDate);
                srcMap.put("srcChangeId", changeid);
                srcMap.put("srcChangeDate", changeDate);
                srcMap.put("srcProjectRef", projectRef);
                srcMap.put("srcJobRef", jobRef);
                srcMap.put("srcClient", client);
                srcMap.put("similarity", distance + "");
                tuSrc.put(groupId, srcMap);
            }
        }
        if (lang.equalsIgnoreCase(tgtLang)) {
            Map<String, String> tgtMap = new HashMap<String, String>();
            tgtMap.put("tgtLang", lang);
            tgtMap.put("tgtText", pureText);
            tgtMap.put("tgtContent", content);
            tgtMap.put("tgtCreationId", creationId);
            tgtMap.put("tgtCreationDate", creationDate);
            tgtMap.put("tgtChangeId", changeid);
            tgtMap.put("tgtChangeDate", changeDate);
            tgtMap.put("tgtProjectRef", projectRef);
            tgtMap.put("tgtJobRef", jobRef);
            tgtMap.put("tgtClient", client);
            tuTgt.put(groupId, tgtMap);
        }
    }
    stmt.close();
    String dbName = getMetaData().getDatabaseName();
    if (tuSrc.size() > 0) {
        Iterator<Entry<Integer, Map<String, String>>> itr = tuSrc.entrySet().iterator();
        while (itr.hasNext()) {
            Entry<Integer, Map<String, String>> entry = itr.next();
            Integer key = entry.getKey();
            Map<String, String> srcMap = entry.getValue();
            Map<String, String> tgtMap = tuTgt.get(key);
            if (tgtMap == null) {
                continue;
            }
            Hashtable<String, String> tu = new Hashtable<String, String>();
            tu.putAll(srcMap);
            tu.putAll(tgtMap);
            // 应用于origin属性
            tu.put("dbName", dbName);
            result.add(tu);
        }
    }
    int resultSize = result.size();
    if (resultSize > 1) {
        Collections.sort(result, new FindMatchComparator());
    }
    while (resultSize > matchUpperLimit) {
        resultSize--;
        result.remove(resultSize);
    }
    // System.out.println(bf.toString());
    return result;
}
Also used : HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) OraclePreparedStatement(oracle.jdbc.OraclePreparedStatement) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Entry(java.util.Map.Entry) ResultSet(java.sql.ResultSet) Vector(java.util.Vector) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with Entry

use of java.util.Map.Entry in project guava by hceylan.

the class PopulatedCachesTest method testKeySet_populated.

public void testKeySet_populated() {
    for (LoadingCache<Object, Object> cache : caches()) {
        Set<Object> keys = cache.asMap().keySet();
        List<Entry<Object, Object>> warmed = warmUp(cache);
        Object[] expectedArray = Maps.newHashMap(cache.asMap()).keySet().toArray(new Object[0]);
        ASSERT.that(keys).hasContentsAnyOrder(expectedArray);
        ASSERT.that(asList(keys.toArray())).hasContentsAnyOrder(expectedArray);
        ASSERT.that(asList(keys.toArray(new Object[(int) cache.size()]))).hasContentsAnyOrder(expectedArray);
        new EqualsTester().addEqualityGroup(cache.asMap().keySet(), keys).addEqualityGroup(ImmutableSet.of()).testEquals();
        assertEquals(WARMUP_SIZE, keys.size());
        for (int i = WARMUP_MIN; i < WARMUP_MAX; i++) {
            Object key = warmed.get(i - WARMUP_MIN).getKey();
            assertTrue(keys.contains(key));
            assertTrue(keys.remove(key));
            assertFalse(keys.remove(key));
            assertFalse(keys.contains(key));
        }
        checkEmpty(keys);
        checkEmpty(cache);
    }
}
Also used : Entry(java.util.Map.Entry) EqualsTester(com.google.common.testing.EqualsTester)

Example 35 with Entry

use of java.util.Map.Entry in project translationstudio8 by heartsome.

the class DBOperator method findMatch_1.

public Vector<Hashtable<String, String>> findMatch_1(String text, String fullText, String srcLang, String tgtLang, int minSimilarity, boolean caseSensitive, int matchUpperLimit, int contextSize, String preHash, String nextHash, boolean isIngoreTarget) throws SQLException {
    Vector<Hashtable<String, String>> result = new Vector<Hashtable<String, String>>();
    this.commit();
    int[] ngrams = generateNgrams(srcLang, text);
    int size = ngrams.length;
    if (size == 0) {
        return result;
    }
    // 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 result;
    }
    String textDataSql = dbConfig.getOperateDbSQL("getTMMatch1");
    textDataSql = textDataSql.replace("__SET__", bf.toString().substring(1));
    PreparedStatement stmt = conn.prepareStatement(textDataSql);
    stmt.setString(1, srcLang);
    stmt.setString(2, tgtLang);
    // System.out.println(stmt);
    ResultSet rs = stmt.executeQuery();
    Map<Integer, Map<String, String>> tuSrc = new HashMap<Integer, Map<String, String>>();
    Map<Integer, Map<String, String>> tuTgt = new HashMap<Integer, Map<String, String>>();
    while (rs.next()) {
        Integer groupId = rs.getInt("GROUPID");
        String lang = rs.getString("LANG");
        String pureText = rs.getString("PURE");
        String content = rs.getString("CONTENT");
        String creationId = rs.getString("CREATIONID");
        creationId = creationId == null ? "" : creationId;
        String creationDate = "";
        Timestamp tempCdate = rs.getTimestamp("CREATIONDATE");
        if (tempCdate != null) {
            creationDate = DateUtils.formatToUTC(tempCdate.getTime());
        }
        String changeDate = "";
        Timestamp tempChangeDate = rs.getTimestamp("CHANGEDATE");
        if (tempChangeDate != null) {
            changeDate = DateUtils.formatToUTC(tempChangeDate.getTime());
        }
        String changeid = rs.getString("CHANGEID");
        changeid = changeid == null ? "" : changeid;
        String projectRef = rs.getString("PROJECTREF");
        projectRef = projectRef == null ? "" : projectRef;
        String jobRef = rs.getString("JOBREF");
        jobRef = jobRef == null ? "" : jobRef;
        String client = rs.getString("CLIENT");
        client = client == null ? "" : client;
        if (lang.equalsIgnoreCase(srcLang)) {
            int distance;
            if (caseSensitive) {
                if (isIngoreTarget) {
                    distance = similarity(text, pureText);
                } else {
                    distance = similarity(fullText, content);
                }
            } else {
                if (isIngoreTarget) {
                    distance = similarity(text.toLowerCase(), pureText.toLowerCase());
                } else {
                    distance = similarity(fullText.toLowerCase(), content.toLowerCase());
                }
            }
            if (distance == 100 && CommonFunction.checkEdition("U")) {
                String preContext = rs.getString("PRECONTEXT");
                String nextContext = rs.getString("NEXTCONTEXT");
                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)) {
                        distance = 101;
                    }
                }
            }
            if (distance >= minSimilarity) {
                Map<String, String> srcMap = new HashMap<String, String>();
                srcMap.put("srcLang", srcLang);
                srcMap.put("srcText", pureText);
                srcMap.put("srcContent", content);
                srcMap.put("srcCreationId", creationId);
                srcMap.put("srcCreationDate", creationDate);
                srcMap.put("srcChangeId", changeid);
                srcMap.put("srcChangeDate", changeDate);
                srcMap.put("srcProjectRef", projectRef);
                srcMap.put("srcJobRef", jobRef);
                srcMap.put("srcClient", client);
                srcMap.put("similarity", distance + "");
                tuSrc.put(groupId, srcMap);
            }
        }
        if (lang.equalsIgnoreCase(tgtLang)) {
            Map<String, String> tgtMap = new HashMap<String, String>();
            tgtMap.put("tgtLang", tgtLang);
            tgtMap.put("tgtText", pureText);
            tgtMap.put("tgtContent", content);
            tgtMap.put("tgtCreationId", creationId);
            tgtMap.put("tgtCreationDate", creationDate);
            tgtMap.put("tgtChangeId", changeid);
            tgtMap.put("tgtChangeDate", changeDate);
            tgtMap.put("tgtProjectRef", projectRef);
            tgtMap.put("tgtJobRef", jobRef);
            tgtMap.put("tgtClient", client);
            tuTgt.put(groupId, tgtMap);
        }
    }
    if (stmt != null) {
        stmt.close();
    }
    if (rs != null) {
        rs.close();
    }
    String dbName = getMetaData().getDatabaseName();
    if (tuSrc.size() > 0) {
        Iterator<Entry<Integer, Map<String, String>>> itr = tuSrc.entrySet().iterator();
        while (itr.hasNext()) {
            Entry<Integer, Map<String, String>> entry = itr.next();
            Integer key = entry.getKey();
            Map<String, String> srcMap = entry.getValue();
            Map<String, String> tgtMap = tuTgt.get(key);
            if (tgtMap == null) {
                continue;
            }
            Hashtable<String, String> tu = new Hashtable<String, String>();
            tu.putAll(srcMap);
            tu.putAll(tgtMap);
            if (!isDuplicated(result, tu)) {
                tu.put("tupkid", key + "");
                // 应用于origin属性
                tu.put("dbName", dbName);
                result.add(tu);
            }
        }
    }
    int resultSize = result.size();
    if (resultSize > 1) {
        Collections.sort(result, new FindMatchComparator());
    }
    while (resultSize > matchUpperLimit) {
        resultSize--;
        result.remove(resultSize);
    }
    // System.out.println(bf.toString());
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) Timestamp(java.sql.Timestamp) Entry(java.util.Map.Entry) ResultSet(java.sql.ResultSet) Vector(java.util.Vector) Hashtable(java.util.Hashtable) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Aggregations

Entry (java.util.Map.Entry)1334 HashMap (java.util.HashMap)367 Map (java.util.Map)365 ArrayList (java.util.ArrayList)329 List (java.util.List)217 Iterator (java.util.Iterator)168 IOException (java.io.IOException)137 Test (org.junit.Test)89 LinkedHashMap (java.util.LinkedHashMap)84 Set (java.util.Set)79 HashSet (java.util.HashSet)74 File (java.io.File)67 Key (lucee.runtime.type.Collection.Key)63 Struct (lucee.runtime.type.Struct)60 Collection (java.util.Collection)54 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)47 TreeMap (java.util.TreeMap)43 LinkedList (java.util.LinkedList)41 Properties (java.util.Properties)35 StructImpl (lucee.runtime.type.StructImpl)34