com.jstarcraft.ai.data.attribute.QualityAttribute Java Examples

The following examples show how to use com.jstarcraft.ai.data.attribute.QualityAttribute. 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
@Bean("movieItems")
List<MovieItem> getItems(DataSpace movieDataSpace) throws Exception {
    File movieItemFile = new File("data/ml-100k/u.item");
    List<MovieItem> items = new LinkedList<>();

    QualityAttribute<Integer> itemAttribute = movieDataSpace.getQualityAttribute("item");
    try (InputStream stream = new FileInputStream(movieItemFile); InputStreamReader reader = new InputStreamReader(stream, StringUtility.CHARSET); BufferedReader buffer = new BufferedReader(reader)) {
        try (CSVParser parser = new CSVParser(buffer, CSVFormat.newFormat('|'))) {
            Iterator<CSVRecord> iterator = parser.iterator();
            while (iterator.hasNext()) {
                CSVRecord datas = iterator.next();
                // 物品标识
                int id = Integer.parseInt(datas.get(0));
                // 物品索引
                int index = itemAttribute.convertData(id);
                // 物品标题
                String title = datas.get(1);
                // 物品日期
                LocalDate date = StringUtility.isEmpty(datas.get(2)) ? LocalDate.of(1970, 1, 1) : LocalDate.parse(datas.get(2), formatter);
                MovieItem item = new MovieItem(index, title, date);
                items.add(item);
            }
        }
    }

    items = new ArrayList<>(items);
    return items;
}
 
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());
}
 
Example #3
Source File: YongfengZhangDatasetConverter.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
public YongfengZhangDatasetConverter(QualityAttribute<String> wordAttribute, Collection<QualityAttribute> qualityAttributes, Collection<QuantityAttribute> quantityAttributes) {
    super(qualityAttributes, quantityAttributes);
    this.wordAttribute = wordAttribute;
}
 
Example #4
Source File: StreamConverter.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
protected StreamConverter(Collection<QualityAttribute> qualityAttributes, Collection<QuantityAttribute> quantityAttributes) {
    super(qualityAttributes, quantityAttributes);
}
 
Example #5
Source File: JsonConverter.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
public JsonConverter(Collection<QualityAttribute> qualityAttributes, Collection<QuantityAttribute> quantityAttributes) {
    super(qualityAttributes, quantityAttributes);
}
 
Example #6
Source File: CsvConverter.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
public CsvConverter(CSVFormat format, Collection<QualityAttribute> qualityAttributes, Collection<QuantityAttribute> quantityAttributes) {
    super(qualityAttributes, quantityAttributes);
    this.format = format;
}
 
Example #7
Source File: ParquetConverter.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
public ParquetConverter(Collection<QualityAttribute> qualityAttributes, Collection<QuantityAttribute> quantityAttributes) {
    super(qualityAttributes, quantityAttributes);
}
 
Example #8
Source File: QueryConverter.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
protected QueryConverter(Collection<QualityAttribute> qualityAttributes, Collection<QuantityAttribute> quantityAttributes) {
    super(qualityAttributes, quantityAttributes);
}
 
Example #9
Source File: ArffConverter.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
public ArffConverter(Collection<QualityAttribute> qualityAttributes, Collection<QuantityAttribute> quantityAttributes) {
    super(CSVFormat.DEFAULT, qualityAttributes, quantityAttributes);
}
 
Example #10
Source File: DataSpace.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
public QualityAttribute getQualityAttribute(String attributeName) {
    return qualityAttributes.get(attributeName);
}
 
Example #11
Source File: DataSpace.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
public Collection<QualityAttribute> getQualityAttributes() {
    return qualityAttributes.values();
}
 
Example #12
Source File: NaiveBayesianClassifier.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
public NaiveBayesianClassifier(QualityAttribute attribute) {
    this.attribute = attribute;
}