Classify complex text using DatumBox
Code: "dbox_text_classify.py". Programming language: Python DMelt Version 2.2. Last modified: 02/19/2018. License: Pro
https://datamelt.org/code/cache/dbox_text_classify_5632.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


"""
     * Example of how to classify text. 
     * - datumbox.configuration.properties: It defines for the default storage engine (required)
     * - datumbox.concurrencyconfiguration.properties: It controls the concurrency levels (required)
     * - datumbox.inmemoryconfiguration.properties: It contains the configurations for the InMemory storage engine (required)
     * - datumbox.mapdbconfiguration.properties: It contains the configurations for the MapDB storage engine (optional)
     * - logback.xml: It contains the configuration file for the logger (optional)
"""

print "Get data .." 
from jhplot import *
http="http://datamelt.org/examples/data/"
print Web.get(http+"rt-polarity.pos")
print Web.get(http+"rt-polarity.neg")

from com.datumbox.framework.common import Configuration
from  com.datumbox.framework.applications.nlp import TextClassifier
from com.datumbox.framework.core.common.dataobjects import Record
from com.datumbox.framework.common.utilities import RandomGenerator
from com.datumbox.framework.core.machinelearning import MLBuilder
from com.datumbox.framework.core.machinelearning.classification import MultinomialNaiveBayes
from com.datumbox.framework.core.machinelearning.featureselection import ChisquareSelect
from com.datumbox.framework.core.machinelearning.modelselection.metrics import ClassificationMetrics
from com.datumbox.framework.core.common.text.extractors import NgramsExtractor
from java.io import *
from java.util import *
from java.net import *

print "Create some configuration files.."
c=open("datumbox.configuration.default.properties","w")
s = """
    # The full package name of the Storage Engine. This determines the default storage engine:
    configuration.storageConfiguration=com.datumbox.framework.storage.inmemory.InMemoryConfiguration
    """
c.write(s)
c.close()

c=open("datumbox.concurrencyconfiguration.default.properties","w")
s = """
    # Whether the concurrent execution of tasks is allowed (options: true/false):
    concurrencyConfiguration.parallelized=true
    # The maximum number of Threads that can be executed concurrently for each task: 
    #   - Use 0 for setting it equal to the number of CPUs on the system.
    #   - Use 1 to turn off concurrency (same as concurrencyConfiguration.parallelized=false).
    #   - Any other positive value acts as a limit on the concurrency level, provided that the concurrencyConfiguration.parallelized=true.
    concurrencyConfiguration.maxNumberOfThreadsPerTask=0
    """
c.write(s)
c.close()

c=open("datumbox.inmemoryconfiguration.default.properties","w")
s="""# The relative or absolute path for the directory where the models are stored (if not specified the temporary directory is used):
    inMemoryConfiguration.directory=
  """
c.write(s)
c.close()

print "Initialization.." 
RandomGenerator.setGlobalSeed(42L)               # optionally set a specific seed for all Random objects
configuration = Configuration.getConfiguration() # default configuration based on properties file
#configuration.setStorageConfiguration(InMemoryConfiguration())
# configuration.setStorageConfiguration(new InMemoryConfiguration()) //use In-Memory engine (default)
# configuration.setStorageConfiguration(new MapDBConfiguration()) //use MapDB engine
# configuration.getConcurrencyConfiguration().setParallelized(true) //turn on/off the parallelization
# configuration.getConcurrencyConfiguration().setMaxNumberOfThreadsPerTask(4) //set the concurrency level
        
print "Reading Data .."
datasets = HashMap() # The examples of each category are stored on the same file, one example per row.
datasets.put("positive", File("rt-polarity.pos").toURI())
datasets.put("negative", File("rt-polarity.neg").toURI())

# Setup Training Parameters
trainingParameters = TextClassifier.TrainingParameters()
# numerical scaling configuration
trainingParameters.setNumericalScalerTrainingParameters(None)
        
# Set feature selection configuration
trainingParameters.setFeatureSelectorTrainingParametersList(Arrays.asList(ChisquareSelect.TrainingParameters()))
        
# Set text extraction configuration
trainingParameters.setTextExtractorParameters(NgramsExtractor.Parameters())

# Classifier configuration
trainingParameters.setModelerTrainingParameters(MultinomialNaiveBayes.TrainingParameters())

#Fit the classifier
textClassifier = MLBuilder.create(trainingParameters, configuration)
textClassifier.fit(datasets)
textClassifier.save("SentimentAnalysis")


# Use the classifier. Get validation metrics on the dataset
vm = textClassifier.validate(datasets)
        
# Classify a single sentence
sentence = "Datumbox is amazing!"
r = textClassifier.predict(sentence)

print ("Results:")
print ("Classifing sentence: \""+sentence+"\"")
print("Predicted class: ",r.getYPredicted())
print("Probability: ",r.getYPredictedProbabilities().get(r.getYPredicted()))
print("Classifier Accuracy: ",vm.getAccuracy())

textClassifier.delete() 



You see the box below because you did not login.