<commit_before>/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 <NAME> (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package de.danielnaber.languagetool.gui;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import de.danielnaber.languagetool.JLanguageTool;
import de.danielnaber.languagetool.Language;
import de.danielnaber.languagetool.rules.RuleMatch;

/**
 * A simple GUI to check texts with.
 * 
 * @author <NAME>
 */
class Main implements ActionListener {

  private static final String OPTIONS_BUTTON = "Options...";
  
  private JTextArea textArea = null;
  private JTextPane resultArea = null;
  private JComboBox langBox = null;
  
  private Map configDialogs = new HashMap();       // Language -> ConfigurationDialog

  private Main() {
  }

  private void createAndShowGUI() {
    JFrame frame = new JFrame("LanguageTool " +JLanguageTool.VERSION+ " Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    textArea = new JTextArea("This is a example input to to show you how JLanguageTool works. " +
        "Note, however, that it does not include a spell checka.");
    // TODO: wrong line number is displayed for lines that are wrapped automatically:
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    resultArea = new JTextPane();
    resultArea.setContentType("text/html");
    resultArea.setText("Results will appear here");
    JLabel label = new JLabel("Please type or paste text to check in the top area");
    JButton button = new JButton("Check text");
    button.setMnemonic('c'); 
    button.addActionListener(this);

    JButton configButton = new JButton(OPTIONS_BUTTON);
    configButton.setMnemonic('o'); 
    configButton.addActionListener(this);

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints buttonCons = new GridBagConstraints();
    buttonCons.gridx = 0;
    buttonCons.gridy = 0;
    panel.add(button, buttonCons);
    buttonCons.gridx = 1;
    buttonCons.gridy = 0;
    panel.add(new JLabel(" in: "), buttonCons);
    buttonCons.gridx = 2;
    buttonCons.gridy = 0;
    langBox = new JComboBox(Language.LANGUAGES);
    panel.add(langBox, buttonCons);
    buttonCons.gridx = 3;
    buttonCons.gridy = 0;
    buttonCons.insets = new Insets(0, 10, 0, 0);
    panel.add(configButton, buttonCons);

    Container contentPane = frame.getContentPane();
    GridBagLayout gridLayout = new GridBagLayout();
    contentPane.setLayout(gridLayout);
    GridBagConstraints cons = new GridBagConstraints();
    cons.fill = GridBagConstraints.BOTH;
    cons.weightx = 10.0f;
    cons.weighty = 10.0f;
    cons.gridx = 0;
    cons.gridy = 0;
    contentPane.add(new JScrollPane(textArea), cons);
    cons.gridy = 1;
    cons.weighty = 5.0f;
    contentPane.add(new JScrollPane(resultArea), cons);

    cons.fill = GridBagConstraints.NONE;
    cons.gridx = 0;
    cons.gridy = 2;
    cons.weighty = 0.0f;
    cons.insets = new Insets(3,3,3,3);
    //cons.fill = GridBagConstraints.NONE;
    contentPane.add(label, cons);
    cons.gridy = 3;
    contentPane.add(panel, cons);
    
    frame.pack();
    frame.setSize(600, 600);
    frame.setVisible(true);
  }
  
  public void actionPerformed(ActionEvent e) {
    String langName = langBox.getSelectedItem().toString();
    Language language = Language.getLanguageforName(langName);
    ConfigurationDialog configDialog = null;
    if (configDialogs.containsKey(language)) {
      configDialog = (ConfigurationDialog)configDialogs.get(language);
    } else {
      configDialog = new ConfigurationDialog(false);
      configDialogs.put(language, configDialog);
    }
    JLanguageTool langTool;
    try {
      langTool = new JLanguageTool(language);
      langTool.activateDefaultPatternRules();
      Set disabledRules = configDialog.getDisabledRuleIds();
      if (disabledRules != null) {
        for (Iterator iter = disabledRules.iterator(); iter.hasNext();) {
          String ruleId = (String) iter.next();<commit_msg>option to set mother tongue<commit_after>/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 <NAME> (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package de.danielnaber.languagetool.gui;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import de.danielnaber.languagetool.JLanguageTool;
import de.danielnaber.languagetool.Language;
import de.danielnaber.languagetool.rules.RuleMatch;

/**
 * A simple GUI to check texts with.
 * 
 * @author <NAME>
 */
class Main implements ActionListener {

  private static final String OPTIONS_BUTTON = "Options...";
  private static final String NO_MOTHER_TONGUE = "---";
  
  private JTextArea textArea = null;
  private JTextPane resultArea = null;
  private JComboBox langBox = null;
  private JComboBox motherTongueBox = null;
  
  private Map configDialogs = new HashMap();       // Language -> ConfigurationDialog

  private Main() {
  }

  private void createAndShowGUI() {
    JFrame frame = new JFrame("LanguageTool " +JLanguageTool.VERSION+ " Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    textArea = new JTextArea("This is a example input to to show you how JLanguageTool works. " +
        "Note, however, that it does not include a spell checka.");
    // TODO: wrong line number is displayed for lines that are wrapped automatically:
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    resultArea = new JTextPane();
    resultArea.setContentType("text/html");
    resultArea.setText("Results will appear here");
    JLabel label = new JLabel("Please type or paste text to check in the top area");
    JButton button = new JButton("Check text");
    button.setMnemonic('c'); 
    button.addActionListener(this);

    JButton configButton = new JButton(OPTIONS_BUTTON);
    configButton.setMnemonic('o'); 
    configButton.addActionListener(this);

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints buttonCons = new GridBagConstraints();
    buttonCons.gridx = 0;
    buttonCons.gridy = 0;
    panel.add(button, buttonCons);
    buttonCons.gridx = 1;
    buttonCons.gridy = 0;
    panel.add(new JLabel(" in: "), buttonCons);
    buttonCons.gridx = 2;
    buttonCons.gridy = 0;
    langBox = new JComboBox(Language.LANGUAGES);
    panel.add(langBox, buttonCons);
    buttonCons.gridx = 3;
    buttonCons.gridy = 0;
    panel.add(new JLabel(" Mother tongue: "), buttonCons);
    buttonCons.gridx = 4;
    buttonCons.gridy = 0;
    motherTongueBox = new JComboBox(getPossibleMotherTongues());
    panel.add(motherTongueBox, buttonCons);
    buttonCons.gridx = 5;
    buttonCons.gridy = 0;
    buttonCons.insets = new Insets(0, 10, 0, 0);
    panel.add(configButton, buttonCons);

    Container contentPane = frame.getContentPane();
    GridBagLayout gridLayout = new GridBagLayout();
    contentPane.setLayout(gridLayout);
    GridBagConstraints cons = new GridBagConstraints();
    cons.fill = GridBagConstraints.BOTH;
    cons.weightx = 10.0f;
    cons.weighty = 10.0f;
    cons.gridx = 0;
    cons.gridy = 0;
    contentPane.add(new JScrollPane(textArea), cons);
    cons.gridy = 1;
    cons.weighty = 5.0f;
    contentPane.add(new JScrollPane(resultArea), cons);

    cons.fill = GridBagConstraints.NONE;
    cons.gridx = 0;
    cons.gridy = 2;
    cons.weighty = 0.0f;
    cons.insets = new Insets(3,3,3,3);
    //cons.fill = GridBagConstraints.NONE;
    contentPane.add(label, cons);
    cons.gridy = 3;
    contentPane.add(panel, cons);
    
    frame.pack();
    frame.setSize(600, 600);
    frame.setVisible(true);
  }
  
  private Object[] getPossibleMotherTongues() {
    List motherTongues = new ArrayList();
    motherTongues.add(NO_MOTHER_TONGUE);
    for (int i = 0; i < Language.LANGUAGES.length; i++) {
      motherTongues.add(Language.LANGUAGES[i]);
    }
    return motherTongues.toArray();
  }

  public void actionPerformed(ActionEvent e) {
    String langName = langBox.getSelectedItem().toString();
    Language language = Language.getLanguageforName(langName);
    String motherTongueName = motherTongueBox.getSelectedItem().toString();
    Language motherTongue = Language.getLanguageforName(motherTongueName);
    ConfigurationDialog configDialog = null;
    if (configDialogs.containsKey(language)) {
      configDialog = (ConfigurationDialog)configDialogs.get(language);
    } else {
      configDialog = new ConfigurationDialog(false);
      configDialogs.put(language, configDialog);
    }
    JLanguageTool langTool;
    try {
      langTool = new JLanguageTool(language, motherTongue);
      langTool.activateDefaultPatternRules();
      langTool.activateDefaultFalseFriendRules();
      Set disabledRules = configDialog.getDisabledRuleIds();
      if (disabledRules != null) {
        for (Iterator iter = disabledRules.iterator(); iter.hasNext();) {
          String ruleId = (String) iter.next();
{'fraction_non_alphanumeric': 0.10286895838718015, 'fraction_numerical': 0.01008012406306539, 'mean_word_length': 3.8447412353923207, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '4556717', 'n_tokens_mistral': 3592, 'n_tokens_neox': 3463, 'n_words': 954}