通过AI把图片的属性值提取整合到一起,通过JSON格式数据显示出来

使用Java程序通过AI将图片生成JSON格式数据,以下是实现映射操作的完整方案,以及如何将JSON结构有效传递给AI的方法。

一、系统架构设计

图片输入 → Java程序 → AI服务 → 原始JSON → 映射模块 → 标准化JSON
                      ↑
                结构提示/示例

二、Java实现映射操作的核心代码

1. 映射配置类

public class FieldMappingConfig {
    private String sourcePattern; // 支持正则表达式
    private String targetField;
    private double minConfidence;
    private String valueProcessor;
    
    // getters & setters
}

public class SchoolMappingProfile {
    private String schoolId;
    private List<FieldMappingConfig> fieldMappings;
    private Map<String, String> valueProcessors;
    
    // 从JSON加载配置
    public static SchoolMappingProfile fromJson(String json) {
        return new ObjectMapper().readValue(json, SchoolMappingProfile.class);
    }
}

2. 核心映射处理器

public class AIDataMapper {
    private Map<String, SchoolMappingProfile> schoolProfiles;
    
    public JsonNode mapToStandardFormat(JsonNode aiGeneratedJson, String schoolId) {
        SchoolMappingProfile profile = schoolProfiles.get(schoolId);
        if (profile == null) {
            throw new IllegalArgumentException("Unknown school profile: " + schoolId);
        }
        
        ObjectNode result = JsonNodeFactory.instance.objectNode();
        ObjectNode metadata = result.putObject("_metadata");
        
        Iterator<Map.Entry<String, JsonNode>> fields = aiGeneratedJson.fields();
        while (fields.hasNext()) {
            Map.Entry<String, JsonNode> entry = fields.next();
            processField(entry.getKey(), entry.getValue(), profile, result, metadata);
        }
        
        return result;
    }
    
    private void processField(String sourceKey, JsonNode value, 
                            SchoolMappingProfile profile,
                            ObjectNode result, ObjectNode metadata) {
        // 1. 尝试找到最佳匹配
        FieldMappingConfig bestMatch = findBestMatch(sourceKey, profile);
        
        // 2. 处理字段
        if (bestMatch != null) {
            String processedValue = processValue(value.asText(), bestMatch.getValueProcessor(), profile);
            result.put(bestMatch.getTargetField(), processedValue);
            
            // 记录元数据
            metadata.putObject(bestMatch.getTargetField())
                .put("sourceField", sourceKey)
                .put("mappingConfidence", bestMatch.getConfidence());
        } else {
            // 未匹配字段放入扩展区
            result.with("extras").set(sourceKey, value);
        }
    }
    
    private FieldMappingConfig findBestMatch(String sourceKey, SchoolMappingProfile profile) {
        FieldMappingConfig bestMatch = null;
        double maxScore = 0;
        
        for (FieldMappingConfig config : profile.getFieldMappings()) {
            double score = calculateSimilarity(sourceKey, config.getSourcePattern());
            if (score > config.getMinConfidence() && score > maxScore) {
                maxScore = score;
                bestMatch = config;
            }
        }
        
        return bestMatch;
    }
    
    private double calculateSimilarity(String source, String pattern) {
        // 实现字符串相似度算法
        return new LevenshteinDistance().apply(source, pattern);
    }
    
    private String processValue(String value, String processor, SchoolMappingProfile profile) {
        // 实现各种值处理器
        switch (processor) {
            case "trim": return value.trim();
            case "parseDouble": return String.valueOf(Double.parseDouble(value));
            // 其他处理器...
            default: return value;
        }
    }
}

三、如何将JSON结构传递给AI

方法1:结构化提示词(推荐)

public String generateAIPrompt(SchoolMappingProfile profile) {
    StringBuilder prompt = new StringBuilder();
    prompt.append("请从图片中提取以下结构化信息:\n");
    prompt.append("需要的JSON格式:\n");
    
    // 添加字段说明
    profile.getFieldMappings().forEach(config -> {
        prompt.append(String.format("- %s (%s): [描述期望的内容]\n", 
            config.getTargetField(), 
            config.getSourcePattern()));
    });
    
    prompt.append("\n请严格按照以下示例格式输出JSON:\n");
    prompt.append(generateExampleJson(profile));
    
    return prompt.toString();
}

private String generateExampleJson(SchoolMappingProfile profile) {
    ObjectNode example = JsonNodeFactory.instance.objectNode();
    profile.getFieldMappings().forEach(config -> {
        example.put(config.getTargetField(), "[示例值]");
    });
    return example.toString();
}

方法2:Few-shot示例法

public List<String> getFewShotExamples() {
    return Arrays.asList(
        "{ \"课程编号\": \"CS101\", \"课程名称\": \"计算机科学\" }",
        "{ \"course_code\": \"MATH201\", \"course_title\": \"高等数学\" }"
    );
}

方法3:JSON Schema法

public String generateJsonSchemaPrompt() {
    return """
        {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "type": "object",
          "properties": {
            "courseId": {
              "type": "string",
              "description": "课程唯一标识符"
            },
            "courseName": {
              "type": "string",
              "description": "课程全称"
            }
            // 其他字段...
          },
          "required": ["courseId", "courseName"]
        }
        """;
}

四、完整工作流程实现

public class AIDataProcessingPipeline {
    private final AIDataMapper mapper;
    private final AIClient aiClient;
    
    public JsonNode processImage(byte[] imageData, String schoolId) throws Exception {
        // 1. 获取AI提示
        SchoolMappingProfile profile = mapper.getProfile(schoolId);
        String prompt = generateAIPrompt(profile);
        
        // 2. 调用AI服务获取原始JSON
        String aiResponse = aiClient.analyzeImage(imageData, prompt);
        JsonNode rawJson = new ObjectMapper().readTree(aiResponse);
        
        // 3. 执行标准化映射
        return mapper.mapToStandardFormat(rawJson, schoolId);
    }
    
    // ...其他方法...
}

五、增强功能实现

1. 模糊匹配增强

public class EnhancedSimilarityCalculator {
    private static final Map<String, List<String>> SYNONYM_DICT = Map.of(
        "编号", Arrays.asList("ID", "代码", "标识"),
        "名称", Arrays.asList("标题", "name", "title")
    );
    
    public double enhancedSimilarity(String source, String target) {
        double baseScore = new JaroWinklerSimilarity().apply(source, target);
        
        // 同义词增强
        if (SYNONYM_DICT.containsKey(source)) {
            if (SYNONYM_DICT.get(source).contains(target)) {
                return Math.min(1.0, baseScore + 0.3);
            }
        }
        
        return baseScore;
    }
}

2. 自动映射建议

public List<FieldMappingSuggestion> suggestMappings(JsonNode aiJson, SchoolMappingProfile profile) {
    List<FieldMappingSuggestion> suggestions = new ArrayList<>();
    
    aiJson.fieldNames().forEachRemaining(fieldName -> {
        if (!profile.containsTargetFor(fieldName)) {
            FieldMappingConfig bestConfig = profile.getMappings().stream()
                .max(Comparator.comparingDouble(
                    config -> calculateSimilarity(fieldName, config.getSourcePattern())
                )
                .orElse(null);
            
            if (bestConfig != null) {
                suggestions.add(new FieldMappingSuggestion(
                    fieldName, 
                    bestConfig.getTargetField(),
                    calculateSimilarity(fieldName, bestConfig.getSourcePattern())
                ));
            }
        }
    });
    
    return suggestions;
}

六、配置示例

学校映射配置示例

{
  "schoolId": "Tsinghua",
  "fieldMappings": [
    {
      "sourcePattern": "课程.*编号",
      "targetField": "courseId",
      "minConfidence": 0.85,
      "valueProcessor": "trimUpperCase"
    },
    {
      "sourcePattern": "课程.*名称",
      "targetField": "courseName",
      "minConfidence": 0.78,
      "valueProcessor": "trim"
    }
  ],
  "valueProcessors": {
    "trimUpperCase": "value.trim().toUpperCase()",
    "trim": "value.trim()"
  }
}

AI提示词示例

你是一个专业的信息提取AI,请从图片中提取课程大纲信息。

需要提取的字段包括:
- courseId (课程编号/代码): 课程的唯一标识码
- courseName (课程名称/标题): 课程的全称
- credits (学分/学时): 课程的学分值

请严格按照以下JSON格式输出:
{
  "courseId": "CS101",
  "courseName": "计算机科学导论",
  "credits": 4.0
}

如果某些字段无法识别,请保留原始值放入"extras"字段。

七、模糊匹配映射JSON结构

{
  "version": "1.0",
  "description": "基于AI模糊匹配的学校大纲Key映射配置",
  "fuzzy_matching_rules": {
    "course_id": {
      "target_field": "courseId",
      "candidate_keys": ["课程编号", "course_code", "科目ID", "cid", "课程代码"],
      "matching_threshold": 0.85,
      "value_processor": "trim & uppercase"
    },
    "course_name": {
      "target_field": "courseName",
      "candidate_keys": ["课程名称", "course_title", "科目名", "name", "title"],
      "matching_threshold": 0.78,
      "value_processor": "trim"
    },
    "credits": {
      "target_field": "credits",
      "candidate_keys": ["学分", "credit_hours", "学时", "credits", "hours"],
      "matching_threshold": 0.8,
      "value_processor": "to_float"
    }
  },
  "school_specific_overrides": {
    "school_A": {
      "direct_mappings": {
        "开课单位": "department",
        "考核方式": "assessmentMethod"
      }
    },
    "school_B": {
      "direct_mappings": {
        "faculty": "department",
        "exam_type": "assessmentMethod"
      }
    }
  },
  "value_processors": {
    "trim": {
      "type": "string",
      "operation": "trim"
    },
    "to_float": {
      "type": "number",
      "operation": "parse_float"
    },
    "uppercase": {
      "type": "string",
      "operation": "to_upper_case"
    }
  },
  "fallback_strategy": {
    "unmatched_fields": "include_in_extras",
    "confidence_threshold": 0.7,
    "low_confidence_action": "flag_for_review"
  }
}

模糊匹配过程示例

假设有原始数据:

{
  "课程代码": "CS101",
  "科目名": "计算机科学导论",
  "学分": "4",
  "开课单位": "计算机学院"
}

AI模糊匹配过程:

  1. 对每个原始Key计算与候选Key的相似度:

    • "课程代码" ≈ "课程编号" (0.92), "course_code" (0.85), "课程代码" (1.0)

    • "科目名" ≈ "课程名称" (0.88), "course_title" (0.76), "科目名" (1.0)

    • "学分" ≈ "学分" (1.0), "credit_hours" (0.82)

  2. 应用匹配结果:

{
  "standard_output": {
    "courseId": "CS101",
    "courseName": "计算机科学导论",
    "credits": 4.0,
    "department": "计算机学院"
  },
  "matching_metadata": {
    "courseId": {
      "source_key": "课程代码",
      "matched_rule": "course_id",
      "confidence": 1.0
    },
    "courseName": {
      "source_key": "科目名",
      "matched_rule": "course_name",
      "confidence": 1.0
    },
    "credits": {
      "source_key": "学分",
      "matched_rule": "credits",
      "confidence": 1.0
    },
    "department": {
      "source_key": "开课单位",
      "matched_type": "direct_mapping",
      "confidence": 1.0
    }
  }
}

模糊匹配规则详解

1. 基本匹配规则

{
  "field_name": {
    "target_field": "标准字段名",
    "candidate_keys": ["可能的键名1", "键名2"],
    "matching_threshold": "最小相似度阈值(0-1)",
    "value_processor": "值处理器"
  }
}

2. 学校特定覆盖规则

{
  "school_specific_overrides": {
    "学校名称": {
      "direct_mappings": {
        "原始键名": "目标键名"
      },
      "priority": "高于通用规则"
    }
  }
}

3. 值处理器配置

{
  "value_processors": {
    "processor_name": {
      "type": "string/number/date",
      "operation": "处理操作",
      "params": {}
    }
  }
}

八、异常处理与验证

public class DataMappingExceptionHandler {
    public void validateMappedData(JsonNode mappedData) throws InvalidDataException {
        // 检查必填字段
        if (!mappedData.has("courseId")) {
            throw new InvalidDataException("Missing required field: courseId");
        }
        
        // 检查字段类型
        if (mappedData.get("credits") != null && !mappedData.get("credits").isNumber()) {
            throw new InvalidDataException("credits must be a number");
        }
    }
    
    public JsonNode handleLowConfidenceFields(JsonNode mappedData) {
        JsonNode metadata = mappedData.get("_metadata");
        List<String> lowConfidenceFields = new ArrayList<>();
        
        metadata.fields().forEachRemaining(entry -> {
            if (entry.getValue().get("confidence").asDouble() < 0.7) {
                lowConfidenceFields.add(entry.getKey());
            }
        });
        
        if (!lowConfidenceFields.isEmpty()) {
            ((ObjectNode)mappedData).put("_warnings", 
                "Low confidence mappings: " + String.join(", ", lowConfidenceFields));
        }
        
        return mappedData;
    }
}

这个方案提供了从AI生成JSON到标准化输出的完整处理流程,具有以下特点:

  1. 灵活的字段映射配置

  2. 智能的模糊匹配能力

  3. 完善的值处理机制

  4. 详细的元数据记录

  5. 强大的异常处理能力

您可以根据实际需求调整映射规则和相似度阈值,逐步优化匹配精度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值