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.model;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
问题有多个证据 证据用于提取候选答案

Author(s):
杨尚川
 
 public class Question {
 
     private static final Logger LOG = LoggerFactory.getLogger(Question.class);
     private String question;
     private final List<Evidenceevidences = new ArrayList<>();
 
     private String expectAnswer;
 
     //候选的问题类型,对问题进行分类的时候,可能会有多个类型
     private final Set<QuestionTypecandidateQuestionTypes = new HashSet<>();
 
     public void clearCandidateQuestionType() {
         .clear();
     }
 
     public void addCandidateQuestionType(QuestionType questionType) {
         .add(questionType);
     }
 
     public void removeCandidateQuestionType(QuestionType questionType) {
         .remove(questionType);
     }
 
         return ;
     }
 
     public Map.Entry<StringIntegergetHot() {
         List<StringquestionTerms = getTerms();
         Map<StringIntegermap = new HashMap<>();
         List<Termterms = WordParser.parse(getText());
         for (Term term : terms) {
             Integer count = map.get(term.getName());
             if (count == null) {
                 count = 1;
             } else {
                 count++;
             }
             map.put(term.getName(), count);
         }
         Map<StringIntegerquestionMap = new HashMap<>();
         for (String questionTerm : questionTerms) {
             Integer count = map.get(questionTerm);
             if (questionTerm.length() > 1 && count != null) {
                 questionMap.put(questionTermcount);
                 .debug("问题热词统计: " + questionTerm + " " + map.get(questionTerm));
             }
         }
         List<Map.Entry<StringInteger>> list = Tools.sortByIntegerValue(questionMap);
         Collections.reverse(list);
         if (!list.isEmpty()) {
             return list.get(0);
         }
         return null;
    }
    public int getExpectAnswerRank() {
        if ( == null) {
            .info("未指定期望的答案");
            return -2;
        }
        List<CandidateAnswercandidateAnswers = this.getAllCandidateAnswer();
        int len = candidateAnswers.size();
        for (int i = 0; i < leni++) {
            CandidateAnswer candidateAnswer = candidateAnswers.get(i);
            if (.trim().equals(candidateAnswer.getAnswer().trim())) {
                return (i + 1);
            }
        }
        return -1;
    }

    
对问题进行分词

Returns:
分词结果
    public List<StringgetTerms() {
        List<Stringresult = new ArrayList<>();
        List<Termterms = WordParser.parse(.replace("?""").replace("?"""));
        for (Term term : terms) {
            result.add(term.getName());
        }
        return result;
    }

    
获取所有候选答案

Returns:
所有候选答案
        Map<StringDoublemap = new HashMap<>();
        for (Evidence evidence : ) {
            for (CandidateAnswer candidateAnswer : evidence.getCandidateAnswerCollection().getAllCandidateAnswer()) {
                Double score = map.get(candidateAnswer.getAnswer());
                //候选答案的分值和证据的分值 用于计算最终的候选答案分值
                Double candidateAnswerFinalScore = candidateAnswer.getScore() + evidence.getScore();
                if (score == null) {
                    score = candidateAnswerFinalScore;
                } else {
                    score += candidateAnswerFinalScore;
                }
                map.put(candidateAnswer.getAnswer(), score);
            }
        }
        //组装候选答案
        List<CandidateAnswercandidateAnswers = new ArrayList<>();
        for (Map.Entry<StringDoubleentry : map.entrySet()) {
            String answer = entry.getKey();
            Double score = entry.getValue();
            if (answer != null && score != null && score > 0 && score < .) {
                CandidateAnswer candidateAnswer = new CandidateAnswer();
                candidateAnswer.setAnswer(answer);
                candidateAnswer.setScore(score);
                candidateAnswers.add(candidateAnswer);
            }
        }
        Collections.sort(candidateAnswers);
        Collections.reverse(candidateAnswers);
        //过滤候选答案
        if ( != null) {
            .filter(thiscandidateAnswers);
        }
        //分值归一化
        if (candidateAnswers.size() > 0) {
            double baseScore = candidateAnswers.get(0).getScore();
            for (CandidateAnswer candidateAnswer : candidateAnswers) {
                double score = candidateAnswer.getScore() / baseScore;
                candidateAnswer.setScore(score);
            }
        }
        return candidateAnswers;
    }

    
获取topN候选答案

Parameters:
topN
Returns:
topN候选答案
    public List<CandidateAnswergetTopNCandidateAnswer(int topN) {
        List<CandidateAnswertopNcandidateAnswers = new ArrayList<>();
        List<CandidateAnswerallCandidateAnswers = getAllCandidateAnswer();
        if (topN > allCandidateAnswers.size()) {
            topN = allCandidateAnswers.size();
        }
        for (int i = 0; i < topNi++) {
            topNcandidateAnswers.add(allCandidateAnswers.get(i));
        }
        return topNcandidateAnswers;
    }
    public String getText() {
        StringBuilder text = new StringBuilder();
        for (Evidence evidence : ) {
            text.append(evidence.getTitle()).append(evidence.getSnippet());
        }
        return text.toString();
    }
    public String getQuestion() {
        return ;
    }
    public void setQuestion(String question) {
        this. = question;
    }
    public List<EvidencegetEvidences() {
        return this.;
    }
    public void addEvidences(List<Evidenceevidences) {
        this..addAll(evidences);
    }
    public void addEvidence(Evidence evidence) {
        this..add(evidence);
    }
    public void removeEvidence(Evidence evidence) {
        this..remove(evidence);
    }
    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append("?. ").append().append("\n\n");
        for (Evidence evidence : this.) {
            result.append("Title: ").append(evidence.getTitle()).append("\n");
            result.append("Snippet: ").append(evidence.getSnippet()).append("\n\n");
        }
        return result.toString();
    }
    public String toString(int index) {
        StringBuilder result = new StringBuilder();
        result.append("?").append(index).append(". ").append().append("\n\n");
        for (Evidence evidence : this.) {
            result.append("Title: ").append(evidence.getTitle()).append("\n");
            result.append("Snippet: ").append(evidence.getSnippet()).append("\n\n");
        }
        return result.toString();
    }
    public String getExpectAnswer() {
        return ;
    }
    public void setExpectAnswer(String expectAnswer) {
        this. = expectAnswer;
    }
    public void setQuestionType(QuestionType questionType) {
        this. = questionType;
    }
    public QuestionType getQuestionType() {
        return ;
    }
        return ;
    }
    public void setCandidateAnswerFilter(CandidateAnswerFilter candidateAnswerFilter) {
        this. = candidateAnswerFilter;
    }
New to GrepCode? Check out our FAQ X