@ -1,2 +1,17 @@
|
||||
# legal_counsel
|
||||
|
||||
# 系统简介:
|
||||
# 当前,法律服务存在效率低下、资源分散、普及率不高等问题,缺乏有效的工具来提升学习和工作效率。
|
||||
# 为了解决这些问题,我们利用人工智能、大数据等技术,提供一系列智能化法律服务工具,
|
||||
# 普及法律知识,促进法律知识的学习和研究,帮助公众更好地理解和运用法律,从而推动法律服务行业的现代化和法治社会的建设。
|
||||
# 该项目针对的主要用户群体包括法律专业人士、学生、研究人员以及普通公众。
|
||||
|
||||
# 配置环境:
|
||||
# VS Code + mySQL
|
||||
|
||||
# 成员:
|
||||
# 220340238 刘明耀
|
||||
# 220340222 张之阳
|
||||
# 220340223 开钰昊
|
||||
# 210340227 梅诗睿
|
||||
# 220340209 钱 浩
|
||||
|
Before Width: | Height: | Size: 936 KiB |
Before Width: | Height: | Size: 230 KiB |
Before Width: | Height: | Size: 260 KiB |
Before Width: | Height: | Size: 132 KiB |
Before Width: | Height: | Size: 142 KiB |
Before Width: | Height: | Size: 1.2 MiB |
After Width: | Height: | Size: 441 KiB |
@ -0,0 +1,179 @@
|
||||
// 聊天相关的状态
|
||||
let chatVisible = false;
|
||||
let chatMinimized = false;
|
||||
|
||||
// 定义JSON文件路径
|
||||
const jsonFiles = [
|
||||
'./json/legaladvice/legalAdvice1.json',
|
||||
'./json/legaladvice/legalAdvice2.json',
|
||||
'./json/legaladvice/legalAdvice3.json',
|
||||
'./json/legaladvice/legalAdvice4.json',
|
||||
'./json/legaladvice/legalAdvice5.json',
|
||||
'./json/legaladvice/legalAdvice6.json',
|
||||
'./json/legaladvice/legalAdvice7.json',
|
||||
'./json/legaladvice/legalAdvice8.json'
|
||||
];
|
||||
|
||||
// 启动聊天
|
||||
function startLegalChat() {
|
||||
const input = document.getElementById('legal-search');
|
||||
const question = input.value.trim();
|
||||
|
||||
if (!question) {
|
||||
alert('请输入您的问题');
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示聊天容器
|
||||
const chatContainer = document.getElementById('chat-container');
|
||||
chatContainer.style.display = 'block';
|
||||
chatVisible = true;
|
||||
|
||||
// 添加用户消息
|
||||
addMessage(question, 'user');
|
||||
|
||||
// 使用数据集生成回复
|
||||
searchLegalAdvice(question);
|
||||
|
||||
// 清空搜索框
|
||||
input.value = '';
|
||||
}
|
||||
|
||||
// 发送新消息
|
||||
function sendChatMessage() {
|
||||
const input = document.getElementById('chat-input');
|
||||
const message = input.value.trim();
|
||||
|
||||
if (!message) return;
|
||||
|
||||
addMessage(message, 'user');
|
||||
searchLegalAdvice(message);
|
||||
|
||||
input.value = '';
|
||||
}
|
||||
|
||||
// 添加消息到对话框
|
||||
function addMessage(text, type) {
|
||||
const messagesContainer = document.getElementById('chat-messages');
|
||||
const messageDiv = document.createElement('div');
|
||||
messageDiv.className = `message ${type}-message`;
|
||||
messageDiv.textContent = text;
|
||||
messagesContainer.appendChild(messageDiv);
|
||||
|
||||
// 滚动到最新消息
|
||||
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
||||
}
|
||||
|
||||
// 搜索法律建议
|
||||
async function searchLegalAdvice(question) {
|
||||
// 显示加载状态
|
||||
addMessage('正在查找相关法律建议...', 'bot');
|
||||
|
||||
try {
|
||||
// 并行获取所有JSON文件
|
||||
const responses = await Promise.all(jsonFiles.map(url => fetch(url)));
|
||||
const dataArray = await Promise.all(responses.map(response => response.json()));
|
||||
|
||||
// 合并所有数据
|
||||
const combinedData = dataArray.reduce((acc, curr) => acc.concat(curr), []);
|
||||
|
||||
// 移除加载消息
|
||||
const messagesContainer = document.getElementById('chat-messages');
|
||||
messagesContainer.removeChild(messagesContainer.lastChild);
|
||||
|
||||
// 处理搜索结果
|
||||
const result = processQuery(combinedData, question);
|
||||
addMessage(result, 'bot');
|
||||
|
||||
} catch (error) {
|
||||
console.error('获取法律建议时出错:', error);
|
||||
addMessage('抱歉,获取法律建议时出现错误,请稍后再试。', 'bot');
|
||||
}
|
||||
}
|
||||
|
||||
// 处理查询结果
|
||||
function processQuery(data, searchTerm) {
|
||||
let foundAnswer = null;
|
||||
let closestMatch = null;
|
||||
let closestDistance = Infinity;
|
||||
|
||||
const searchWords = searchTerm.split(/\s+/).filter(word => word.length > 0);
|
||||
|
||||
data.forEach(item => {
|
||||
const question = item.question.toLowerCase();
|
||||
const isMatch = searchWords.every(word => question.includes(word.toLowerCase()));
|
||||
|
||||
if (isMatch) {
|
||||
foundAnswer = item.answers[0];
|
||||
} else {
|
||||
const distance = getLevenshteinDistance(searchTerm.toLowerCase(), question);
|
||||
if (distance < closestDistance) {
|
||||
closestDistance = distance;
|
||||
closestMatch = item;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (foundAnswer) {
|
||||
return foundAnswer;
|
||||
} else if (closestMatch) {
|
||||
const similarityScore = calculateSimilarity(searchTerm.toLowerCase(), closestMatch.question.toLowerCase());
|
||||
return similarityScore < 0.2 ?
|
||||
'抱歉,我没有找到完全匹配的答案。您能否更详细地描述您的问题?' :
|
||||
closestMatch.answers[0];
|
||||
} else {
|
||||
return '抱歉,我没有找到相关的法律建议。请尝试用不同的方式描述您的问题。';
|
||||
}
|
||||
}
|
||||
|
||||
// Levenshtein距离计算
|
||||
function getLevenshteinDistance(a, b) {
|
||||
const matrix = [];
|
||||
for (let i = 0; i <= b.length; i++) {
|
||||
matrix[i] = [i];
|
||||
}
|
||||
for (let j = 0; j <= a.length; j++) {
|
||||
matrix[0][j] = j;
|
||||
}
|
||||
|
||||
for (let i = 1; i <= b.length; i++) {
|
||||
for (let j = 1; j <= a.length; j++) {
|
||||
if (b.charAt(i - 1) === a.charAt(j - 1)) {
|
||||
matrix[i][j] = matrix[i - 1][j - 1];
|
||||
} else {
|
||||
matrix[i][j] = Math.min(
|
||||
matrix[i - 1][j - 1] + 1,
|
||||
matrix[i][j - 1] + 1,
|
||||
matrix[i - 1][j] + 1
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return matrix[b.length][a.length];
|
||||
}
|
||||
|
||||
// 计算相似度
|
||||
function calculateSimilarity(a, b) {
|
||||
const distance = getLevenshteinDistance(a, b);
|
||||
const maxLength = Math.max(a.length, b.length);
|
||||
return maxLength ? (maxLength - distance) / maxLength : 1;
|
||||
}
|
||||
|
||||
// 切换聊天框显示状态
|
||||
function toggleChat() {
|
||||
const chatContainer = document.getElementById('chat-container');
|
||||
chatMinimized = !chatMinimized;
|
||||
chatContainer.classList.toggle('minimized');
|
||||
}
|
||||
|
||||
// 关闭聊天
|
||||
function closeChat() {
|
||||
const chatContainer = document.getElementById('chat-container');
|
||||
chatContainer.style.display = 'none';
|
||||
chatVisible = false;
|
||||
chatMinimized = false;
|
||||
chatContainer.classList.remove('minimized');
|
||||
|
||||
// 清空聊天记录
|
||||
document.getElementById('chat-messages').innerHTML = '';
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
"""Django's command-line utility for administrative tasks."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
"""Run administrative tasks."""
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chat.settings')
|
||||
try:
|
||||
from django.core.management import execute_from_command_line
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"Couldn't import Django. Are you sure it's installed and "
|
||||
"available on your PYTHONPATH environment variable? Did you "
|
||||
"forget to activate a virtual environment?"
|
||||
) from exc
|
||||
execute_from_command_line(sys.argv)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|