Search in sources :

Example 1 with KeyValue

use of com.kyj.fx.voeditor.visual.framework.KeyValue in project Gargoyle by callakrsos.

the class ValueUtil method toTF_IDF.

public static List<KeyValue> toTF_IDF(String[] contents, boolean autoFilter) {
    TF_IDF tf_IDF = new TF_IDF(contents);
    String[] words = tf_IDF.getWordVector();
    // 배열구조 docIndx - worIdx
    double[][] tf_IDFMatrix = tf_IDF.getTF_IDFMatrix();
    List<KeyValue> arrayList = new ArrayList<>();
    int docCount = tf_IDFMatrix.length;
    int wordCount = tf_IDFMatrix[0].length;
    // 평균을 위한 합.
    double[] average = new double[wordCount];
    for (int docIndex = 0; docIndex < tf_IDFMatrix.length; docIndex++) {
        double[] wordIndexTable = tf_IDFMatrix[docIndex];
        for (int wordIndex = 0; wordIndex < wordIndexTable.length; wordIndex++) {
            average[wordIndex] = wordIndexTable[wordIndex] + average[wordIndex];
        }
    }
    // 평균값 도출.
    for (int i = 0; i < average.length; i++) {
        String keyword = words[i];
        average[i] = average[i] / docCount;
        arrayList.add(new KeyValue(keyword, average[i]));
    }
    // 정렬. - 내림차순.
    Collections.sort(arrayList, new Comparator<KeyValue>() {

        @Override
        public int compare(KeyValue o1, KeyValue o2) {
            Double d1 = (Double) o1.getValue();
            Double d2 = (Double) o2.getValue();
            return ~Double.compare(d1, d2);
        }
    });
    return arrayList;
}
Also used : KeyValue(com.kyj.fx.voeditor.visual.framework.KeyValue) ArrayList(java.util.ArrayList)

Example 2 with KeyValue

use of com.kyj.fx.voeditor.visual.framework.KeyValue in project Gargoyle by callakrsos.

the class AbstractDatabaseMetaDataController method listMetadata.

/**
	 * @작성자 : KYJ
	 * @작성일 : 2016. 11. 28.
	 * @return
	 * @throws Exception
	 */
private List<KeyValue> listMetadata() throws Exception {
    List<KeyValue> items = new ArrayList<>();
    try (Connection connection = this.parent.getConnection()) {
        DatabaseMetaData metaData = connection.getMetaData();
        Method[] declaredMethods = DatabaseMetaData.class.getDeclaredMethods();
        for (Method m : declaredMethods) {
            Class<?> returnType = m.getReturnType();
            if ((m.getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC) {
                if (!m.isAccessible())
                    m.setAccessible(true);
                if (m.isAccessible()) {
                    if (m.getParameterCount() == 0) {
                        KeyValue keyValue = new KeyValue();
                        String methodName = m.getName();
                        Object value = null;
                        if (returnType == java.lang.String.class) {
                            value = m.invoke(metaData);
                        } else if (returnType.isAssignableFrom(java.lang.Number.class)) {
                            value = m.invoke(metaData);
                        } else if (returnType == java.lang.Boolean.class) {
                            value = m.invoke(metaData);
                        } else if (returnType == java.lang.Character.class) {
                            value = m.invoke(metaData);
                        } else if (returnType == java.util.Properties.class) {
                            value = m.invoke(metaData);
                            if (value != null)
                                value = value.toString();
                        } else if (returnType.isAssignableFrom(java.util.Collection.class)) {
                            value = m.invoke(metaData);
                            if (value != null)
                                value = value.toString();
                        } else if (returnType.isPrimitive()) {
                            try {
                                value = m.invoke(metaData);
                            } catch (InvocationTargetException e) {
                                //NotSupprted
                                value = "Not Supported.";
                            }
                        }
                        keyValue.setKey(methodName);
                        keyValue.setValue(value);
                        items.add(keyValue);
                    }
                }
            }
        }
    }
    return items;
}
Also used : KeyValue(com.kyj.fx.voeditor.visual.framework.KeyValue) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) Method(java.lang.reflect.Method) DatabaseMetaData(java.sql.DatabaseMetaData) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with KeyValue

use of com.kyj.fx.voeditor.visual.framework.KeyValue in project Gargoyle by callakrsos.

the class SimpleSQLResultView method executeSQL.

public void executeSQL(Node root) {
    LOGGER.debug("sql check....");
    if (this.sql == null || this.sql.isEmpty()) {
        return;
    }
    LOGGER.debug("param bind....");
    /* [시작] 바인드변수 맵핑시키는 테이블 */
    param.keySet().stream().forEach(key -> {
        KeyValue keyValue = new KeyValue();
        keyValue.setKey(key);
        keyValue.setValue(param.get(key));
        tbBind.getItems().add(keyValue);
    });
    /* [끝] 바인드변수 맵핑시키는 테이블 */
    String wrapperedSQL = ConfigResourceLoader.getInstance().get(ConfigResourceLoader.SQL_LIMIT_WRAPPER);
    LOGGER.debug(String.format("wrapperedSql : %s", wrapperedSQL));
    param.put(ConfigResourceLoader.USER_SQL, this.sql);
    String velocityToText = ValueUtil.getVelocityToText(wrapperedSQL, param);
    param.remove(ConfigResourceLoader.USER_SQL);
    LOGGER.debug(String.format("before velocityText %s", velocityToText));
    String sql = ValueUtil.getVelocityToText(velocityToText, param);
    LOGGER.debug(String.format("after velocityText %s", sql));
    columns = new ArrayList<>();
    try {
        // Iterator<Entry<String, Object>> iterator =
        // param.entrySet().iterator();
        MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource(param);
        // while (iterator.hasNext()) {
        // Entry<String, Object> next = iterator.next();
        // Object value = null;
        // if (next.getValue() != null)
        // value = "'".concat(next.getValue().toString()).concat("'");
        // mapSqlParameterSource.addValue(next.getKey(), value);
        // }
        List<Map<String, Object>> select = DbUtil.select(sql, mapSqlParameterSource, (rs, row) -> {
            Map<String, Object> hashMap = new HashMap<String, Object>();
            final ResultSetMetaData metaData = rs.getMetaData();
            final int columnCount = metaData.getColumnCount();
            hashMap = new HashMap<String, Object>();
            for (int c = 1; c <= columnCount; c++) {
                String columnLabel = metaData.getColumnLabel(c);
                if (row == 0) {
                    TableModelDVO tableModelDVO = new TableModelDVO();
                    tableModelDVO.setDatabaseColumnName(columnLabel);
                    String columnTypeName = metaData.getColumnTypeName(c);
                    if ("unknown".equals(columnTypeName)) {
                        LOGGER.debug("unknown type detected....");
                        LOGGER.debug("convert varchar type...");
                        LOGGER.debug("type Number : " + metaData.getColumnType(c));
                        // TODO 잠재적인 버그가 있을 가능성이 있을지 ???? 확신이 안섬.
                        columnTypeName = "varchar";
                    }
                    tableModelDVO.setDabaseTypeName(columnTypeName);
                    columns.add(tableModelDVO);
                }
                hashMap.put(columnLabel, rs.getString(c));
            }
            return hashMap;
        });
        if (select != null && !select.isEmpty()) {
            clear();
            createColumns(columns);
            tbResult.getItems().addAll(select);
        }
    } catch (Exception e) {
        LOGGER.error(ValueUtil.toString(e));
        // DialogUtil.showConfirmDialog();
        // 에러 다이얼로그 수정.
        DialogUtil.showExceptionDailog(SharedMemory.getPrimaryStage(), e);
    }
    Iterator<String> iterator = param.keySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        Object value = param.get(key);
        if (value == null || value.toString().isEmpty())
            param.put(key, null);
        else if (value instanceof List) {
            List<Object> items = (List<Object>) value;
            // StringBuffer sb = new StringBuffer();
            // for (Object obj : items) {
            // sb.append(obj).append(",");
            // }
            // if (items != null && !items.isEmpty()) //bug fix. sb가 빈 경우
            // 에러발생.
            // sb.setLength(sb.length() - 1);
            param.put(key, items);
        } else
            param.put(key, value);
    }
    this.mappingedSqlKeywords.setContent(ValueUtil.getVelocityToText(this.sql, param, true));
    this.sqlKeywords.setContent(this.sql);
}
Also used : KeyValue(com.kyj.fx.voeditor.visual.framework.KeyValue) HashMap(java.util.HashMap) TableModelDVO(kyj.Fx.dao.wizard.core.model.vo.TableModelDVO) IOException(java.io.IOException) ResultSetMetaData(java.sql.ResultSetMetaData) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ArrayList(java.util.ArrayList) List(java.util.List) ObservableList(javafx.collections.ObservableList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with KeyValue

use of com.kyj.fx.voeditor.visual.framework.KeyValue in project Gargoyle by callakrsos.

the class TF_IDF method getString.

public void getString(Collection<String> links) {
    URLModel[] array = links.parallelStream().map(link -> {
        URLModel model = URLModel.empty();
        try {
            ResponseHandler<URLModel> responseHandler = new ResponseHandler<URLModel>() {

                @Override
                public URLModel apply(InputStream is, Integer code) {
                    if (code == 200) {
                        return new URLModel(link, ValueUtil.toString(is));
                    }
                    return URLModel.empty();
                }
            };
            if (link.startsWith("https")) {
                model = RequestUtil.requestSSL(new URL(link), responseHandler);
            } else {
                model = RequestUtil.request(new URL(link), responseHandler);
            }
        } catch (Exception e) {
            return URLModel.empty();
        }
        return model;
    }).filter(v -> !v.isEmpty()).map(v -> {
        String content = v.getContent();
        ExtractorBase instance = ArticleExtractor.getInstance();
        InputSource source = new InputSource(new StringReader(content));
        source.setEncoding("UTF-8");
        try {
            content = ValueUtil.HTML.getNewsContent(instance, source);
            v.setContent(content);
        } catch (Exception e) {
            v = URLModel.empty();
            e.printStackTrace();
        }
        return v;
    }).filter(v -> !v.isEmpty()).toArray(URLModel[]::new);
    List<KeyValue> tf_IDF = ValueUtil.toTF_IDF(array);
    tf_IDF.forEach(v -> {
        System.out.println(v.toString());
    });
}
Also used : URL(java.net.URL) RequestUtil(com.kyj.fx.voeditor.visual.util.RequestUtil) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BoilerpipeSAXInput(com.kohlschutter.boilerpipe.sax.BoilerpipeSAXInput) KeyValue(com.kyj.fx.voeditor.visual.framework.KeyValue) ExtractorBase(com.kohlschutter.boilerpipe.extractors.ExtractorBase) URLModel(com.kyj.fx.voeditor.visual.framework.URLModel) Before(org.junit.Before) InputSource(org.xml.sax.InputSource) ProxyInitializable(com.kyj.fx.voeditor.visual.main.initalize.ProxyInitializable) Logger(org.slf4j.Logger) ResponseHandler(com.kyj.fx.voeditor.visual.util.ResponseHandler) MalformedURLException(java.net.MalformedURLException) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) ValueUtil(com.kyj.fx.voeditor.visual.util.ValueUtil) ArticleSentencesExtractor(com.kohlschutter.boilerpipe.extractors.ArticleSentencesExtractor) Collectors(java.util.stream.Collectors) List(java.util.List) KeepEverythingExtractor(com.kohlschutter.boilerpipe.extractors.KeepEverythingExtractor) StringReader(java.io.StringReader) Document(org.jsoup.nodes.Document) Jsoup(org.jsoup.Jsoup) Elements(org.jsoup.select.Elements) Collections(java.util.Collections) TextDocument(com.kohlschutter.boilerpipe.document.TextDocument) InputStream(java.io.InputStream) ArticleExtractor(com.kohlschutter.boilerpipe.extractors.ArticleExtractor) InputSource(org.xml.sax.InputSource) KeyValue(com.kyj.fx.voeditor.visual.framework.KeyValue) ResponseHandler(com.kyj.fx.voeditor.visual.util.ResponseHandler) ExtractorBase(com.kohlschutter.boilerpipe.extractors.ExtractorBase) InputStream(java.io.InputStream) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) StringReader(java.io.StringReader) URLModel(com.kyj.fx.voeditor.visual.framework.URLModel)

Aggregations

KeyValue (com.kyj.fx.voeditor.visual.framework.KeyValue)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 TextDocument (com.kohlschutter.boilerpipe.document.TextDocument)1 ArticleExtractor (com.kohlschutter.boilerpipe.extractors.ArticleExtractor)1 ArticleSentencesExtractor (com.kohlschutter.boilerpipe.extractors.ArticleSentencesExtractor)1 ExtractorBase (com.kohlschutter.boilerpipe.extractors.ExtractorBase)1 KeepEverythingExtractor (com.kohlschutter.boilerpipe.extractors.KeepEverythingExtractor)1 BoilerpipeSAXInput (com.kohlschutter.boilerpipe.sax.BoilerpipeSAXInput)1 URLModel (com.kyj.fx.voeditor.visual.framework.URLModel)1 ProxyInitializable (com.kyj.fx.voeditor.visual.main.initalize.ProxyInitializable)1 RequestUtil (com.kyj.fx.voeditor.visual.util.RequestUtil)1 ResponseHandler (com.kyj.fx.voeditor.visual.util.ResponseHandler)1 ValueUtil (com.kyj.fx.voeditor.visual.util.ValueUtil)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1