以上是一份聊天记录,如你所见除了部分无关聊天外,充斥着关于物...
Author:匿名用户
2026/01/05 09:12
Description
用JavaScript编写物流数据提取函数,识别价格、时效、地点和货物参数,支持多平台聊天记录解析。
Tags
コーディング情報抽出コンテンツ生成
Content
```python
# 提取物流价格时效信息的JS代码
// 第一部分:基础数据提取函数
function extractLogisticsInfo(chatLog) {
const lines = chatLog.split('\n');
const results = [];
let currentEntry = {};
for (const line of lines) {
// 跳过空行和明显无关的消息
if (!line.trim() || line.includes('你好') || line.includes('谢谢') || line.includes('不客气')) {
continue;
}
// 匹配客服回复的价格信息 (示例模式)
const priceMatch = line.match(/(\d+\.?\d*)\s*(元|RMB|USD|¥)/);
// 匹配时效信息 (示例模式)
const timeMatch = line.match(/(\d+)\s*(天|小时|日|工作日)/);
// 匹配地点信息
const locationMatch = line.match(/(发往|到|至|from|to)\s*([\u4e00-\u9fa5A-Za-z]+)/);
// 匹配货物参数
const cargoMatch = line.match(/(\d+)\s*(plt|pcs|件|个|kg|公斤|立方|CBM)/i);
if (priceMatch) {
currentEntry.price = priceMatch[0];
}
if (timeMatch) {
currentEntry.deliveryTime = timeMatch[0];
}
if (locationMatch) {
currentEntry.destination = locationMatch[2];
}
if (cargoMatch) {
currentEntry.cargoParams = currentEntry.cargoParams || [];
currentEntry.cargoParams.push(cargoMatch[0]);
}
// 当检测到时间戳时,认为是新的一条记录开始
if (line.match(/\d+\/\d+\s+\d+:\d+:\d+/)) {
if (Object.keys(currentEntry).length > 0) {
results.push(currentEntry);
currentEntry = {};
}
}
}
// 添加最后一条记录
if (Object.keys(currentEntry).length > 0) {
results.push(currentEntry);
}
return results;
}
// 第二部分:时效分析增强函数
function analyzeDeliveryTime(chatLog) {
const timePattern = /(\d+\/\d+\s+\d+:\d+:\d+).*?(今天提|预计|交货|送达).*?(\d+\/\d+)/g;
const timeMatches = [...chatLog.matchAll(timePattern)];
const timeAnalysis = [];
for (const match of timeMatches) {
const startDate = new Date(match[1]);
const endDate = new Date(match[3]);
const days = Math.ceil((endDate - startDate) / (1000 * 60 * 60 * 24));
timeAnalysis.push({
startDate: match[1],
endDate: match[3],
daysRequired: days
});
}
return timeAnalysis;
}
// 第三部分:主处理函数
function processChatLog(chatLog) {
const logisticsInfo = extractLogisticsInfo(chatLog);
const timeAnalysis = analyzeDeliveryTime(chatLog);
// 合并时效分析结果
logisticsInfo.forEach((entry, index) => {
if (timeAnalysis[index]) {
entry.actualDeliveryDays = timeAnalysis[index].daysRequired;
}
});
return logisticsInfo;
}
// 使用示例
// const cleanedData = processChatLog(yourChatLogText);
// console.log(cleanedData);
```