Search in sources :

Example 41 with Language

use of org.languagetool.Language in project languagetool by languagetool-org.

the class SynthDictionaryBuilderTest method testExportPosDictAndCreateSynth.

@Test
@Ignore("for interactive use only")
public void testExportPosDictAndCreateSynth() throws Exception {
    for (Language language : Languages.get()) {
        String langCode = language.getShortCode();
        File dir = new File("./languagetool-language-modules/" + langCode + "/src/main/resources/org/languagetool/resource/" + langCode);
        File oldBinarySynthFile = new File(dir, language.getName().toLowerCase() + "_synth.dict");
        if (!oldBinarySynthFile.exists()) {
            System.out.println("Ignoring " + language + ", no synth file found");
            continue;
        }
        File oldBinaryFile = new File(dir, language.getName().toLowerCase() + ".dict");
        File infoFile = new File(dir, language.getName().toLowerCase() + "_synth.info");
        File exportFile = exportDictionaryContents(oldBinaryFile);
        if (exportFile.length() == 0) {
            System.out.println("Zero-size output for " + language + ", skipping dictionary generation");
            exportFile.delete();
            continue;
        }
        SynthDictionaryBuilder builder = new SynthDictionaryBuilder(infoFile);
        File newBinarySynthFile = builder.build(exportFile, infoFile);
        exportFile.delete();
        System.out.println(language + " old binary file size: " + oldBinarySynthFile.length() + " bytes (" + oldBinarySynthFile.getName() + ")");
        System.out.println(language + " new binary file size: " + newBinarySynthFile.length() + " bytes (" + newBinarySynthFile.getAbsolutePath() + ")");
        // comment in to copy the new files over the old ones:
        /*boolean b = newBinarySynthFile.renameTo(oldBinarySynthFile);
      if (!b) {
        throw new RuntimeException("Could not rename " + newBinarySynthFile.getAbsolutePath() + " to " + oldBinarySynthFile.getCanonicalPath());
      }*/
        System.out.println("");
    }
}
Also used : Language(org.languagetool.Language) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 42 with Language

use of org.languagetool.Language in project languagetool by languagetool-org.

the class LanguageManagerDialog method getLanguages.

/**
   * Return all external Languages.
   */
List<Language> getLanguages() throws IllegalAccessException, InstantiationException {
    List<Language> languages = new ArrayList<>();
    for (File ruleFile : ruleFiles) {
        if (ruleFile != null) {
            Language newLanguage = LanguageBuilder.makeAdditionalLanguage(ruleFile);
            languages.add(newLanguage);
        }
    }
    return languages;
}
Also used : Language(org.languagetool.Language) ArrayList(java.util.ArrayList) File(java.io.File)

Example 43 with Language

use of org.languagetool.Language in project languagetool by languagetool-org.

the class LanguageToolSupport method init.

private void init() {
    try {
        config = new Configuration(new File(System.getProperty("user.home")), CONFIG_FILE, null);
    } catch (IOException ex) {
        throw new RuntimeException("Could not load configuration", ex);
    }
    Language defaultLanguage = config.getLanguage();
    if (defaultLanguage == null) {
        defaultLanguage = Languages.getLanguageForLocale(Locale.getDefault());
    }
    /**
     * Warm-up: we have a lot of lazy init in LT, which causes the first check to
     * be very slow (several seconds) for languages with a lot of data and a lot of
     * rules. We just assume that the default language is the language that the user
     * often uses and init the LT object for that now, not just when it's first used.
     * This makes the first check feel much faster:
     */
    reloadLanguageTool(defaultLanguage);
    checkExecutor = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setDaemon(true);
            t.setPriority(Thread.MIN_PRIORITY);
            t.setName(t.getName() + "-lt-background");
            return t;
        }
    });
    check = new AtomicInteger(0);
    this.textComponent.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void insertUpdate(DocumentEvent e) {
            mustDetectLanguage = config.getAutoDetect();
            recalculateSpans(e.getOffset(), e.getLength(), false);
            if (backgroundCheckEnabled) {
                checkDelayed(null);
            }
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            mustDetectLanguage = config.getAutoDetect();
            recalculateSpans(e.getOffset(), e.getLength(), true);
            if (backgroundCheckEnabled) {
                checkDelayed(null);
            }
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            mustDetectLanguage = config.getAutoDetect();
            if (backgroundCheckEnabled) {
                checkDelayed(null);
            }
        }
    });
    mouseListener = new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent me) {
        }

        @Override
        public void mousePressed(MouseEvent me) {
            if (me.isPopupTrigger()) {
                showPopup(me);
            }
        }

        @Override
        public void mouseReleased(MouseEvent me) {
            if (me.isPopupTrigger()) {
                showPopup(me);
            }
        }

        @Override
        public void mouseEntered(MouseEvent me) {
        }

        @Override
        public void mouseExited(MouseEvent me) {
        }
    };
    this.textComponent.addMouseListener(mouseListener);
    actionListener = e -> _actionPerformed(e);
    mustDetectLanguage = config.getAutoDetect();
    if (!this.textComponent.getText().isEmpty() && backgroundCheckEnabled) {
        checkImmediately(null);
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) MouseEvent(java.awt.event.MouseEvent) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) IOException(java.io.IOException) MouseListener(java.awt.event.MouseListener) Language(org.languagetool.Language) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File)

Example 44 with Language

use of org.languagetool.Language in project languagetool by languagetool-org.

the class LanguageComboBoxModel method create.

static LanguageComboBoxModel create(ResourceBundle messages, String extLangSuffix, boolean includeHidden, List<Language> external, LanguageAdapter first) {
    LanguageComparator comparator = new LanguageComparator(messages, extLangSuffix);
    LanguageComboBoxModel model = new LanguageComboBoxModel();
    if (first != null) {
        //e.g. an option like "System Default"
        model.addElement(first);
    }
    if (external != null) {
        // do not sort the original list
        ArrayList<Language> ext = new ArrayList<>(external);
        Collections.sort(ext, comparator);
        for (Language l : ext) {
            model.addElement(new LanguageAdapter(l));
        }
    }
    // the original list is unmodifiable
    ArrayList<Language> internal = new ArrayList<>(Languages.get());
    Collections.sort(internal, comparator);
    for (Language l : internal) {
        if (includeHidden || !l.isHiddenFromGui()) {
            model.addElement(new LanguageAdapter(l));
        }
    }
    return model;
}
Also used : Language(org.languagetool.Language) ArrayList(java.util.ArrayList)

Example 45 with Language

use of org.languagetool.Language in project languagetool by languagetool-org.

the class LanguageComboBoxRenderer method getListCellRendererComponent.

@Override
public Component getListCellRendererComponent(JList list, LanguageAdapter adapter, int index, boolean isSelected, boolean cellHasFocus) {
    setComponentOrientation(list.getComponentOrientation());
    if (isSelected) {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
    } else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
    }
    if (adapter != null) {
        Language lang = adapter.getLanguage();
        if (lang != null) {
            setText(getTranslatedName(lang));
            String langTag = lang.getLocaleWithCountryAndVariant().toLanguageTag();
            String country = lang.getLocaleWithCountryAndVariant().getCountry().toLowerCase();
            ResourceDataBroker dataBroker = JLanguageTool.getDataBroker();
            String filename = "flags/bytag/" + langTag + ".png";
            if (!dataBroker.resourceExists(filename)) {
                filename = "flags/" + country + ".png";
            }
            if (!dataBroker.resourceExists(filename)) {
                filename = "flags/empty.png";
            }
            ImageIcon icon = new ImageIcon(dataBroker.getFromResourceDirAsUrl(filename));
            setIcon(icon);
        } else {
            setText(adapter.getValue());
            setIcon(null);
        }
    }
    setEnabled(list.isEnabled());
    setFont(list.getFont());
    setBorder(BORDER);
    return this;
}
Also used : ImageIcon(javax.swing.ImageIcon) Language(org.languagetool.Language) ResourceDataBroker(org.languagetool.databroker.ResourceDataBroker)

Aggregations

Language (org.languagetool.Language)84 Test (org.junit.Test)23 File (java.io.File)15 ArrayList (java.util.ArrayList)12 JLanguageTool (org.languagetool.JLanguageTool)11 Rule (org.languagetool.rules.Rule)11 RuleMatch (org.languagetool.rules.RuleMatch)10 IOException (java.io.IOException)7 Ignore (org.junit.Ignore)6 StringTools.readerToString (org.languagetool.tools.StringTools.readerToString)5 InputStream (java.io.InputStream)4 English (org.languagetool.language.English)4 BitextRule (org.languagetool.rules.bitext.BitextRule)4 URL (java.net.URL)3 HashSet (java.util.HashSet)3 MultiThreadedJLanguageTool (org.languagetool.MultiThreadedJLanguageTool)3 AmericanEnglish (org.languagetool.language.AmericanEnglish)3 LanguageModel (org.languagetool.languagemodel.LanguageModel)3 LuceneLanguageModel (org.languagetool.languagemodel.LuceneLanguageModel)3 BufferedReader (java.io.BufferedReader)2