Issue with Experta-based Recommendation System - Python3

27 views Asked by At

I am currently working on a recommendation system using the Experta library in Python. The system is designed to recommend laptops based on usage patterns and price preferences. However, I am facing an issue where the recommendations are not being displayed as expected.

Below is the code for the recommendation system:

import yaml
from experta import KnowledgeEngine, Rule, Fact, AND

class Usage(Fact):
    pass

class Price(Fact):
    pass

class LaptopRecommender(KnowledgeEngine):
    pass

def load_rules_from_yaml(engine, yaml_file):
    with open(yaml_file, 'r') as file:
        rules_data = yaml.safe_load(file)
        for rule_data in rules_data:
            rule_name = rule_data['rule']
            conditions = rule_data['conditions']
            recommendation = rule_data['recommendation']


            @Rule(Usage(conditions[0]['usage']) & Price(conditions[1]['price']))
            def temporary_name():
                print(recommendation)

            setattr(LaptopRecommender, rule_name, temporary_name)

engine = LaptopRecommender()
load_rules_from_yaml(engine, 'laptop_rules.yaml')

engine.reset()
engine.declare(Usage('gaming and design'))
engine.declare(Price('High'))
engine.run()

And here are the content of the 'laptop_rules.yaml' file:

- rule: recommend_gaming_design_high_price
  conditions:
    - usage: gaming and design
    - price: High
  recommendation: "I recommend a high-performance gaming laptop for design and gaming."

- rule: recommend_gaming_design_medium_price
  conditions:
    - usage: gaming and design
    - price: Medium
  recommendation: "I recommend a gaming laptop with good performance for design and gaming."

- rule: recommend_gaming_design_low_price
  conditions:
    - usage: gaming and design
    - price: Low
  recommendation: "I recommend a budget-friendly gaming laptop for design and gaming."

- rule: recommend_desk_work_high_price
  conditions:
    - usage: desk work
    - price: High
  recommendation: "I recommend a high-quality laptop for desk work."

- rule: recommend_desk_work_medium_price
  conditions:
    - usage: desk work
    - price: Medium
  recommendation: "I recommend a reliable laptop for desk work."

- rule: recommend_desk_work_low_price
  conditions:
    - usage: desk work
    - price: Low
  recommendation: "I recommend a budget-friendly laptop for desk work."

- rule: recommend_engineering_high_price
  conditions:
    - usage: engineering
    - price: High
  recommendation: "I recommend a powerful laptop for engineering tasks."

- rule: recommend_engineering_medium_price
  conditions:
    - usage: engineering
    - price: Medium
  recommendation: "I recommend a laptop with good performance for engineering tasks."

- rule: recommend_engineering_low_price
  conditions:
    - usage: engineering
    - price: Low
  recommendation: "I recommend a budget-friendly laptop for engineering tasks."

I have carefully reviewed my code and the YAML file, ensuring that the rule conditions match the declared facts. I've also checked for any syntax errors or issues with the Experta library. Additionally, I have searched online documentation and forums for similar issues, but I couldn't find a solution. I expect that when running the recommendation system with the provided facts (usage: 'gaming and design', price: 'High'), the corresponding rule (recommend_gaming_design_high_price) would be triggered, and the system should print the recommendation: "I recommend a high-performance gaming laptop for design and gaming." However, I am not seeing any output, and I'm seeking assistance to understand why the recommendations are not being displayed as expected.

0

There are 0 answers