<commit_before>package org.monarchinitiative.phenotefx;

import org.monarchinitiative.phenotefx.gui.infoviewer.InfoViewerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * For many articles we are curating, the authors put up a row with features for some system such as
 * facial dysmorphism. Each cell contains multiple entries, separated by comma. This tool allows the
 * user to copy such a row to the system clipboard. The individual columns are then separated by tab.
 * We then count up the entries and present a simple HTML table to the curator with counts for each
 * of the entries. This will not work with all supplemental tables, but it seems to be applicable to
 * a decent proportion of them.
 */
public class RowTallyTool {

    private final Map<String, Integer> items;

    public RowTallyTool(String row) {
        items = new HashMap<>();
        String []fields = row.split("\t");
        for (String f : fields) {
            String[] features = f.split("[,;.]");
            for (String feat : features) {
                if (feat.isEmpty() || feat.equals("a") || feat.equals("n")) {
                    continue; // reduce the noise, somewhat
                }
                items.putIfAbsent(feat, 0);
                items.merge(feat, 1, Integer::sum);
            }
        }
    }

    public void showTable() {
        StringBuilder sb = new StringBuilder();
        sb.append(getHTMLHead());
        sb.append(getTable());
        sb.append(getFooter());
        InfoViewerFactory.openDialog(sb.toString());
    }


    private String getHTMLHead() {
        return  "<html><body>\n" +
                inlineCSS() +
                "<h1>PhenoteFX: Tallying Phenotypes from Clipboard (Row)</h1>";
    }



    private String getTable() {
        StringBuilder sb = new StringBuilder();
        sb.append("<table>\n<caption>Items</caption>\n")
                .append("  <tr><th>Item</th><th>Count</th></tr>\n");
        for (Map.Entry<String, Integer> entry : items.entrySet()) {
            sb.append("<tr><td>").
                    append(entry.getKey()).
                    append( "</td><td>").
                    append(entry.getValue()).
                    append("</td></tr>\n");
        }
        sb.append("</table>\n<br/><br/>");
        return sb.toString();
    }

    private String getFooter() {
        return  "</body></html>";<commit_msg>adding tally tool for c&p rows<commit_after>package org.monarchinitiative.phenotefx;

/*-
 * #%L
 * PhenoteFX
 * %%
 * Copyright (C) 2017 - 2020 <NAME>
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import org.monarchinitiative.phenotefx.gui.infoviewer.InfoViewerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * For many articles we are curating, the authors put up a row with features for some system such as
 * facial dysmorphism. Each cell contains multiple entries, separated by comma. This tool allows the
 * user to copy such a row to the system clipboard. The individual columns are then separated by tab.
 * We then count up the entries and present a simple HTML table to the curator with counts for each
 * of the entries. This will not work with all supplemental tables, but it seems to be applicable to
 * a decent proportion of them.
 */
public class RowTallyTool {

    private final Map<String, Integer> items;

    private final int totalUsableColumns;

    public RowTallyTool(String row) {
        items = new HashMap<>();
        String []fields = row.split("\t");
        int N = 0;
        for (String f : fields) {
            if (f.isEmpty()){
                continue; // no observation
            } else if (f.equalsIgnoreCase("n/a")) {
                continue; // data not available
            } else {
                N++;
            }
            String[] features = f.split("[,;.]");
            for (String feat : features) {
                feat = feat.trim();
                if (feat.isEmpty() || feat.equals("a") || feat.equals("n")) {
                    continue; // reduce the noise, somewhat
                }
                items.putIfAbsent(feat, 0);
                items.merge(feat, 1, Integer::sum);
            }
        }
        totalUsableColumns = N;
    }

    public void showTable() {
        StringBuilder sb = new StringBuilder();
        sb.append(getHTMLHead());
        sb.append(getPara());
        sb.append(getTable());
        sb.append(getFooter());
        InfoViewerFactory.openDialog(sb.toString());
    }


    private String getHTMLHead() {
        return  "<html><body>\n" +
                inlineCSS() +
                "<h1>PhenoteFX: Tallying Phenotypes from Clipboard (Row)</h1>";
    }

    private String getPara() {
        StringBuilder sb = new StringBuilder();
        sb.append("<p>Entries tallied up from row of data about phenotypic abnormalities. We counted a ")
                .append("total of ").append(totalUsableColumns).append(" columns with observed phenotype data.<p>");
        return sb.toString();
    }



    private String getTable() {
        StringBuilder sb = new StringBuilder();
        sb.append("<table>\n<caption>Items</caption>\n")
                .append("  <tr><th>Item</th><th>Count</th></tr>\n");
        for (Map.Entry<String, Integer> entry : items.entrySet()) {
            sb.append("<tr><td>").
                    append(entry.getKey()).
                    append( "</td><td>").
                    append(entry.getValue()).
                    append("</td></tr>\n");
        }
        sb.append("</table>\n<br/><br/>");
        return sb.toString();
    }

    private String getFooter() {
        return  "</body></html>";
{'fraction_non_alphanumeric': 0.12479819179851469, 'fraction_numerical': 0.0033903777849531806, 'mean_word_length': 2.1801848049281314, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '677675', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1639, 'n_words': 536}