Python Forum
Use PM4PY and create working file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Use PM4PY and create working file (/thread-41119.html)



Use PM4PY and create working file - thomaskissas33 - Nov-14-2023

Hello
I have a small project to read a text file ( a small book) and make some data mining using nltk library and pm4py. With nltk libray ok but with pm4py i have a few issues. For example my basic issue is how to convert a text file to xes file with activities date and time and be a format like the picture

https://pm4py.fit.fraunhofer.de/static/assets/images/getting_started/csv_snapshot.png

I figured to read a text file (book) and create a xes file with random dates and hours and names but i am not sure if create the correct file

i tried this:

#Program that will create a file from text with random dates and time and save it as xes

import pm4py
from pm4py.objects.log.obj import EventLog, Trace, Event
from datetime import datetime, timedelta
import random

# Specify the path to read file
input_text_file = 'C:\\Users\\User\\Desktop\python\Program_Prof\\room.txt'

# Read the content of the text file
with open(input_text_file, 'r', encoding='utf-8') as file:
    activity_names = [activity.strip() for activity in file.readlines()]

# Create an empty event log
event_log = EventLog()

# Specify the number of traces and events per trace
num_traces = 10
num_events_per_trace = 5

# Generate random traces with activities, hours, and datetime
for _ in range(num_traces):
    trace = Trace()
    for _ in range(num_events_per_trace):
        activity = random.choice(activity_names)
        random_datetime = datetime.now() - timedelta(days=random.randint(0, 30), hours=random.randint(0, 23), minutes=random.randint(0, 59))

        event = Event()
        event['concept:name'] = activity
        event['time:timestamp'] = random_datetime
        event['custom:additional_column'] = 'some_value'  # Add custom columns as needed

        trace.append(event)

    event_log.append(trace)

# Specify the path to your output XES file
output_xes_file = 'C:\\Users\\User\\Desktop\python\Program_Prof\\room5.xes'

# Write the event log to the XES file
pm4py.write_xes(event_log, output_xes_file)

print(f"XES log with random data saved to {output_xes_file}")

#read and print first activities

xes_file_path = 'C:\\Users\\User\\Desktop\python\Program_Prof\\room5.xes'

# Open the XES file and print the first 10 lines
with open(xes_file_path, 'r', encoding='utf-8') as file:
    for i in range(30):
        line = file.readline()
        print(line.strip())
Any better ideas to create my xes file better so i can use algorithms like fuzy miner and alpha miner and obtain Process Model and a map process