Java Code Examples for com.jstarcraft.ai.data.DataSpace#getQuantityAttributes()

The following examples show how to use com.jstarcraft.ai.data.DataSpace#getQuantityAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MovieDataConfigurer.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
/**
 * 装配数据模型
 * 
 * @param movieDataSpace
 * @return
 */
@Bean("movieDataModule")
DataModule getMovieDataModule(DataSpace movieDataSpace, List<MovieUser> movieUsers, List<MovieItem> movieItems) throws Exception {
    TreeMap<Integer, String> configuration = new TreeMap<>();
    configuration.put(1, "user");
    configuration.put(2, "item");
    configuration.put(3, "score");
    configuration.put(4, "instant");
    DataModule dataModule = movieDataSpace.makeDenseModule("score", configuration, 1000000);

    File file = new File("data/ml-100k/u.data");
    CSVFormat format = CSVFormat.DEFAULT.withDelimiter('\t');
    DataConverter<InputStream> convertor = new CsvConverter(format, movieDataSpace.getQualityAttributes(), movieDataSpace.getQuantityAttributes());
    try (InputStream stream = new FileInputStream(file)) {
        convertor.convert(dataModule, stream);
    }

    int userDimension = dataModule.getQualityInner("user");
    int itemDimension = dataModule.getQualityInner("item");
    int scoreDimension = dataModule.getQuantityInner("score");
    for (DataInstance instance : dataModule) {
        int userIndex = instance.getQualityFeature(userDimension);
        int itemIndex = instance.getQualityFeature(itemDimension);
        instance.setQuantityMark(instance.getQuantityFeature(scoreDimension));
        movieUsers.get(userIndex).click(itemIndex);
    }

    return dataModule;
}
 
Example 2
Source File: YongfengZhangDatasetTestCase.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Test
public void testDataset() throws Exception {
    File file = new File("data/labeled_DC/DC_feature_opinion/DC.txt");

    // 定义数据空间
    Map<String, Class<?>> qualityDifinitions = new HashMap<>();
    qualityDifinitions.put("user", String.class);
    qualityDifinitions.put("item", String.class);
    qualityDifinitions.put("word", String.class);
    Map<String, Class<?>> quantityDifinitions = new HashMap<>();
    quantityDifinitions.put("score", Float.class);
    quantityDifinitions.put("sentiment", Float.class);
    DataSpace dataSpace = new DataSpace(qualityDifinitions, quantityDifinitions);

    // 处理数据属性
    try (InputStream stream = new FileInputStream(file)) {
        InputSource xmlSource = new InputSource(stream);
        SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxFactory.newSAXParser();
        XMLReader sheetParser = saxParser.getXMLReader();
        YongfengZhangAttributeHandler handler = new YongfengZhangAttributeHandler(dataSpace);
        sheetParser.setContentHandler(handler);
        sheetParser.parse(xmlSource);
    }
    QualityAttribute<String> userAttribute = dataSpace.getQualityAttribute("user");
    QualityAttribute<String> itemAttribute = dataSpace.getQualityAttribute("item");
    QualityAttribute<String> wordAttribute = dataSpace.getQualityAttribute("word");
    Assert.assertEquals(89373, userAttribute.getSize());
    Assert.assertEquals(2397, itemAttribute.getSize());
    Assert.assertEquals(333, wordAttribute.getSize());

    // 定义数据模块
    // 使用word属性大小作为sentiment特征维度
    TreeMap<Integer, String> configuration = new TreeMap<>();
    configuration.put(1, "user");
    configuration.put(2, "item");
    configuration.put(3, "score");
    configuration.put(3 + wordAttribute.getSize(), "sentiment");
    DataModule dataModule = dataSpace.makeSparseModule("score", configuration, 1000000);

    // 处理数据实例
    DataConverter<InputStream> convertor = new YongfengZhangDatasetConverter(wordAttribute, dataSpace.getQualityAttributes(), dataSpace.getQuantityAttributes());
    try (InputStream stream = new FileInputStream(file)) {
        convertor.convert(dataModule, stream);
    }
    Assert.assertEquals(123732, dataModule.getSize());
}