1

I am trying to use fuzzy logic to weight and extract the best sentences for the query. I have extracted the following features which they can be used in fuzzy logic:

  • Each sentence has cosine value.
  • How many proper-noun is in the sentence.
  • the position of the sentence in the document.
  • sentence length.

I want to use the above features to apply the fuzzy logic. for instance, i want to create the rule base something like the following

if cosineValue >= 0.9 && numberOfPropernoun >=1 THEN the sentence is important

I am not quite sure how to start implementing the rule base, the facts and inference engine. It would like someone to guide me to implement this in python. Please note that I am not familiar with logic programming languages. I would like to implement it in python

false
  • 10,264
  • 13
  • 101
  • 209
elyon
  • 37
  • 6

1 Answers1

0

This is just a sketch; I'm not even going to try this code because I'm not sure what you want.

Make a class for your features:

Features = namedtuple('Features', ['cosine', 'nouns', 'position', ...])

etc.

Now imagine you are building your AST. What grammar does your language have? Well, you have conditions, and your conditions have consequences, and your conditions can be combined by boolean operators, so let's make some basic ones:

class CosineValue(object):
  def evaluate(self, features):
    return features.cosine

class Nouns(object):
  def evaluate(self, features):
    return features.nouns

... etc.

Now you need to combine these AST nodes with some operations

class GreaterThan(object):
  def __init__(self, property, value):
    self.property, self.value = property, value

  def evaluate(self, sentence):
    return property.evaluate(sentence) > self.value

Now GreaterThan(CosineValue(), 0.9) is an object (an abstract syntax tree, actually) that represents cosineValue > 0.9. You can evaluate it like so:

expr = GreaterThan(CosineValue(), 0.9)
expr.evaluate(Features(cosine=0.95, ...))  # returns True
expr.evaluate(Features(cosine=0.40, ...))  # returns False

These objects don't look like much, but what they are doing is reifying your process. Their structure encodes what formerly would have been code. Think about this, because this is the only hard part about what you are trying to do: comprehending how you can delay computation by turning it into structure, and how you can play with when values become part of your computation. You were probably stuck thinking about how to write those "if" statements and keeping them separate from the code and the runtime values you need to run them against. Now you should be able to see how, but it's a more advanced way of thinking about programming.

Now you need to build your if/then structure. I'm not sure what you want here either but I would say your if/then is going to be a class that takes an expression like we've just created as one argument and a "then" case, and does the test and either performs or does not perform the "then" case. Probably you will need if/then/else, or else a way to track if it fired, or a way to evaluate your if into a value. You will have to think about this part; nobody can tell you based on what you wrote above what you should be doing.

To make your conditions more powerful, you will need to add some more classes for boolean operators that take conditions as arguments, but it should be straightforward; you'll have And and Or, they'll both take two Condition arguments and their evaluation will do the sensible thing. You could make a Condition superclass, and then add some methods like And and Or to simplify generating these structures.

Finally, if you want to parse something like what you have above, you should try out pyparsing, but make sure you have the AST figured out first or it will be an uphill battle. Or look at what they have; maybe they have some primitives for this, I haven't dealt with pyparsing in a long time.

Best of luck, and please ask a better question next time!

Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77