Search in sources :

Example 1 with SingleWord

use of net.heartsome.cat.ts.core.bean.SingleWord in project translationstudio8 by heartsome.

the class SpellQA method startQA.

@Override
public String startQA(final QAModel model, IProgressMonitor monitor, IFile iFile, QAXmlHandler xmlHandler, QATUDataBean tuDataBean) {
    if (tuDataBean.getTgtContent() == null || "".equals(tuDataBean.getTgtContent())) {
        return "";
    }
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    hasError = false;
    isContinue = false;
    //目标语言
    final String tgtLang = tuDataBean.getTgtLang();
    //若未配置该目标语言的词典,退出程序的执行
    if (nonSpellTarLangList.indexOf(tgtLang) != -1) {
        return "";
    }
    String tgtPureText = TextUtil.resetSpecialString(tuDataBean.getTgtPureText());
    String lineNumber = tuDataBean.getLineNumber();
    String rowId = tuDataBean.getRowId();
    if (spelling == null) {
        if (isHunspell) {
            spelling = new Hunspell(model.getShell());
            if (ignoreNontrans) {
                nontransOper = new NonTransElementOperate();
                nontransOper.openNonTransDB();
            }
        } else {
            spelling = new AspellChecker();
        }
    }
    // 若拼写检查器错误,或者出错,返回 null
    if (spelling == null || spelling.isError()) {
        return null;
    }
    //如果该拼写检查实例为空,退出执行,并且下次遇到相同目标语言不再检查
    if (!spelling.checkLangAvailable(tgtLang)) {
        nonSpellTarLangList.add(tgtLang);
        if (!isHunspell) {
            Display.getDefault().syncExec(new Runnable() {

                public void run() {
                    String message = Messages.getString("qa.SpellQA.addTip1");
                    message = MessageFormat.format(message, new Object[] { tgtLang });
                    isContinue = MessageDialog.openConfirm(model.getShell(), Messages.getString("qa.all.dialog.confirm"), message);
                }
            });
            if (!isContinue) {
                monitor.setCanceled(true);
            }
        }
        return "";
    }
    List<SingleWord> errorWords;
    if (isHunspell) {
        LinkedList<SingleWord> wordList = new LinkedList<SingleWord>();
        getSingleWords(tgtPureText, wordList);
        errorWords = spelling.getErrorWords(null, wordList, tgtLang);
    } else {
        LinkedList<SingleWord> wordList = new LinkedList<SingleWord>();
        getSingleWords(tgtPureText, wordList);
        errorWords = spelling.getErrorWords(tgtPureText, wordList, tgtLang);
    }
    if (spelling.isError()) {
        return null;
    }
    //开始输入结果
    if (errorWords == null || errorWords.size() == 0) {
        return "";
    }
    String qaTypeText = Messages.getString("qa.all.qaItem.SpellQA");
    //		String errorTip = Messages.getString("qa.SpellQA.tip1");
    //		for (int i = 0; i < errorWords.size(); i++) {
    //			errorTip += "'" + errorWords.get(i).getPureWord() + "' 、";
    //		}
    //		errorTip = errorTip.substring(0, errorTip.length() - 1);
    //		errorTip += Messages.getString("qa.SpellQA.tip2");
    hasError = true;
    super.printQAResult(new QAResultBean(level, QAConstant.QA_SPELL, qaTypeText, null, tuDataBean.getFileName(), lineNumber, tuDataBean.getSrcContent(), tuDataBean.getTgtContent(), rowId));
    String result = "";
    if (hasError && level == 0) {
        result = QAConstant.QA_SPELL;
    }
    return result;
}
Also used : NonTransElementOperate(net.heartsome.cat.ts.ui.qa.nonTransElement.NonTransElementOperate) AspellChecker(net.heartsome.cat.ts.ui.qa.spell.AspellChecker) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) QAResultBean(net.heartsome.cat.ts.ui.qa.model.QAResultBean) SingleWord(net.heartsome.cat.ts.core.bean.SingleWord) Hunspell(net.heartsome.cat.ts.ui.qa.spell.Hunspell) LinkedList(java.util.LinkedList)

Example 2 with SingleWord

use of net.heartsome.cat.ts.core.bean.SingleWord in project translationstudio8 by heartsome.

the class SpellQA method getSingleWords.

/**
	 * 获取单个单词,这里面主要是根据不同的选项。得到要进行拼写检查的单词
	 */
private void getSingleWords(String pureText, List<SingleWord> tgtWordList) {
    stringToken = new StringTokenizer(pureText, Constant.SEPARATORS, false);
    List<Integer[]> ignoreParaList = null;
    // 如果要处理忽略非译元素,那么执行如下操作 (备注:所有的忽略项。只是针对 hunspell)
    if (isHunspell && ignoreNontrans) {
        ignoreParaList = nontransOper.getIgnorePara(pureText, null);
    }
    int start = 0;
    int length = 0;
    int end = 0;
    while (stringToken.hasMoreTokens()) {
        String pureWord = stringToken.nextToken();
        start = pureText.indexOf(pureWord, start);
        length = pureWord.length();
        end = start + length;
        // 经过一系列的判断,从而删除一些不符合标准的单词。将剩下的单词传入拼写检查器中进行检查
        if (isHunspell) {
            // 是否忽略非译元素
            if (ignoreNontrans) {
                boolean needIgnore = false;
                for (Integer[] ignoreIndexs : ignoreParaList) {
                    if (start >= ignoreIndexs[0] && end <= ignoreIndexs[1]) {
                        needIgnore = true;
                        break;
                    }
                }
                if (needIgnore) {
                    start = start + pureWord.length();
                    continue;
                }
            }
            // 是否忽略首字母为数字
            if (ignoreDigitalFirst && checkDigitalFirst(pureWord)) {
                start = start + pureWord.length();
                continue;
            }
            // 是否忽略首字母为大写
            if (ignoreUpperCaseFirst && checkUpperCaseFirst(pureWord)) {
                start = start + pureWord.length();
                continue;
            }
            //是否忽略全大写单词
            if (ignoreAllUpperCase && checkAllUpperCase(pureWord)) {
                start = start + pureWord.length();
                continue;
            }
        }
        tgtWordList.add(new SingleWord(null, pureWord, start, length));
        start = start + pureWord.length();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) SingleWord(net.heartsome.cat.ts.core.bean.SingleWord)

Example 3 with SingleWord

use of net.heartsome.cat.ts.core.bean.SingleWord in project translationstudio8 by heartsome.

the class RealTimeSpellCheck method getSingleWords.

/**
	 * 获取单个单词,这里面主要是根据不同的选项。得到要进行拼写检查的单词
	 */
private void getSingleWords(String tgtText, List<SingleWord> tgtWordList) {
    stringToken = new StringTokenizer(tgtText, Constant.SEPARATORS, false);
    List<Integer[]> ignoreParaList = null;
    // 如果要处理忽略非译元素,那么执行如下操作 (备注:所有的忽略项。只是针对 hunspell)
    if (isHunspell && ignoreNontrans) {
        List<Integer> tagPositionList = getTagPosition(tgtText);
        String pureText = PlaceHolderEditModeBuilder.PATTERN.matcher(tgtText).replaceAll("");
        ignoreParaList = nontransOper.getIgnorePara(pureText, tagPositionList);
    }
    int start = 0;
    int length = 0;
    int end = 0;
    while (stringToken.hasMoreTokens()) {
        String word = stringToken.nextToken();
        String pureWord = PlaceHolderEditModeBuilder.PATTERN.matcher(word).replaceAll("");
        start = tgtText.indexOf(word, start);
        length = word.length();
        end = start + length;
        // 经过一系列的判断,从而删除一些不符合标准的单词。将剩下的单词传入拼写检查器中进行检查
        if (isHunspell) {
            // 是否忽略非译元素
            if (ignoreNontrans) {
                boolean needIgnore = false;
                for (Integer[] ignoreIndexs : ignoreParaList) {
                    if (start >= ignoreIndexs[0] && end <= ignoreIndexs[1]) {
                        needIgnore = true;
                        break;
                    }
                }
                if (needIgnore) {
                    start = start + word.length();
                    continue;
                }
            }
            // 是否忽略首字母为数字
            if (ignoreDigitalFirst && checkDigitalFirst(pureWord)) {
                start = start + word.length();
                continue;
            }
            // 是否忽略首字母为大写
            if (ignoreUpperCaseFirst && checkUpperCaseFirst(pureWord)) {
                start = start + word.length();
                continue;
            }
            //是否忽略全大写单词
            if (ignoreAllUpperCase && checkAllUpperCase(pureWord)) {
                start = start + word.length();
                continue;
            }
        }
        tgtWordList.add(new SingleWord(word, pureWord, start, length));
        start = start + word.length();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) SingleWord(net.heartsome.cat.ts.core.bean.SingleWord)

Example 4 with SingleWord

use of net.heartsome.cat.ts.core.bean.SingleWord in project translationstudio8 by heartsome.

the class AspellChecker method parseSuggestions.

/**
     * 分解查询结果,将错误单词添加到结果集中
     * 查询结果如:& yayay 62 1: Maya, ayah, ya, Yalu, Yuan, yaws, yuan, yaw, 第二个值为错误单词,第三个值为建议单词个数,第四个值为该单词起始下标(从1开始的)
     * @param line ;
     */
private void parseSuggestions(String line) {
    StringTokenizer st = new StringTokenizer(line);
    if (st.hasMoreTokens()) {
        st.nextToken();
        st.nextToken();
        st.nextToken();
        int start = -1;
        if (st.hasMoreTokens()) {
            // 备注,aspell 的查询结果,每个单词的起始坐标是从 1 开始的
            start = Integer.parseInt(st.nextToken().replace(":", "")) - 1;
        } else {
            return;
        }
        int startAdd = 0;
        if (tagPositionList != null) {
            // 将标记放回去,使每个非译片段回复之前未去标记的状态
            for (Integer tagIndex : tagPositionList) {
                if (start > tagIndex) {
                    startAdd++;
                }
            }
            start = start + startAdd;
        }
        boolean exsit = false;
        for (SingleWord word : wordList) {
            if (word.getStart() == start) {
                errorWords.add(word);
                exsit = true;
                break;
            }
        }
        if (!exsit) {
            logger.error("Aspell check error.");
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) SingleWord(net.heartsome.cat.ts.core.bean.SingleWord)

Example 5 with SingleWord

use of net.heartsome.cat.ts.core.bean.SingleWord in project translationstudio8 by heartsome.

the class HsMultiCellEditorControl method tgtTextRealTimeSpellCheck.

/**
	 * 当文本处于正在编辑时,实时进行拼写检查,<div style='color:red'>该方法与{@link #tgtTextFirstRealTimeSpellCheck} 类似</div>
	 */
private static void tgtTextRealTimeSpellCheck(final String tgtLang, final HsMultiCellEditor targetEditor) {
    final StyledTextCellEditor tgtEditor = targetEditor.getCellEditor();
    final StyledText text = tgtEditor.getSegmentViewer().getTextWidget();
    if (tgtLang == null) {
        return;
    }
    text.addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent e) {
            String tgtText = text.getText();
            if (tgtText.isEmpty()) {
                return;
            }
            String endStr = tgtText.substring(tgtText.length() - 1, tgtText.length());
            if (endStr.matches(ENDREGEX)) {
                List<SingleWord> errorWordList = new LinkedList<SingleWord>();
                errorWordList = spellTrigger.getErrorWords(tgtText, tgtLang);
                if (errorWordList != null && errorWordList.size() > 0) {
                    targetEditor.highLightedErrorWord(tgtText, errorWordList);
                } else {
                    targetEditor.refreshErrorWordsStyle(null);
                }
            }
        }
    });
}
Also used : ModifyEvent(org.eclipse.swt.events.ModifyEvent) StyledText(org.eclipse.swt.custom.StyledText) ModifyListener(org.eclipse.swt.events.ModifyListener) SingleWord(net.heartsome.cat.ts.core.bean.SingleWord) List(java.util.List) LinkedList(java.util.LinkedList)

Aggregations

SingleWord (net.heartsome.cat.ts.core.bean.SingleWord)7 LinkedList (java.util.LinkedList)3 StringTokenizer (java.util.StringTokenizer)3 StyledText (org.eclipse.swt.custom.StyledText)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 QAResultBean (net.heartsome.cat.ts.ui.qa.model.QAResultBean)1 NonTransElementOperate (net.heartsome.cat.ts.ui.qa.nonTransElement.NonTransElementOperate)1 AspellChecker (net.heartsome.cat.ts.ui.qa.spell.AspellChecker)1 Hunspell (net.heartsome.cat.ts.ui.qa.spell.Hunspell)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 StyleRange (org.eclipse.swt.custom.StyleRange)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1 Point (org.eclipse.swt.graphics.Point)1 TextStyle (org.eclipse.swt.graphics.TextStyle)1