当前位置: 首页>>代码示例>>Java>>正文


Java Instance类代码示例

本文整理汇总了Java中edu.cmu.minorthird.classify.Instance的典型用法代码示例。如果您正苦于以下问题:Java Instance类的具体用法?Java Instance怎么用?Java Instance使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Instance类属于edu.cmu.minorthird.classify包,在下文中一共展示了Instance类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createInstance

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
public Instance createInstance(String question){
    String[] tokens = question.split("\\s+");
    List<String> words = new ArrayList<String>();
    for (String token : tokens) words.add(token);
    try {
        String parse = StanfordParser.parse(question);
        return createInstance(question,parse);
    } catch (Exception e) {
        log.error("Failed to parse question, using only word-level features.",e);
        List<Term> terms = new ArrayList<Term>();
        for (String word : words) terms.add(new Term(0,0,word));

        MutableInstance instance = new MutableInstance(question);
        addWordLevelFeatures(instance, terms, null);
        return instance;
    }
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:18,代码来源:EnglishFeatureExtractor.java

示例2: printFeaturesFromQuestions

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
/**
 * Prints the features generated for each example in an input file.  If feature
 * types are included as command-line arguments, only those types are printed. 
 * Otherwise, all features are printed.
 * 
 * @param questionSetFileName the name of the file containing the dataset to load
 * @param features a List of the features to print
 */
public void printFeaturesFromQuestions (String questionSetFileName, List<String> features) {
    String questions = IOUtil.readFile(questionSetFileName);
    
    for (String question : questions.split("[\\n\\r\\f]")) {
        Instance instance = createInstance(question);
        StringBuilder sb = new StringBuilder();
        if (features.size() > 0) {
            for(Iterator it = instance.binaryFeatureIterator(); it.hasNext();) {
                Feature feat = (Feature)it.next();
                String name = "";
                for (String s : feat.getName()) name += "."+s;
                name = name.replaceFirst(".","");
                if (features.contains(feat.getName()[0]))
                    sb.append(name+"  ");
            }
            System.out.println(sb.toString() + " " + question);
        }
        else System.out.println(instance + " " + question);
    }
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:29,代码来源:FeatureExtractor.java

示例3: classify

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
public List<AnswerType> classify(Instance instance)
{
    List<AnswerType> res = new ArrayList<AnswerType>();
    
    for (QuestionClassifier classifier : classifiers) {
        List<AnswerType> classifierResult = classifier.classify(instance);
        if (classifierResult != null && classifierResult.size() > 0
             && !classifierResult.get(0).getType().equals("NOANS")) {
            if (!mergeResults) return classifierResult;
            for (AnswerType atype : classifierResult) 
                if (!res.contains(atype)) res.add(atype);
        } 
    }
    Collections.sort(res, atypeComparator);
    for (int i = 0; i < res.size(); i++) {
        if (res.get(i).getConfidence() < confidenceThreshold) {
            res.remove(i);
            i--;
        }
    }
    if (res.size() == 0)
        res.add(AnswerType.constructFromString("NONE"));
    return res;
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:25,代码来源:HybridQuestionClassifier.java

示例4: matches

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
/**
 * Tells whether a given Instance matches this Rule by returning
 * the appropriate answer type if it does match, and <code>null</code> 
 * if it does not.
 * 
 * @param instance the Instance to consider
 * @return the answer type and subtype formed by matching this Rule to the
 * given Instance, or <code>null</code> if this Rule does not match the Instance.
 * @throws IllegalStateException if this Rule has not been compiled yet
 */
public AnswerType matches (Instance instance) {
    if (elements == null) 
        throw new IllegalStateException("Must compile rule before using it");
    
    String retAtype = atype;
    AnswerType res = null;
    // All elements of the rule must match in order for the rule to match
    for (RuleElement element : elements) {
        String result = element.matches(instance);
        if (result == null) {
            return null;
        } else if (element.getFeature().equals("FOCUS_TYPE")) {
            result = result.toUpperCase();
            if (!result.equals(atype) && result != "")
                retAtype = atype + "." + result;
        }
    }
    res = AnswerType.constructFromString(retAtype);
    res.setConfidence(confidence);
    return res;
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:32,代码来源:Rule.java

示例5: matches

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
/**
 * Tells whether a given Instance has a feature that matches this RuleElement. 
 * by returning the matching value upon a successful match or <code>null</code>
 * if this RuleElement does not match the Instance.  When one of 
 * this RuleElement's values contains the substring "->", the portion of the 
 * value before the "->" is used for matching against the input Instance, while
 * the portion of the value after the "->" is used as the matching value;
 * 
 * @param instance the Instance to consider
 * @return whether a given Instance has a feature that matches this RuleElement
 * @throws IllegalStateException if this RuleElement has not been compiled yet
 */
public String matches (Instance instance) {
    if (values == null) 
        throw new IllegalStateException("values not initialized");
    for (Iterator it = instance.binaryFeatureIterator(); it.hasNext();) {
        Feature f = (Feature)it.next();
        if (!f.getPart(0).equals(feature)) continue;
        for (String value : values) {
            String[] parts = value.split("=",-1);
            if (parts[0].equals(f.getPart(1).trim())) {
                return (parts.length == 2) ? parts[1] : f.getPart(1).trim();
            }
        }
    }
    return null;
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:28,代码来源:RuleElement.java

示例6: createInstance

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
/**
 * Convenience method that tokenizes the given question by whitespace, creates
 * Terms, and calls {@link #createInstance(List, String)}.
 * 
 * @param question the question to create an Instance from
 * @param parseTree the syntactic parse tree of the question
 */
public Instance createInstance(String question, String parseTree){
    String[] tokens = question.split("\\s+");
    List<Term> terms = new ArrayList<Term>();
    for (String token : tokens) {
        terms.add(new Term(0,0,token));
    }
    return createInstance(terms,parseTree);
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:16,代码来源:FeatureExtractor.java

示例7: createExample

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
/**
 * Creates an edu.cmu.minorthird.classify.Example object from one line
 * of a dataset file using {@link #createInstance(String, String)}.
 * 
 * @param datasetLine the line from the dataset file from which to create
 * the Example 
 * @return the Example created
 * @throws Exception
 */
public Example[] createExample(String datasetLine) throws Exception {
    Matcher m=datasetExamplePattern.matcher(datasetLine);

    if (!m.matches()) 
        throw new Exception("Malformed dataset line:\n"+datasetLine);

    String[] aTypes = null;

    aTypes = m.group(labelPosition)
                .replaceAll(",$", "")   
                .replaceAll(",", ".")
                .split("\\|");
    String question = m.group(questionPosition);
    String sentParse = null;
    if (parsePosition > -1) sentParse = m.group(parsePosition);

    Instance instance = createInstance(question,sentParse);

    Example[] result = new Example[aTypes.length];

    //create example(s) and add it to list
    for(int i=0;i<aTypes.length;i++){
        String newATypeName=HierarchicalClassifier.getHierarchicalClassName(aTypes[i],classLevels,useClassLevels);
        result[i] = new Example(instance,new ClassLabel(newATypeName));
    }
    return result;
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:37,代码来源:FeatureExtractor.java

示例8: classification

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
public ClassLabel classification(Instance instance){
	String labelName="";
	double weight=1;
	for(int i=0;i<classLevels;i++){
		Classifier currentClassifier=(Classifier)classifiers.get(labelName);
		ClassLabel currentLabel=currentClassifier.classification(instance);
		labelName=getNewLabelName(labelName,currentLabel.bestClassName(),i);
		weight*=currentLabel.bestWeight();
	}
	return new ClassLabel(labelName,weight);
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:12,代码来源:HierarchicalClassifier.java

示例9: explain

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
public String explain(Instance instance){
	String labelName="";
	String explanation="";
	for(int i=0;i<classLevels;i++){
		Classifier currentClassifier=(Classifier)classifiers.get(labelName);
		ClassLabel currentLabel=currentClassifier.classification(instance);
		labelName=getNewLabelName(labelName,currentLabel.bestClassName(),i);
		explanation+=currentClassifier.explain(instance);
	}
	return explanation;
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:12,代码来源:HierarchicalClassifier.java

示例10: nextQuery

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
public Instance nextQuery(){
	return instancePool.nextInstance();
	/*
	ClassifierLearner[] learners=toValueArray(classifierLearners);
	for(int i=0;i<learners.length;i++){
		if(learners[i].hasNextQuery()){
			return learners[i].nextQuery();
		}
	}
	return null;
	*/
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:13,代码来源:HierarchicalClassifierLearner.java

示例11: classify

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
public List<AnswerType> classify(Instance instance) {
    List<AnswerType> res = new ArrayList<AnswerType>();
    ClassLabel label = null;
    synchronized (classifier) {
        label = classifier.classification(instance);
    }
    String labelStr = label.bestClassName().replaceAll("-",".");
    AnswerType atype = AnswerType.constructFromString(labelStr);
    double weight = label.bestWeight();
    if (weight > 1.0) weight = (1 - (1 / weight));
    atype.setConfidence(weight);
    res.add(atype);
    return res;
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:15,代码来源:TrainedQuestionClassifier.java

示例12: classify

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
public List<AnswerType> classify(Instance instance) {
    List<AnswerType> res = new ArrayList<AnswerType>();
    for (Rule rule : rules) {
        AnswerType result = rule.matches(instance);
        if (result != null) {
            res.add(result);
            //return res;
        }
    }
    return res;
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:12,代码来源:RuleBasedQuestionClassifier.java

示例13: getFeatureValue

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
/**
 * Retrieves the value of the given feature from the given Instance.
 * 
 * @param instance the Instance to consider
 * @param featName the name of the feature
 */
public static String getFeatureValue (Instance instance, String featName) {
    for (Iterator it = instance.binaryFeatureIterator(); it.hasNext();) {
        Feature f = (Feature)it.next();
        if (f.getPart(0).equals(featName)) {
            String val = f.getPart(1).trim();
            val = val.equals("-") ? null : val;
            return val;
        }
    }
    return null;
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:18,代码来源:RuleBasedQuestionClassifier.java

示例14: getAnswerTypes

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
/**
 * Classifies the question represented by the given List of Terms and parse tree 
 * as having a particular answer type and possibly subtype. 
 * 
 * @param terms the Terms that make up the question to classify
 * @param parseTreeStr the syntactic parse tree of the question, in String format
 * 
 * @return the candidate answer type / subtypes.
 * @throws Exception
 */
public List<AnswerType> getAnswerTypes(List<Term> terms, String parseTreeStr) throws Exception {
    if(!isInitialized())
        throw new Exception("getAnswerTypes called while not initialized");

    String question = "";
    for (Term term : terms) question += term.getText()+" ";

    // create the instance
    Instance instance = new MutableInstance(question);
    if (extractor != null)
        instance = extractor.createInstance(terms,parseTreeStr);

    return classify(instance);
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:25,代码来源:QuestionClassifier.java

示例15: createInstance

import edu.cmu.minorthird.classify.Instance; //导入依赖的package包/类
/**
 * Creates an instance for training/evaluation or classification from an
 * answer candidate.
 * 
 * @param features selected features
 * @param result answer candidate
 * @param results all answers to the question
 * @return instance for training/evaluation or classification
 */
private static Instance createInstance(String[] features, Result result,
		Result[] results) {
	// create instance from source object
	MutableInstance instance = new MutableInstance(result);
	
	// add selected features to the instance
	addSelectedFeatures(instance, features, result, results);
	return instance;
}
 
开发者ID:claritylab,项目名称:lucida,代码行数:19,代码来源:ScoreNormalizationFilter.java


注:本文中的edu.cmu.minorthird.classify.Instance类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。