本文目录导读:

模糊粗糙规划的几种方法。
Python实现方案
基础模糊匹配版本
import os
import re
from pathlib import Path
def fuzzy_file_organizer(base_path, rules):
"""
模糊粗糙规划文件
rules: 规则字典 {目标文件夹: [匹配关键词列表]}
"""
path = Path(base_path)
for file_path in path.iterdir():
if file_path.is_file():
# 读取文件内容
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
except:
continue
# 模糊匹配规则
for dest_folder, keywords in rules.items():
if fuzzy_match(content, keywords):
# 创建目标目录
dest_path = path / dest_folder
dest_path.mkdir(exist_ok=True)
# 移动文件
new_path = dest_path / file_path.name
file_path.rename(new_path)
print(f"移动: {file_path.name} -> {dest_folder}/")
break
def fuzzy_match(text, keywords, threshold=0.3):
"""简单的模糊匹配"""
if not keywords:
return False
text_lower = text.lower()
match_count = 0
for keyword in keywords:
if keyword.lower() in text_lower:
match_count += 1
# 匹配比例达到阈值即认为匹配
return match_count / len(keywords) >= threshold
使用模糊匹配库
from fuzzywuzzy import fuzz
from pathlib import Path
import shutil
def advanced_fuzzy_organizer(source_dir, rules_config):
"""
高级模糊匹配文件管理
rules_config: {目标文件夹: (主关键词, 相似度阈值)}
"""
source = Path(source_dir)
for file_path in source.glob('*'):
if file_path.is_file():
content = read_file_content(file_path)
best_match = None
best_score = 0
for dest_folder, (keywords, threshold) in rules_config.items():
score = calculate_fuzzy_score(content, keywords)
if score > best_score and score >= threshold:
best_score = score
best_match = dest_folder
if best_match:
organize_file(file_path, source / best_match)
def calculate_fuzzy_score(text, keywords):
"""计算模糊匹配分数"""
total_score = 0
for keyword in keywords:
# 使用部分匹配
score = fuzz.partial_ratio(keyword.lower(), text.lower())
total_score += score
return total_score / len(keywords) if keywords else 0
def read_file_content(file_path):
"""安全读取文件内容"""
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
return f.read()
except:
return ""
Shell脚本实现
Bash版本
#!/bin/bash
模糊分类脚本
ORGANIZE_DIR="$1"
: ${ORGANIZE_DIR:="./"}
# 定义规则
declare -A RULES
RULES["reports"]="报告|报表|统计"
RULES["logs"]="日志|log|exception"
RULES["data"]="数据|data|records"
RULES["notes"]="笔记|备忘|note"
fuzzy_classify() {
local file=$1
local content=$(cat "$file" 2>/dev/null | head -100) # 只读取前100行
for folder in "${!RULES[@]}"; do
local pattern="${RULES[$folder]}"
# 使用模糊匹配检查
if echo "$content" | grep -Eiq "$pattern"; then
mkdir -p "$ORGANIZE_DIR/$folder"
mv "$file" "$ORGANIZE_DIR/$folder/"
echo "已分类: $file -> $folder/"
return 0
fi
done
echo "未分类: $file"
}
# 遍历文件
for file in "$ORGANIZE_DIR"/*; do
[ -f "$file" ] && fuzzy_classify "$file"
done
完整实用脚本
#!/usr/bin/env python3
"""模糊粗糙规划工具
支持多种匹配策略和自动分类
"""
import os
import re
import hashlib
from pathlib import Path
from collections import Counter
import argparse
import json
class FuzzyOrganizer:
def __init__(self, config_file=None):
self.config = self.load_config(config_file) if config_file else {}
self.statistics = Counter()
def load_config(self, config_file):
"""加载配置文件"""
with open(config_file, 'r', encoding='utf-8') as f:
return json.load(f)
def extract_features(self, content):
"""提取内容特征"""
features = {
'words': self.extract_keywords(content),
'patterns': self.detect_patterns(content),
'length': len(content),
'line_count': content.count('\n')
}
return features
def extract_keywords(self, content, top_n=10):
"""提取关键词"""
words = re.findall(r'\w+', content.lower())
word_counts = Counter(words)
return [word for word, _ in word_counts.most_common(top_n)]
def detect_patterns(self, content):
"""检测内容模式"""
patterns = []
if re.search(r'<html|<div|<script', content, re.I):
patterns.append('html')
if re.search(r'def |class |import ', content):
patterns.append('code')
if re.search(r'日期|时间|月份', content):
patterns.append('datetime')
return patterns
def fuzzy_classify(self, file_path):
"""模糊分类文件"""
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read(10000) # 只读取前1万个字符
except:
return None
features = self.extract_features(content)
# 检查规则匹配
best_category = None
best_score = 0
for category, rules in self.config.items():
score = self.calculate_rule_score(features, rules)
if score > best_score:
best_score = score
best_category = category
# 阈值判断
if best_score >= 0.3: # 30%匹配度
return best_category
return 'uncategorized'
def calculate_rule_score(self, features, rules):
"""计算规则匹配分数"""
score = 0
total_weight = 0
# 关键词匹配
if 'keywords' in rules:
total_weight += 1
keywords = set(k.lower() for k in rules['keywords'])
matched = keywords.intersection(set(features['words']))
score += len(matched) / len(keywords)
# 模式匹配
if 'patterns' in rules:
total_weight += 1
patterns = set(rules['patterns'])
matched = patterns.intersection(set(features['patterns']))
score += len(matched) / len(patterns) if patterns else 0
return score / total_weight if total_weight > 0 else 0
def organize(self, source_dir, dry_run=False):
"""执行文件组织"""
source = Path(source_dir)
for file_path in source.iterdir():
if not file_path.is_file():
continue
category = self.fuzzy_classify(file_path)
self.statistics[category] += 1
if category and not dry_run:
dest_dir = source / category
dest_dir.mkdir(exist_ok=True)
dest_path = dest_dir / file_path.name
if not dest_path.exists():
file_path.rename(dest_path)
print(f"移动: {file_path.name} -> {category}/")
else:
# 处理重名文件
new_name = f"{file_path.stem}_{hashlib.md5(str(file_path).encode()).hexdigest()[:8]}{file_path.suffix}"
file_path.rename(dest_dir / new_name)
print(f"移动(重命名): {file_path.name} -> {category}/{new_name}")
# 输出统计信息
print("\n分类统计:")
for category, count in self.statistics.most_common():
print(f" {category}: {count}个文件")
# 使用示例
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='文件内容模糊粗糙规划工具')
parser.add_argument('directory', help='要处理的目录')
parser.add_argument('--config', help='规则配置文件')
parser.add_argument('--dry-run', action='store_true', help='仅预览不执行')
args = parser.parse_args()
# 如果没有配置文件,使用默认规则
default_config = {
'documents': {
'keywords': ['文档', '说明', '手册', '指南'],
'patterns': ['text']
},
'scripts': {
'keywords': ['python', '脚本', '函数', '类'],
'patterns': ['code']
},
'data': {
'keywords': ['数据', '记录', '统计', '统计'],
'patterns': ['csv', 'json']
}
}
organizer = FuzzyOrganizer(args.config)
# 如果没有配置,设置默认
if not organizer.config:
organizer.config = default_config
organizer.organize(args.directory, args.dry_run)
使用配置文件示例
// rules.json
{
"reports": {
"keywords": ["报告", "报表", "统计", "分析"],
"patterns": ["text", "datetime"],
"min_score": 0.3
},
"code": {
"keywords": ["import", "def", "class", "function"],
"patterns": ["code"],
"min_score": 0.4
},
"documents": {
"keywords": ["说明", "文档", "笔记", "quot;],
"patterns": ["text"],
"min_score": 0.2
},
"logs": {
"keywords": ["error", "warning", "exception", "log"],
"patterns": ["datetime", "code"],
"min_score": 0.3
}
}
使用建议
- 先进行预览:使用
--dry-run参数预览结果 - 定义清晰的规则:关键词要具有代表性
- 设置合适的阈值:根据文件类型调整匹配敏感度
- 定期检查结果:确保分类效果符合预期
这些脚本可以根据您的具体需求进行修改和扩展。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播