【问题标题】:How to allow more than two words to be checked in this Java Code如何允许在此 Java 代码中检查两个以上的单词
【发布时间】:2024-01-13 16:12:01
【问题描述】:

我需要修改这个脚本,以便可以检查两个以上的单词,而且我对 Java 的了解太有限,无法自己进行更改。 此脚本是 OpenOffice (LanguageTool) 的开源语法检查器的一部分,脚本的目的是用其他词替换某些词。

要检查的单词文件名为“coherency.txt”,格式如下: 错误字 1=正确字 1 WrongWord2=CorrectWord2

当我输入:WrongWord1 时,脚本会标记它并告诉我应该改用 CorrectWord1。

但我需要能够使用三个或更多单词,例如: 错误字 1=错误字 2=正确字 1 错误字 3=错误字 4=错误字 5=正确字 2 WrongWord6=CorrectWord3

所以当我输入 WrongWord3 时,它会被标记,并且脚本告诉我应该使用 CorrectWord2 或者 当我输入 WrongWord2 时,它也会被标记,脚本告诉我应该使用 CorrectWord1

如果您可以提供帮助,我可以在 http://www.sbbic.org/lang/en-us/volunteer/ 处提供指向您网页的链接

您可以就如何修改此代码以允许对两个以上的单词进行比较和替换提供任何帮助,我们将不胜感激! 谢谢, 内森

    /* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 Daniel Naber (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.rules;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import de.danielnaber.languagetool.AnalyzedSentence;
import de.danielnaber.languagetool.AnalyzedTokenReadings;
import de.danielnaber.languagetool.JLanguageTool;
import de.danielnaber.languagetool.tools.StringTools;

/**
 * A Khmer rule that matches words or phrases which should not be used and suggests
 * correct ones instead. Loads the relevant words  from 
 * <code>rules/km/coherency.txt</code>, where km is a code of the language.
 * 
 * @author Andriy Rysin
 */
public abstract class KhmerWordCoherencyRule extends KhmerRule {

  private Map<String, String> wrongWords; // e.g. "вреѿті реѿт" -> "зреѿтою"

  private static final String FILE_NAME = "/km/coherency.txt";

  public abstract String getFileName();

  private static final String FILE_ENCODING = "utf-8";

  public String getEncoding() {
    return FILE_ENCODING;
  }

  /**
   * Indicates if the rule is case-sensitive. Default value is <code>true</code>.
   * @return true if the rule is case-sensitive, false otherwise.
   */
  public boolean isCaseSensitive() {
    return false;  
  }

  /**
   * @return the locale used for case conversion when {@link #isCaseSensitive()} is set to <code>false</code>.
   */
  public Locale getLocale() {
    return Locale.getDefault();
  }  

  public KhmerWordCoherencyRule(final ResourceBundle messages) throws IOException {
    if (messages != null) {
      super.setCategory(new Category(messages.getString("category_misc")));
    }
    wrongWords = loadWords(JLanguageTool.getDataBroker().getFromRulesDirAsStream(getFileName()));
  }

  public String getId() {
    return "KM_WORD_COHERENCY";
  }

  public String getDescription() {
    return "Checks for wrong words/phrases";
  }

  public String getSuggestion() {
    return " is not valid, use ";
  }

  public String getShort() {
    return "Wrong word";
  }

  public final RuleMatch[] match(final AnalyzedSentence text) {
    final List<RuleMatch> ruleMatches = new ArrayList<RuleMatch>();
    final AnalyzedTokenReadings[] tokens = text.getTokensWithoutWhitespace();

    for (int i = 1; i < tokens.length; i++) {
      final String token = tokens[i].getToken();

      final String origToken = token;
      final String replacement = isCaseSensitive()?wrongWords.get(token):wrongWords.get(token.toLowerCase(getLocale()));
      if (replacement != null) {
        final String msg = token + getSuggestion() + replacement;
        final int pos = tokens[i].getStartPos();
        final RuleMatch potentialRuleMatch = new RuleMatch(this, pos, pos
            + origToken.length(), msg, getShort());
        if (!isCaseSensitive() && StringTools.startsWithUppercase(token)) {
          potentialRuleMatch.setSuggestedReplacement(StringTools.uppercaseFirstChar(replacement));
        } else {
          potentialRuleMatch.setSuggestedReplacement(replacement);
        }
        ruleMatches.add(potentialRuleMatch);
      }
    }
    return toRuleMatchArray(ruleMatches);
  }


  private Map<String, String> loadWords(final InputStream file) throws IOException {
    final Map<String, String> map = new HashMap<String, String>();
    InputStreamReader isr = null;
    BufferedReader br = null;
    try {
      isr = new InputStreamReader(file, getEncoding());
      br = new BufferedReader(isr);
      String line;

      while ((line = br.readLine()) != null) {
        line = line.trim();
        if (line.length() < 1) {
          continue;
        }
        if (line.charAt(0) == '#') { // ignore comments
          continue;
        }
        final String[] parts = line.split("=");
        if (parts.length != 2) {
          throw new IOException("Format error in file "
              + JLanguageTool.getDataBroker().getFromRulesDirAsUrl(getFileName()) + ", line: " + line);
        }
        map.put(parts[0], parts[1]);
      }

    } finally {
      if (br != null) {
        br.close();
      }
      if (isr != null) {
        isr.close();
      }
    }
    return map;
  }

  public void reset() {
  }  

}

【问题讨论】:

  • 你发布的代码太多了。发布最小的一段代码来证明你的问题。我什至不确定你的问题是什么。
  • 如果有人按照您的要求进行操作,您是否具有编译调整后的源代码的专业知识,并且如果需要,将所有已编译的源代码再次打包到一个 JAR 文件中?因为它不太可能通过替换调整后的源来简单地“工作”。
  • 你确定它像 word1=wrongword2 而不是 wrongword2=word1
  • 谢谢大家,是的,你是对的,我的顺序搞砸了 - 它是 Wrongword1=CorrectWord1。 @Bart 是的,我可以编译源代码,我花了一些时间,就像我说我不懂 Java,但我可以编译源代码并让它在 OpenOffice 中工作。 @Falmarri 抱歉,我发布了整个内容,因为这是问题所在,我也不知道需要更改什么 - 抱歉!

标签: java map hashmap languagetool


【解决方案1】:

对于小的改编:

考虑将所需的输入格式更改为

WrongWord = CorrectWord[, CorrectWord]*

键是不正确的单词,值是逗号分隔的正确选项列表。所以你可以保持文件解析不变。

Map 应该是Map&lt;String, Set&lt;String&gt;&gt; 类型 - 每个标记都映射到一组替代项。

现在您可以将= 周围的每一行拆分为一个键/值对,将, 周围的每个值拆分为一组建议的标记来替换输入。


那么您需要在match 中进行一些更改来组合新消息,因为您现在期待不止一个建议。

final String origToken = token;之后的行改为

final String[] replacements = wrongWords.get(token);
  if (replacements != null) {
    final String msg = createMessage(token, replacements);
    final int pos = tokens[i].getStartPos();

并实现createMessage 方法以返回人类可读的消息,告诉用户令牌的一对多替代方案。

【讨论】:

  • 你是对的 Andreas - 我改变了问题以符合现实。谢谢!
  • 我是否理解正确,我应该在 coherency.txt 文件中输入 Word = WrongWord[, WrongWord]* ?让我做一个真实的单词示例以确保我理解: Hello = Hi[Hullo, Hiya]* 还是应该是 Hello = Hi[Hullo, Hi]* (“Hi 重复两次)?感谢您愿意提供帮助我。
  • @Nathan - 我选择了一个非常常见的语法,匹配这个“模式”的真实行可以是Wrong1 = Correct1 以及Wrong2 = Correct2a, Correct2b
  • 另外,当我尝试编译代码时,我收到代码错误:Map>
  • @Nathan - 不幸的是,我真的怀疑您是否能够修改该 Java 代码 - 您肯定需要 Java 编程技能。签名更改不正确。它必须Map&lt;String, Set&lt;String&gt;&gt; - 键 (String) 是候选令牌,值 (Set&lt;String&gt;) 是包含建议替代方案的 Set。请勿更改顺序,否则地图将无法按预期工作。
【解决方案2】:

你需要改变的是 loadWords 中的这一部分:

final String[] parts = line.split("=");
if (parts.length != 2) {
    throw new IOException("Format error in file " + JLanguageTool.getDataBroker().getFromRulesDirAsUrl(getFileName()) + ", line: " + line);
}
map.put(parts[0], parts[1]);

这会将等号的左侧作为键,将右侧作为值放入映射中。因此,我认为左侧必须是错误的词。因此你的输入应该变成wrong1 = wrong2 = ... = correct

使用此设置,您只需将其更改为以下内容

final String[] parts = line.split("=");
if (parts.length < 2) {
    throw new IOException("Format error in file " + JLanguageTool.getDataBroker().getFromRulesDirAsUrl(getFileName()) + ", line: " + line);
}
for (int i = 0; i < parts.length - 1; i++) {
    map.put(parts[i], parts[parts.length - 1]);
}

这将在地图中生成以下条目:

wrong1 = correct
wrong2 = correct
wrong3 = correct
...

这可能不是最有效的解决方案,但它应该以某种方式工作。使用此地图可以搜索错误的单词,并且建议将是正确的。

(PS:我无法运行代码,因此其中可能存在一些编码错误)

【讨论】:

  • 谢谢各位,我尝试了这个解决方案,虽然它没有导致任何错误(部分长度超过两个),但代码没有标记不正确的单词......原因代码无法运行是因为它正在调用 LanguageTool 源中包含的其他脚本,但我不知道如何进一步解决这个问题……有什么可以帮助你找到的吗有效的解决方案?感谢您花时间帮我看看这个。
  • 当然,必须至少有两个部分...在您的版本中,它检查为正好有两个,而在我的版本中,它必须至少有两个。我不清楚“标记不正确的单词”是什么意思。如果您将上面提到的行与我写的行交换,并将其与原始文件一起提供,它应该与以前完全相同。最后它只返回一个规则匹配数组
  • 谢谢各位,我所说的“标记不正确的单词”是指在编译代码然后在 OpenOffice 中使用此扩展(Java 代码是扩展的一部分)时,扩展不再标记单词用于替换(当我写出 wrongword1 时,它并没有告诉我使用正确的word1)。我尝试使用只有两个单词的之前的 coherency.txt 文件,它也没有将任何单词标记为不正确...虽然不知道如何解决它...有什么想法吗?
  • 可能您必须将新的语言扩展注册到该工具。已经完成了所有的工作来添加一种新的语言,就像这里描述的那样? languagetool.org/development/#newlanguage
  • 是的,我的新语言运行良好 - 我只是想修改一个 java 规则(如这里:languagetool.org/development/#javarules
【解决方案3】:
import java.util.regex.Matcher;
import java.util.regex.Pattern;




public class Test {

        public static void main(String[] args) {

            String txtFromFile = "Hipopotamus=hIppoPotamus=hiiippotamus Magazine=Mazagine=Masagine";
            String searchWord = "Masagine";
            Pattern searchPattern= Pattern.compile("\\s*(\\w+=)*?("+searchWord+")");
            Matcher m = searchPattern.matcher(txtFromFile);
            String source = "";
            while(m.find()) {
                source = m.group();
                System.out.println("word pairs:"+source);
            }
            System.out.println("correct word:"+source.split("=")[0]);
        }
    }

【讨论】:

    最近更新 更多