79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
# Agenten Plattform
|
|
#
|
|
# (c) 2024 Magnus Bender
|
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
|
# Universitaet Hamburg
|
|
# https://www.chai.uni-hamburg.de/~bender
|
|
#
|
|
# source code released under the terms of GNU Public License Version 3
|
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
|
|
|
import re
|
|
|
|
from typing import Callable
|
|
|
|
from ums.agent import ExtractAudioAgent, ExtractImageAgent, ExtractTextAgent
|
|
from ums.utils import RiddleData, AgentMessage, ExtractedData, ExtractedContent, ExtractedPositions
|
|
|
|
class SimpleExtractAudioAgent(ExtractAudioAgent):
|
|
# here we do not have an implementation for extracting audio,
|
|
# normally, we would not have this class, but here as example
|
|
|
|
def handle(self, data: RiddleData) -> RiddleData:
|
|
print("Audio Process:", data.file_plain)
|
|
return data
|
|
|
|
class SimpleExtractImageAgent(ExtractImageAgent):
|
|
# equally, we would not have this class without implementation
|
|
|
|
def before_response(self, response: AgentMessage, send_it: Callable[[], None]) -> bool:
|
|
# agents are able to prevent sending response messages to the management
|
|
# or send this messages later via `send_it()``
|
|
|
|
print("The response would be:", response)
|
|
|
|
# just stop the response from being sent
|
|
return False
|
|
|
|
def handle(self, data: RiddleData) -> RiddleData:
|
|
print("Image Process:", data.file_plain)
|
|
return data
|
|
|
|
class SimpleExtractTextAgent(ExtractTextAgent):
|
|
|
|
def handle(self, data: RiddleData) -> RiddleData:
|
|
print("Text Process:", data.file_plain)
|
|
|
|
# here we extract the variables assigned with numbers
|
|
found = False
|
|
with open(data.file_plain) as f:
|
|
for i, line in enumerate(f):
|
|
if "=" in line:
|
|
match = re.match(r"([a-z]{1})\s*=\s*(\d+)", line.strip())
|
|
if not match is None:
|
|
variable = match.group(1)
|
|
value = int(match.group(2))
|
|
found = True
|
|
line_no = i
|
|
if found:
|
|
extracted = ExtractedData(
|
|
contents=[
|
|
ExtractedContent(type="variable",content=variable),
|
|
ExtractedContent(type="value",content=value)
|
|
],
|
|
positions=[
|
|
ExtractedPositions(type="line",position=line_no),
|
|
ExtractedPositions(type="line",position=line_no)
|
|
],
|
|
other={
|
|
"variable" : variable,
|
|
"value" : value
|
|
}
|
|
)
|
|
data.file_extracted = self.store_extracted(data, extracted)
|
|
|
|
return data
|
|
|
|
AGENT_CLASSES = [
|
|
SimpleExtractAudioAgent, SimpleExtractImageAgent, SimpleExtractTextAgent
|
|
] |