Add commands and parsers modules
This commit is contained in:
81
.termflow/parsers/pattern_analyzer.py
Normal file
81
.termflow/parsers/pattern_analyzer.py
Normal file
@@ -0,0 +1,81 @@
|
||||
"""Pattern detection in command sequences."""
|
||||
|
||||
from collections import Counter
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class CommandPattern:
|
||||
"""Represents a detected command pattern."""
|
||||
commands: str
|
||||
frequency: int
|
||||
context: Optional[str] = None
|
||||
|
||||
|
||||
class PatternDetector:
|
||||
"""Detects patterns in command sequences."""
|
||||
|
||||
def __init__(self):
|
||||
self.patterns: List[CommandPattern] = []
|
||||
|
||||
def analyze_commands(self, commands: List[str]) -> List[CommandPattern]:
|
||||
"""Analyze command list for patterns."""
|
||||
if not commands:
|
||||
return []
|
||||
|
||||
command_counter = Counter(commands)
|
||||
self.patterns = [
|
||||
CommandPattern(commands=cmd, frequency=count)
|
||||
for cmd, count in command_counter.most_common(10)
|
||||
]
|
||||
|
||||
return self.patterns
|
||||
|
||||
def find_sequences(self, commands: List[str], min_length: int = 2) -> List[CommandPattern]:
|
||||
"""Find common command sequences."""
|
||||
if len(commands) < min_length:
|
||||
return []
|
||||
|
||||
sequences = []
|
||||
for i in range(len(commands) - min_length + 1):
|
||||
seq = " ".join(commands[i : i + min_length])
|
||||
sequences.append(seq)
|
||||
|
||||
sequence_counter = Counter(sequences)
|
||||
return [
|
||||
CommandPattern(commands=seq, frequency=count)
|
||||
for seq, count in sequence_counter.most_common(5)
|
||||
if count > 1
|
||||
]
|
||||
|
||||
|
||||
class SequenceAnalyzer:
|
||||
"""Analyzes command sequences for workflows."""
|
||||
|
||||
def __init__(self):
|
||||
self.common_patterns = [
|
||||
"git add .",
|
||||
"git commit",
|
||||
"git push",
|
||||
"git pull",
|
||||
"npm install",
|
||||
"pip install",
|
||||
"docker build",
|
||||
"docker run",
|
||||
]
|
||||
|
||||
def identify_workflow(self, commands: List[str]) -> str:
|
||||
"""Identify the type of workflow from commands."""
|
||||
command_str = " ".join(commands).lower()
|
||||
|
||||
if "docker" in command_str:
|
||||
return "docker"
|
||||
elif "git" in command_str:
|
||||
return "git"
|
||||
elif "npm" in command_str or "yarn" in command_str:
|
||||
return "javascript"
|
||||
elif "pip" in command_str or "python" in command_str:
|
||||
return "python"
|
||||
else:
|
||||
return "general"
|
||||
Reference in New Issue
Block a user