當前位置: 首頁>>代碼示例>>Python>>正文


Python IdSet.write方法代碼示例

本文整理匯總了Python中Core.IdSet.IdSet.write方法的典型用法代碼示例。如果您正苦於以下問題:Python IdSet.write方法的具體用法?Python IdSet.write怎麽用?Python IdSet.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Core.IdSet.IdSet的用法示例。


在下文中一共展示了IdSet.write方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: ids

# 需要導入模塊: from Core.IdSet import IdSet [as 別名]
# 或者: from Core.IdSet.IdSet import write [as 別名]
class ExampleBuilder:
    structureAnalyzer = None
    """ 
    ExampleBuilder is the abstract base class for specialized example builders.
    Example builders take some data and convert it to examples usable by e.g. SVMs.
    An example builder writes three files, an example-file (in extended Joachim's
    SVM format) and .class_names and .feature_names files, which contain the names
    for the class and feature id-numbers. An example builder can also be given
    pre-existing sets of class and feature ids (optionally in files) so that the
    generated examples are consistent with other, previously generated examples.
    """    
    def __init__(self, classSet=None, featureSet=None):
        if(type(classSet) == types.StringType):
            self.classSet = IdSet(filename=classSet)
        else:
            self.classSet = classSet
        
        if(type(featureSet) == types.StringType):
            self.featureSet = IdSet(filename=featureSet)
        else:
            self.featureSet = featureSet
        
        self.featureTag = ""      
        self.exampleStats = ExampleStats()
        self.parse = None
        self.tokenization = None
        #self.idFileTag = None
        self.classIdFilename = None
        self.featureIdFilename = None
        
        self.styles = None
        self._defaultParameters = None
        self._parameterValueLimits = None
        self._setDefaultParameters(["sentenceLimit"])
        self.debug = False
    
    def _setDefaultParameters(self, defaults=None, valueLimits=None):
        # Initialize
        if self._defaultParameters == None:
            self._defaultParameters = {}
        if self._parameterValueLimits == None:
            self._parameterValueLimits = {}
        newParameters = Utils.Parameters.get({}, defaults, valueLimits=valueLimits)
        self._defaultParameters.update(newParameters)
        if valueLimits != None:
            self._parameterValueLimits.update(valueLimits)
    
    def getParameters(self, parameters):
        return Utils.Parameters.get(parameters, defaults=self._defaultParameters, valueLimits=self._parameterValueLimits)
    
    def setFeature(self, name, value):
        self.features[self.featureSet.getId(self.featureTag+name)] = value
    
    def getElementCounts(self, filename):
        print >> sys.stderr, "Counting elements:",
        if filename.endswith(".gz"):
            f = gzip.open(filename, "rt")
        else:
            f = open(filename, "rt")
        counts = {"documents":0, "sentences":0}
        for line in f:
            if "<document" in line:
                counts["documents"] += 1
            elif "<sentence" in line:
                counts["sentences"] += 1
        f.close()
        print >> sys.stderr, counts
        return counts

    def saveIds(self):
        if self.classIdFilename != None:
            print >> sys.stderr, "Saving class names to", self.classIdFilename
            self.classSet.write(self.classIdFilename)
        else:
            print >> sys.stderr, "Class names not saved"
        if self.featureIdFilename != None:
            print >> sys.stderr, "Saving feature names to", self.featureIdFilename
            self.featureSet.write(self.featureIdFilename)
        else:
            print >> sys.stderr, "Feature names not saved"

    def processCorpus(self, input, output, gold=None, append=False, allowNewIds=True, structureAnalyzer=None):
        # Create intermediate paths if needed
        if os.path.dirname(output) != "" and not os.path.exists(os.path.dirname(output)):
            os.makedirs(os.path.dirname(output))
        # Open output file
        openStyle = "wt"
        if append:
            #print "Appending examples"
            openStyle = "at"
        if output.endswith(".gz"):
            outfile = gzip.open(output, openStyle)
        else:
            outfile = open(output, openStyle)
        
        # Build examples
        self.exampleCount = 0
        if type(input) in types.StringTypes:
            self.elementCounts = self.getElementCounts(input)
            if self.elementCounts["sentences"] > 0:
#.........這裏部分代碼省略.........
開發者ID:DUT-LiuYang,項目名稱:TEES,代碼行數:103,代碼來源:ExampleBuilder.py

示例2: OptionParser

# 需要導入模塊: from Core.IdSet import IdSet [as 別名]
# 或者: from Core.IdSet.IdSet import write [as 別名]
            ExampleUtils.appendExamples(exampleCache, outFile)
            exampleCache = []
    ExampleUtils.appendExamples(exampleCache, outFile)
    outFile.close()
    print "Added", addCount, "polynomial features"

if __name__=="__main__":
    # Import Psyco if available
    try:
        import psyco
        psyco.full()
        print >> sys.stderr, "Found Psyco, using"
    except ImportError:
        print >> sys.stderr, "Psyco not installed"
        
    from optparse import OptionParser # For using command line options
    optparser = OptionParser()
    optparser.add_option("-i", "--ids", default=None, dest="ids", help="")
    optparser.add_option("-e", "--examples", default=None, dest="examples", help="")
    optparser.add_option("-j", "--idOutput", default=None, dest="idOutput", help="")
    optparser.add_option("-o", "--output", default=None, dest="output", help="")
    optparser.add_option("-w", "--weights", default=None, dest="weights", help="")
    optparser.add_option("-c", "--cutoff", type="float", default=10.0, dest="cutoff", help="")
    (options, args) = optparser.parse_args()

    #classIds = IdSet(filename="/usr/share/biotext/GeniaChallenge/extension-data/genia/edge-examples/genia-edge-ids.class_names")
    featureIds = IdSet(filename=options.ids)
    weightFeatures = readWeights(options.weights, options.cutoff)
    polynomizeExamples(options.examples, options.output, weightFeatures, featureIds)
    featureIds.write(options.idOutput)
開發者ID:jbjorne,項目名稱:Tdevel,代碼行數:32,代碼來源:SVMMultiClassPolynomizeExamples.py

示例3: IdSet

# 需要導入模塊: from Core.IdSet import IdSet [as 別名]
# 或者: from Core.IdSet.IdSet import write [as 別名]
       "Localization", 
       "Negative_regulation", 
       "Phosphorylation", 
       "Positive_regulation", 
       "Protein_catabolism", 
       "Regulation", 
       "Transcription"]

classSet = IdSet(filename=TRIGGER_IDS+".class_names")
for triggerClass in triggerClasses:
    makeOneClassExamples(TRIGGER_TRAIN_EXAMPLE_FILE, TRIGGER_TRAIN_EXAMPLE_FILE + "-" + triggerClass, triggerClass, classSet)
    makeOneClassExamples(TRIGGER_TEST_EXAMPLE_FILE, TRIGGER_TEST_EXAMPLE_FILE + "-" + triggerClass, triggerClass, classSet)
    d = {"neg":1, triggerClass:classSet.getId(triggerClass, False)}
    triggerClassIds = IdSet(idDict = d)
    TRIGGER_CLASS_IDS = "trigger-ids-"+triggerClass+".class_names"
    triggerClassIds.write(TRIGGER_CLASS_IDS)

    print >> sys.stderr, "Trigger models for parse", PARSE_TAG, "for class", triggerClass
    TRIGGER_CLASSIFIER_PARAMS="c:" + options.triggerParams
    if "local" not in options.csc:
        clear = False
        if "clear" in options.csc: clear = True
        if "louhi" in options.csc:
            c = CSCConnection(CSC_WORKDIR+"/trigger-models-"+triggerClass, "[email protected]", clear)
        else:
            c = CSCConnection(CSC_WORKDIR+"/trigger-models-"+triggerClass, "[email protected]", clear)
    else:
        c = None
    bestTriggerModel = optimize(CLASSIFIER, Ev, TRIGGER_TRAIN_EXAMPLE_FILE+"-"+triggerClass, TRIGGER_TEST_EXAMPLE_FILE+"-"+triggerClass,\
        TRIGGER_CLASS_IDS, TRIGGER_CLASSIFIER_PARAMS, "trigger-models-"+triggerClass, None, c, False)[1]
開發者ID:jbjorne,項目名稱:Tdevel,代碼行數:32,代碼來源:TriggersWithMultipleClassifiers.py


注:本文中的Core.IdSet.IdSet.write方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。