Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
APDPlat - Application Product Development Platform Copyright (c) 2013, 杨尚川, yang-shangchuan@qq.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
 
 package org.apdplat.qa.score.answer;
 
 import java.util.List;
 
对候选答案进行评分 【文本对齐评分组件】

Author(s):
杨尚川
 
 
     private static final Logger LOG = LoggerFactory.getLogger(TextualAlignmentCandidateAnswerScore.class);
     private ScoreWeight scoreWeight = new ScoreWeight();
 
     @Override
     public void setScoreWeight(ScoreWeight scoreWeight) {
         this. = scoreWeight;
     }
 
     protected List<StringgetQuestionTerms(Question question) {
         return question.getTerms();
     }
 
     @Override
     public void score(Question questionEvidence evidenceCandidateAnswerCollection candidateAnswerCollection) {
         .debug("*************************");
         .debug("文本对齐评分开始");
         //1、对问题进行分词
         List<StringquestionTerms = getQuestionTerms(question);
         int questionTermsSize = questionTerms.size();
         //2、获取证据文本
         String evidenceText = evidence.getTitle() + evidence.getSnippet();
         //将每一个候选答案都放到问题的每一个位置,查找在证据中是否有匹配文本
         for (CandidateAnswer candidateAnswer : candidateAnswerCollection.getAllCandidateAnswer()) {
             //3、计算候选答案的文本对齐
             .debug("计算候选答案 " + candidateAnswer.getAnswer() + " 的文本对齐");
             for (int i = 0; i < questionTermsSizei++) {
                 StringBuilder textualAlignment = new StringBuilder();
                 for (int j = 0; j < questionTermsSizej++) {
                     if (i == j) {
                         textualAlignment.append(candidateAnswer.getAnswer());
                     } else {
                         textualAlignment.append(questionTerms.get(j));
                     }
                 }
                 String textualAlignmentPattern = textualAlignment.toString();
                 if (question.getQuestion().trim().equals(textualAlignmentPattern.trim())) {
                     .debug("文本对齐模式和原问题相同,忽略:" + textualAlignmentPattern);
                     continue;
                 }
                 //4、演化为多个模式,支持模糊匹配
                 List<TermtextualAlignmentPatternTerms = Tools.getTerms(textualAlignmentPattern);
                 List<Stringpatterns = new ArrayList<String>();
                 patterns.add(textualAlignmentPattern);
                 StringBuilder str = new StringBuilder();
                 int len = textualAlignmentPatternTerms.size();
                 for (int t = 0; t < lent++) {
                     str.append(textualAlignmentPatternTerms.get(t).getName());
                     if (t < len - 1) {
                         str.append(".{0,5}");
                     }
                 }
                 patterns.add(str.toString());
                 //5、判断文本是否对齐
                 int count = 0;
                 int length = 0;
                for (String pattern : patterns) {
                    //LOG.debug("模式:"+pattern);
                    Pattern p = Pattern.compile(pattern);
                    Matcher matcher = p.matcher(evidenceText);
                    while (matcher.find()) {
                        String text = matcher.group();
                        .debug("对齐的文本:" + text);
                        .debug("对齐的模式:" + pattern);
                        count++;
                        length += text.length();
                    }
                }
                //6、打分
                if (count > 0) {
                    double avgLen = (doublelength / count;
					//问题长度questionLen为正因子
                    //匹配长度avgLen为负因子
                    int questionLen = question.getQuestion().length();
                    double score = questionLen / avgLen;
                    score *= .getTextualAlignmentCandidateAnswerScoreWeight();
                    candidateAnswer.addScore(score);
                    .debug("文本对齐 " + count + " 次,分值:" + score);
                }
            }
        }
        .debug("文本对齐评分结束");
        .debug("*************************");
    }
New to GrepCode? Check out our FAQ X