加载CRF模型

@param modelPath
@return
182665
private static synchronized SplitWord initCRFModel(KV<String, SplitWord> kv) {
        try {
            if (kv.getV() != null) {
                return kv.getV();
            }

            long start = System.currentTimeMillis();
            LOG.debug("begin init crf model!");
            try (InputStream is = PathToStream.stream(kv.getK())) {
                SplitWord crfSplitWord = new SplitWord(Model.load(CRFModel.class, is));
                kv.setV(crfSplitWord);
                LOG.info("load crf use time:" + (System.currentTimeMillis() - start) + " path is : " + kv.getK());
                return crfSplitWord;
            }
        } catch (Exception e) {
            LOG.error(kv + " load err " + e.getMessage());
            return null;
        }
    }
1048813
private String buildUrl( String methodPath, CPath path )
    {
        StringBuilder sb = new StringBuilder();

        sb.append( END_POINT ).append( '/' ).append( methodPath );

        if ( path != null ) {
            sb.append( '/' ).append( scope ).append( path.getUrlEncoded() );
        }

        return sb.toString();
    }
823197
@RequestMapping(value = "/api/plugins", method = RequestMethod.POST)
    public
    @ResponseBody
    HashMap<String, Object> addPluginPath(Model model, Plugin add) throws Exception {
        PluginManager.getInstance().addPluginPath(add.getPath());

        return pluginInformation();
    }
206120
protected String registerModel(Swagger swagger, Class<?> modelClass) {
        final Tag modelTag = getModelTag(modelClass);
        final String ref = modelTag.getName();

        if (swagger.getDefinitions() != null && swagger.getDefinitions().containsKey(ref)) {
            // model already registered
            return ref;
        }

        ModelImpl model = new ModelImpl();
        swagger.addDefinition(modelTag.getName(), model);

        if (!Strings.isNullOrEmpty(modelTag.getDescription())) {
            model.setDescription(modelTag.getDescription());
        }

        // document any exposed model properties
        for (Field field : ClassUtil.getAllFields(modelClass)) {

            if (field.isAnnotationPresent(Undocumented.class)) {
                // undocumented field, skip
                continue;
            }

            if (!field.isAnnotationPresent(ApiProperty.class) && !field.isAnnotationPresent(Param.class)) {
                // not a documented model property
                continue;
            }

            Property property;
            Class<?> fieldType = field.getType();
            if (fieldType.isArray()) {
                // ARRAY
                Class<?> componentType = fieldType.getComponentType();
                Property componentProperty = getSwaggerProperty(swagger, componentType);
                ArrayProperty arrayProperty = new ArrayProperty();
                arrayProperty.setItems(componentProperty);
                property = arrayProperty;
            } else if (Collection.class.isAssignableFrom(fieldType)) {
                // COLLECTION
                Class<?> componentClass = ClassUtil.getGenericType(field);
                Property componentProperty = getSwaggerProperty(swagger, componentClass);
                ArrayProperty arrayProperty = new ArrayProperty();
                arrayProperty.setItems(componentProperty);
                if (Set.class.isAssignableFrom(fieldType)) {
                    arrayProperty.setUniqueItems(true);
                }
                property = arrayProperty;
            } else {
                // OBJECT
                property = getSwaggerProperty(swagger, fieldType);
            }
            property.setRequired(field.isAnnotationPresent(Required.class) || field.isAnnotationPresent(NotNull.class));

            if (field.isAnnotationPresent(ApiProperty.class)) {
                ApiProperty apiProperty = field.getAnnotation(ApiProperty.class);
                if (!Strings.isNullOrEmpty(apiProperty.name())) {
                    property.setName(apiProperty.name());
                }

                property.setDescription(translate(apiProperty.descriptionKey(), apiProperty.description()));

                if (!Strings.isNullOrEmpty(apiProperty.example())) {
                    property.setExample(apiProperty.example());
                }

                if (!Strings.isNullOrEmpty(apiProperty.defaultValue())) {
                    property.setDefault(apiProperty.defaultValue());
                }

                if (apiProperty.readOnly()) {
                    property.setReadOnly(true);
                }
            }

            String name = prepareParameterName(Optional.fromNullable(property.getName()).or(field.getName()));
            property.setName(name);

            model.addProperty(name, property);

        }

        return ref;
    }
805169
public static ModelLoader load(InputStream stream) throws IOException {
        JsonNode tree = MAPPER.readTree(stream);
        JsonNode version = tree.get("FormatVersion");
        if (version == null || !version.isObject()) {
            throw new IllegalStateException("No FormatVersion found");
        }

        JsonNode major = version.get("Major");
        if (major == null || !major.isNumber()) {
            throw new IllegalStateException("No Major version found");
        }

        int majorVersion = major.asInt();

        switch (majorVersion) {
        case 1:
            return new ModelLoader(loadV1Model(tree, version));

        default:
            throw new IllegalStateException(
                    "Unknown major version " + majorVersion + ". Perhaps "
                    + "you need a newer version of the resources runtime?");
        }
    }
722155
public static void main(String[] args) throws Exception {
    StringUtils.printErrInvocationString("CRFClassifier", args);

    Properties props = StringUtils.argsToProperties(args);
    CRFClassifier<CoreLabel> crf = new CRFClassifier<CoreLabel>(props);
    String testFile = crf.flags.testFile;
    String textFile = crf.flags.textFile;
    String loadPath = crf.flags.loadClassifier;
    String loadTextPath = crf.flags.loadTextClassifier;
    String serializeTo = crf.flags.serializeTo;
    String serializeToText = crf.flags.serializeToText;

    if (loadPath != null) {
      crf.loadClassifierNoExceptions(loadPath, props);
    } else if (loadTextPath != null) {
      System.err.println("Warning: this is now only tested for Chinese Segmenter");
      System.err.println("(Sun Dec 23 00:59:39 2007) (pichuan)");
      try {
        crf.loadTextClassifier(loadTextPath, props);
        // System.err.println("DEBUG: out from crf.loadTextClassifier");
      } catch (Exception e) {
        throw new RuntimeException("error loading " + loadTextPath, e);
      }
    } else if (crf.flags.loadJarClassifier != null) {
      crf.loadJarClassifier(crf.flags.loadJarClassifier, props);
    } else if (crf.flags.trainFile != null || crf.flags.trainFileList != null) {
      crf.train();
    } else {
      crf.loadDefaultClassifier();
    }

    // System.err.println("Using " + crf.flags.featureFactory);
    // System.err.println("Using " +
    // StringUtils.getShortClassName(crf.readerAndWriter));

    if (serializeTo != null) {
      crf.serializeClassifier(serializeTo);
    }

    if (serializeToText != null) {
      crf.serializeTextClassifier(serializeToText);
    }

    if (testFile != null) {
      DocumentReaderAndWriter<CoreLabel> readerAndWriter = crf.makeReaderAndWriter();
      if (crf.flags.searchGraphPrefix != null) {
        crf.classifyAndWriteViterbiSearchGraph(testFile, crf.flags.searchGraphPrefix, crf.makeReaderAndWriter());
      } else if (crf.flags.printFirstOrderProbs) {
        crf.printFirstOrderProbs(testFile, readerAndWriter);
      } else if (crf.flags.printProbs) {
        crf.printProbs(testFile, readerAndWriter);
      } else if (crf.flags.useKBest) {
        int k = crf.flags.kBest;
        crf.classifyAndWriteAnswersKBest(testFile, k, readerAndWriter);
      } else if (crf.flags.printLabelValue) {
        crf.printLabelInformation(testFile, readerAndWriter);
      } else {
        crf.classifyAndWriteAnswers(testFile, readerAndWriter);
      }
    }

    if (textFile != null) {
      crf.classifyAndWriteAnswers(textFile);
    }

    if (crf.flags.readStdin) {
      crf.classifyStdin();
    }
  }
465329
private static Properties loadWithTomcatMode(final String propertyFilePath)
            throws Exception {

        Properties props = new Properties();

        try {

            // 先用TOMCAT模式进行导入
            // http://blog.csdn.net/minfree/article/details/1800311
            // http://stackoverflow.com/questions/3263560/sysloader-getresource-problem-in-java
            URL url = ClassLoaderUtil.getLoader().getResource(propertyFilePath);
            URI uri = new URI(url.toString());
            props.load(new FileInputStream(uri.getPath()));

        } catch (Exception e) {

            // http://stackoverflow.com/questions/574809/load-a-resource-contained-in-a-jar
            props.load(ClassLoaderUtil.getLoader().getResourceAsStream(propertyFilePath));
        }
        return props;
    }
160536
private function getModelsSchemaInfo(string $modelsPath): array
    {
        // check reverse relationships
        $requireReverseRel = true;

        $registered    = [];
        $modelSchemas  = new ModelSchemaInfo();
        $registerModel = function ($modelClass) use ($modelSchemas, &$registered, $requireReverseRel) {
            /** @var ModelInterface $modelClass */
            $modelSchemas->registerClass(
                $modelClass,
                $modelClass::getTableName(),
                $modelClass::getPrimaryKeyName(),
                $modelClass::getAttributeTypes(),
                $modelClass::getAttributeLengths(),
                $modelClass::getRawAttributes()
            );

            $relationships = $modelClass::getRelationships();

            if (array_key_exists(RelationshipTypes::BELONGS_TO, $relationships) === true) {
                foreach ($relationships[RelationshipTypes::BELONGS_TO] as $relName => list($rClass, $fKey, $rRel)) {
                    /** @var string $rClass */
                    $modelSchemas->registerBelongsToOneRelationship($modelClass, $relName, $fKey, $rClass, $rRel);
                    $registered[(string)$modelClass][$relName] = true;
                    $registered[$rClass][$rRel]                = true;

                    // Sanity check. Every `belongs_to` should be paired with `has_many` on the other side.
                    /** @var ModelInterface $rClass */
                    $rRelationships   = $rClass::getRelationships();
                    $isRelationshipOk = $requireReverseRel === false ||
                        (isset($rRelationships[RelationshipTypes::HAS_MANY][$rRel]) === true &&
                            $rRelationships[RelationshipTypes::HAS_MANY][$rRel] === [$modelClass, $fKey, $relName]);
                    /** @var string $modelClass */

                    assert($isRelationshipOk, "`belongsTo` relationship `$relName` of class $modelClass " .
                        "should be paired with `hasMany` relationship.");
                }
            }

            if (array_key_exists(RelationshipTypes::HAS_MANY, $relationships) === true) {
                foreach ($relationships[RelationshipTypes::HAS_MANY] as $relName => list($rClass, $fKey, $rRel)) {
                    // Sanity check. Every `has_many` should be paired with `belongs_to` on the other side.
                    /** @var ModelInterface $rClass */
                    $rRelationships   = $rClass::getRelationships();
                    $isRelationshipOk = $requireReverseRel === false ||
                        (isset($rRelationships[RelationshipTypes::BELONGS_TO][$rRel]) === true &&
                            $rRelationships[RelationshipTypes::BELONGS_TO][$rRel] === [$modelClass, $fKey, $relName]);
                    /** @var string $modelClass */
                    assert($isRelationshipOk, "`hasMany` relationship `$relName` of class $modelClass " .
                        "should be paired with `belongsTo` relationship.");
                }
            }

            if (array_key_exists(RelationshipTypes::BELONGS_TO_MANY, $relationships) === true) {
                foreach ($relationships[RelationshipTypes::BELONGS_TO_MANY] as $relName => $data) {
                    if (isset($registered[(string)$modelClass][$relName]) === true) {
                        continue;
                    }
                    /** @var string $rClass */
                    list($rClass, $iTable, $fKeyPrimary, $fKeySecondary, $rRel) = $data;
                    $modelSchemas->registerBelongsToManyRelationship(
                        $modelClass,
                        $relName,
                        $iTable,
                        $fKeyPrimary,
                        $fKeySecondary,
                        $rClass,
                        $rRel
                    );
                    $registered[(string)$modelClass][$relName] = true;
                    $registered[$rClass][$rRel]                = true;
                }
            }
        };

        /** @noinspection PhpParamsInspection */
        $modelClasses = iterator_to_array($this->selectClasses($modelsPath, ModelInterface::class));
        array_map($registerModel, $modelClasses);

        $data = $modelSchemas->getData();

        return $data;
    }
602054
public static MaxEntModel create(String path)
    {
        MaxEntModel m = new MaxEntModel();
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(IOUtil.newInputStream(path), "UTF-8"));
            DataOutputStream out = new DataOutputStream(IOUtil.newOutputStream(path + Predefine.BIN_EXT));
            br.readLine();  // type
            m.correctionConstant = Integer.parseInt(br.readLine());  // correctionConstant
            out.writeInt(m.correctionConstant);
            m.correctionParam = Double.parseDouble(br.readLine());  // getCorrectionParameter
            out.writeDouble(m.correctionParam);
            // label
            int numOutcomes = Integer.parseInt(br.readLine());
            out.writeInt(numOutcomes);
            String[] outcomeLabels = new String[numOutcomes];
            m.outcomeNames = outcomeLabels;
            for (int i = 0; i < numOutcomes; i++)
            {
                outcomeLabels[i] = br.readLine();
                TextUtility.writeString(outcomeLabels[i], out);
            }
            // pattern
            int numOCTypes = Integer.parseInt(br.readLine());
            out.writeInt(numOCTypes);
            int[][] outcomePatterns = new int[numOCTypes][];
            for (int i = 0; i < numOCTypes; i++)
            {
                StringTokenizer tok = new StringTokenizer(br.readLine(), " ");
                int[] infoInts = new int[tok.countTokens()];
                out.writeInt(infoInts.length);
                for (int j = 0; tok.hasMoreTokens(); j++)
                {
                    infoInts[j] = Integer.parseInt(tok.nextToken());
                    out.writeInt(infoInts[j]);
                }
                outcomePatterns[i] = infoInts;
            }
            // feature
            int NUM_PREDS = Integer.parseInt(br.readLine());
            out.writeInt(NUM_PREDS);
            String[] predLabels = new String[NUM_PREDS];
            m.pmap = new DoubleArrayTrie<Integer>();
            TreeMap<String, Integer> tmpMap = new TreeMap<String, Integer>();
            for (int i = 0; i < NUM_PREDS; i++)
            {
                predLabels[i] = br.readLine();
                assert !tmpMap.containsKey(predLabels[i]) : "重复的键： " + predLabels[i] + " 请使用 -Dfile.encoding=UTF-8 训练";
                TextUtility.writeString(predLabels[i], out);
                tmpMap.put(predLabels[i], i);
            }
            m.pmap.build(tmpMap);
            for (Map.Entry<String, Integer> entry : tmpMap.entrySet())
            {
                out.writeInt(entry.getValue());
            }
            m.pmap.save(out);
            // params
            Context[] params = new Context[NUM_PREDS];
            int pid = 0;
            for (int i = 0; i < outcomePatterns.length; i++)
            {
                int[] outcomePattern = new int[outcomePatterns[i].length - 1];
                for (int k = 1; k < outcomePatterns[i].length; k++)
                {
                    outcomePattern[k - 1] = outcomePatterns[i][k];
                }
                for (int j = 0; j < outcomePatterns[i][0]; j++)
                {
                    double[] contextParameters = new double[outcomePatterns[i].length - 1];
                    for (int k = 1; k < outcomePatterns[i].length; k++)
                    {
                        contextParameters[k - 1] = Double.parseDouble(br.readLine());
                        out.writeDouble(contextParameters[k - 1]);
                    }
                    params[pid] = new Context(outcomePattern, contextParameters);
                    pid++;
                }
            }
            // prior
            m.prior = new UniformPrior();
            m.prior.setLabels(outcomeLabels);
            // eval
            m.evalParams = new EvalParameters(params, m.correctionParam, m.correctionConstant, outcomeLabels.length);
            out.close();
        }
        catch (Exception e)
        {
            logger.severe("从" + path + "加载最大熵模型失败！" + TextUtility.exceptionToString(e));
            return null;
        }
        return m;
    }
427896
function InvalidModelError(boundPath, shortedPath) {
    var instance = new Error("The boundPath of the model is not valid since a value or error was found before the path end.");

    instance.name = "InvalidModelError";
    instance.boundPath = boundPath;
    instance.shortedPath = shortedPath;

    if (Object.setPrototypeOf) {
        Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
    }

    if (Error.captureStackTrace) {
        Error.captureStackTrace(instance, InvalidModelError);
    }

    return instance;
}
92218
public Object initModelIF(EventModel em, ModelForm form, HttpServletRequest request) throws Exception {
		Object result = null;
		try {
			HandlerMetaDef hm = this.modelMapping.getHandlerMetaDef();
			String serviceName = hm.getServiceRef();
			Debug.logVerbose("[JdonFramework] construct the CRUD method for the service:" + serviceName, module);
			MethodMetaArgs methodMetaArgs = maFactory.createinitMethod(hm, em);
			RequestWrapper requestW = new HttpServletRequestWrapper(request);
			Service service = serviceFacade.getService(requestW.getContextHolder().getAppContextHolder());
			if (methodMetaArgs != null)
				result = service.execute(serviceName, methodMetaArgs, requestW);
		} catch (Exception e) {
			Debug.logError("[JdonFramework] initModel error: " + e, module);
			throw new Exception(e);
		}
		return result;
	}
1069072
private void init(String xmlPath) throws MalformedURLException, IOException{
		
		XML xml = new XML(true, xmlPath);
		if(!xml.isInheritedMapped(configuredClass))
			Error.classNotMapped(configuredClass);
		
		for (Class<?> classe :getClasses(xml)){
			relationalManyToOneMapper.put(classe.getName(), new JMapper(configuredClass, classe,ChooseConfig.DESTINATION, xmlPath));
			relationalOneToManyMapper.put(classe.getName(), new JMapper(classe, configuredClass,ChooseConfig.SOURCE, xmlPath));
		}
	}
142983
private static Properties loadWithNormalMode(final String propertyFilePath)
            throws Exception {

        Properties props = new Properties();
        props.load(new InputStreamReader(new FileInputStream(propertyFilePath), "utf-8"));
        return props;
    }
191451
public static ConfigFactory
    webappConfigFactory(ServletContextPath servletContextPath) {

        checkNotNull(servletContextPath);

        return webappConfigFactory()
            .bind(ServletContextPath.class)
            .toInstance(servletContextPath);
    }
640542
public <C2> ColumnPath<C> append(C2 name, Serializer<C2> ser) {
        path.add(ByteBuffer.wrap(ser.toBytes(name)));
        return this;
    }
988330
private static void addRetrieveInternalModelId(CtClass clazz) throws NotFoundException,
        CannotCompileException {
        CtField modelIdField = null;
        CtClass temp = clazz;
        while (temp != null) {
            for (CtField field : temp.getDeclaredFields()) {
                if (JavassistUtils.hasAnnotation(field, OpenEngSBModelId.class.getName())) {
                    modelIdField = field;
                    break;
                }
            }
            temp = temp.getSuperclass();
        }
        CtClass[] params = generateClassField();
        CtMethod valueMethod = new CtMethod(cp.get(Object.class.getName()), "retrieveInternalModelId", params, clazz);
        StringBuilder builder = new StringBuilder();
        builder.append(createTrace("Called retrieveInternalModelId"));
        CtMethod idFieldGetter = getFieldGetter(modelIdField, clazz);
        if (modelIdField == null || idFieldGetter == null) {
            builder.append("return null;");
        } else {
            builder.append(String.format("return %s();", idFieldGetter.getName()));
        }
        valueMethod.setBody(createMethodBody(builder.toString()));
        clazz.addMethod(valueMethod);

        CtMethod nameMethod =
            new CtMethod(cp.get(String.class.getName()), "retrieveInternalModelIdName", generateClassField(), clazz);
        if (modelIdField == null) {
            nameMethod.setBody(createMethodBody("return null;"));
        } else {
            nameMethod.setBody(createMethodBody("return \"" + modelIdField.getName() + "\";"));
        }
        clazz.addMethod(nameMethod);
    }
1339867
public void addClasspath(Map<String, Object> conf, String classpath) {
    String cpKey = Config.TOPOLOGY_ADDITIONAL_CLASSPATH;

    if (conf.containsKey(cpKey)) {
      String newEntry = String.format("%s:%s", conf.get(cpKey), classpath);
      conf.put(cpKey, newEntry);
    } else {
      conf.put(cpKey, classpath);
    }
  }