Esempio n. 1
0
  def __init__(self):
    """
    Configuration
    """

    # Camera settings
    self.FRAME_WIDTH = 341
    self.FRAME_HEIGHT = 256
    self.flip_camera = True # Mirror image
    self.camera = cv2.VideoCapture(1)

    # ...you can also use a test video for input
    #video = "/Users/matthiasendler/Code/snippets/python/tracker/final/assets/test_video/10.mov"
    #self.camera = cv2.VideoCapture(video)
    #self.skip_input(400) # Skip to an interesting part of the video

    if not self.camera.isOpened():
        print "couldn't load webcam"
        return
    #self.camera.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, self.FRAME_WIDTH)
    #self.camera.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, self.FRAME_HEIGHT)

    self.filters_dir = "filters/" # Filter settings in trackbar
    self.filters_file = "filters_default"

    # Load filter settings
    current_config = self.filters_dir + self.filters_file
    self.filters = Filters(current_config)

    # No actions will be triggered in test mode
    # (can be used to adjust settings at runtime)
    self.test_mode = False

    # Create a hand detector
    # In fact, this is a wrapper for many detectors
    # to increase detection confidence
    self.detector = Detector(self.filters.config)

    # Knowledge base for all detectors
    self.kb = KB()
    # Create gesture recognizer.
    # A gesture consists of a motion and a hand state.
    self.gesture = Gesture()

    # The action module executes keyboard and mouse commands
    self.action = Action()

    # Show output of detectors
    self.output = Output()

    self.run()
Esempio n. 2
0
class Tracker(object):
  """
  This is the main program which gives a high-level view
  of all the running subsystems. It connects camera input with
  output in form of "actions" (such as keyboard shortcuts on the users behalf).
  This is done by locating a hand in an image and detecting features,
  like the number of fingers, and trying to match that data with a
  known gesture.
  """

  def __init__(self):
    """
    Configuration
    """

    # Camera settings
    self.FRAME_WIDTH = 341
    self.FRAME_HEIGHT = 256
    self.flip_camera = True # Mirror image
    self.camera = cv2.VideoCapture(1)

    # ...you can also use a test video for input
    #video = "/Users/matthiasendler/Code/snippets/python/tracker/final/assets/test_video/10.mov"
    #self.camera = cv2.VideoCapture(video)
    #self.skip_input(400) # Skip to an interesting part of the video

    if not self.camera.isOpened():
        print "couldn't load webcam"
        return
    #self.camera.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, self.FRAME_WIDTH)
    #self.camera.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, self.FRAME_HEIGHT)

    self.filters_dir = "filters/" # Filter settings in trackbar
    self.filters_file = "filters_default"

    # Load filter settings
    current_config = self.filters_dir + self.filters_file
    self.filters = Filters(current_config)

    # No actions will be triggered in test mode
    # (can be used to adjust settings at runtime)
    self.test_mode = False

    # Create a hand detector
    # In fact, this is a wrapper for many detectors
    # to increase detection confidence
    self.detector = Detector(self.filters.config)

    # Knowledge base for all detectors
    self.kb = KB()
    # Create gesture recognizer.
    # A gesture consists of a motion and a hand state.
    self.gesture = Gesture()

    # The action module executes keyboard and mouse commands
    self.action = Action()

    # Show output of detectors
    self.output = Output()

    self.run()

  def run(self):
    """
    In each step: Read the input image and keys,
    process it and react on it (e.g. with an action).
    """
    while True:
      img = self.get_input()
      hand = self.process(img)
      ref = self.action.get_reference_point()
      self.output.show(img, hand, ref)

  def process(self, img):
    """
    Process input
    """
    # Run detection
    hand = self.detector.detect(img)
    # Store result in knowledge base
    self.kb.update(hand)
    if not self.test_mode:
      # Try to interprete as gesture
      self.interprete(hand)
    return hand

  def interprete(self, hand):
    """
    Try to interprete the input as a gesture
    """
    self.gesture.add_hand(hand)
    operation = self.gesture.detect_gesture()
    self.action.execute(operation)

  def get_input(self):
    """
    Get input from camera and keyboard
    """
    self.get_key()
    _, img = self.camera.read()
    img = cv2.resize(img, (self.FRAME_WIDTH, self.FRAME_HEIGHT))
    if self.flip_camera:
      img = cv2.flip(img, 1)
    return img


  def get_key(self):
    """
    Read keyboard input
    """
    key = cv2.waitKey(self.filters.config["wait_between_frames"])
    if key == ord('+'):
      # Reduce program speed
      self.filters.config["wait_between_frames"] += 500
    if key == ord('-'):
      # Increase program speed
      if self.filters.config["wait_between_frames"] >= 500:
        self.filters.config["wait_between_frames"] -= 500
    #if key == ord('s'):
    # Save config
    #  self.filters.save()
    if key == ord('r'):
      # Reset all detectors
      self.detector.reset()
      self.action.reset()
    if key == ord('d'):
      # Make a screenshot
      self.output.make_screenshot()
    if key == ord('p') or key == ord(' '):
      # Pause
      cv2.waitKey()
    if key == ord('t'):
      # Test mode
      self.test_mode = not self.test_mode
    if key == ord('1'):
      self.output.toggle_estimate()
    if key == ord('2'):
      self.output.toggle_detectors()
    if key == ord('3'):
      self.output.toggle_skin()
    if key == ord('f'):
      self.toggle_filters()
    if key == 63235: # Right arrow
      self.skip_input(20)
    if key == 27 or key == ord('q'):
      # Abort program on ESC, q or space
      exit()

  def toggle_filters(self):
    """
    Load the next filter settings
    """
    self.filters_file = self.next_filters_file()
    current_config = self.filters_dir + self.filters_file
    self.filters.set_config(current_config)

  def next_filters_file(self):
    """
    Get the next filter settings
    """
    filters = listdir(self.filters_dir)
    for i, f in enumerate(filters):
      if f == self.filters_file:
        return filters[(i+1) % len(filters)]

  def skip_input(self, x=1):
    """
    Skip to a different part of a video sequence.
    """
    for i in range(0,x):
      self.camera.grab()
Esempio n. 3
0
hence we play the lowest possible card. If the played card is not from the trump suit, we first check for a card
stronger than that card, then we look for a trump card to win that trick. If these conditions are not fulfilled,
it can be predicted that we loose that trick, hence we simply play the lowest non-trump card. Again, if we don't have
such a card, we play the lowest possible card.

It loads general information about the game, as well as the definition of a strategy,
from load.py.
"""

from api import State, util, Deck
import random, load, copy

from bots.ultra import kb
from kb import KB, Boolean, Integer

kb = KB()
THRESHOLD = 10

class Bot:
    __RANKS = ["a", "t", "k", "q", "j"]

    def __init__(self):
        pass

    def get_move(self, state):
        whose_turn = state.whose_turn()
        leader = state.leader()
        trump_suit = state.get_trump_suit()

        if whose_turn == leader:
            global kb
Esempio n. 4
0
    def __init__(self, num_relations, num_entities,
                 arities=[0.0, 1.0, 0.0],
                 fb_densities=[0.0, 0.0, 0.0],
                 arg_densities=[0., 0.1, 0.0],
                 fact_prob=0.2,
                 num_symm=2,
                 num_impl=[0, 2, 0],
                 num_impl_inv=2,
                 num_impl_conj=[0, 2, 0],
                 num_trans_single=2,
                 num_trans_diff=2,
                 seed=0,
                 position_dependent_args=False,
                 position_densities=[0., 0.5, 0.0]):
        """
        :param num_relations:
        :param num_entities: number of distinct entities to generate
        :param arities:  fraction of arities
        :param arg_densities: fraction of entity combinations that are observed
        :param fact_prob:
        :param num_inv: number of 'inv' formulae   R(X0, X1) :- R(X1, X0)
        :param num_impl:
        :param num_impl_conj:
        :param num_trans:
        :param negated_head_prob:
        :param seed:
        :return:
        """
        random.seed(seed)
        self.kb = KB(seed=seed)

        num_relations_per_arity = [int(x * num_relations) for x in arities]

        entities = list(map(lambda x: "e" + str(x), range(1, num_entities+1)))

        entities_arg1 = []
        entities_arg2 = []
        entities_arg3 = []

        if position_dependent_args:
            arg1_boundary = int(len(entities)*position_densities[0])
            arg2_boundary = arg1_boundary + int(len(entities)*position_densities[1])
            entities_arg1 = entities[0:arg1_boundary]
            entities_arg2 = entities[arg1_boundary:arg2_boundary]
            entities_arg3 = entities[arg2_boundary:]
        else:
            entities_arg1 = entities
            entities_arg2 = entities
            entities_arg3 = entities

        pairs = [(x, y) for x in entities_arg1
                 for y in entities_arg2 if not x == y]

        triples = [(x, y, z) for x in entities_arg1
                    for y in entities_arg2 for z in entities_arg3
                    if not x == y and not y == z and not z == x]

        num_pair_samples = min(len(pairs), int(len(entities_arg1) *
                                               len(entities_arg2) *
                                               arg_densities[1]))
        num_triple_samples = min(len(triples), int(len(entities_arg1) *
                                                   len(entities_arg2) *
                                                   len(entities_arg3) *
                                                   arg_densities[2]))
        entities_per_arity = {
            1: entities_arg1,
            2: random.sample(pairs, num_pair_samples),
            3: random.sample(triples, num_triple_samples)
        }

        relations_per_arity = {}
        for arity in range(1, len(num_relations_per_arity) + 1):
            for i in range(1, num_relations_per_arity[arity - 1] + 1):
                fb_prefix = ""
                if fb_densities[arity-1] > random.uniform(0, 1.0):
                    fb_prefix = "REL$"
                if arity == 1:
                    rel = fb_prefix+"u"
                elif arity == 2:
                    rel = fb_prefix+"b"
                else:
                    rel = fb_prefix+"t"
                rel += str(i)

                if not arity in relations_per_arity:
                    relations_per_arity[arity] = list()
                relations_per_arity[arity].append(rel)

                for args in random.sample(entities_per_arity[arity],
                                          int(len(entities_per_arity[arity]) * fact_prob)):
                    self.kb.add_train(rel, args)

        inverse = []
        # sample symmetric relations r(X,Y) => r(Y,X)
        if 2 in relations_per_arity:
            symm = random.sample([(x, x) for x in relations_per_arity[2]], num_symm)
            inverse += symm

        # sampling implication, reversed: r1(X,Y) => r2(Y,X)
        if 2 in relations_per_arity:
            inverse += random.sample([(x, y) for x in relations_per_arity[2]
                                     for y in relations_per_arity[2]
                                     if not x == y], num_impl_inv)
        if len(inverse) > 0:
            self.kb.add_formulae("inv", {2: inverse})

        # sampling implications:
        # r1(X) => r2(X)
        # r1(X,Y) => r2(X,Y)
        implications_per_arity = {}
        for arity in range(1, len(num_relations_per_arity) + 1):
            if arity in relations_per_arity:
                implications_per_arity[arity] = \
                    random.sample([(x, y) for x in relations_per_arity[arity] for y in relations_per_arity[arity]
                                   if not x == y], num_impl[arity - 1])
        self.kb.add_formulae("impl", implications_per_arity)

        # sampling implications with conjunction in body:
        # r1(X,Y) ^ r2(X,Y) => r3(X,Y)
        # r1(X) ^ r2(X) => r3(X)
        implications_with_conjunction_per_arity = {}
        for arity in range(1, len(num_relations_per_arity) + 1):
            if arity in relations_per_arity and len(relations_per_arity[arity]) >= 3:
                implications_with_conjunction_per_arity[arity] = \
                    random.sample([(x, y, z) for x in relations_per_arity[arity]
                                   for y in relations_per_arity[arity]
                                   for z in relations_per_arity[arity]
                                   if not x == y and not y == z and not z == x],
                                  num_impl_conj[arity - 1])
        self.kb.add_formulae("impl_conj", implications_with_conjunction_per_arity)

        # sampling transitivities:
        transitivities = []
        # (1) simple transitivities  r(X,Y) ^ r(Y,Z) => r(X,Z)
        # (2) general transitivities  r1(X,Y) ^ r2(Y,Z) => r3(X,Z)  (r1, r2, r3 differ)

        if 2 in relations_per_arity:
            if num_trans_single > 0:
                transitivities += random.sample([(x, x, x)
                                                for x in relations_per_arity[2]], num_trans_single)
            if num_trans_diff > 0:
                transitivities += random.sample([(x, y, z)
                                                for x in relations_per_arity[2]
                                                for y in relations_per_arity[2]
                                                for z in relations_per_arity[2]
                                                if not x == y and
                                                not y == z and
                                                not z == x], num_trans_diff)
        if len(transitivities) > 0:
            self.kb.add_formulae("trans", {2: transitivities})
Esempio n. 5
0
    def init(self,
             kb_host="localhost",
             kb_port=6969,
             embeddedkb=False,
             defaultontology=None,
             data_path=None):

        if not data_path:
            # try to guess the current prefix and then the data directory
            data_path = os.path.abspath(__file__).split('lib')[0].split(
                'src')[0] + 'share/dialogs/'
            logger.debug("Assuming Dialogs data dir is <%s>" % data_path)

        try:
            from kb import KB, KbError
        except ImportError:
            logger.error("Python bindings to access the knowledge are not available." + \
                         "Please install 'pykb' and restart Dialogs.")

        try:
            self.ontology_server = KB(kb_host, kb_port, embeddedkb,
                                      defaultontology)
        except KbError:
            logger.error("Error while trying to connect to the knowledge base on %s:%s" % (kb_host, kb_port) + \
                         ". Continuing without knowledge base. Amongst others, resolution won't work.")
            self.ontology_server = DummyKnowledgeBase()

        for line in open(os.path.join(data_path, "adjectives")):
            if line.startswith("#") or not line.strip():
                continue
            try:
                adj, cat = line.split()
            except ValueError:  # for adjectives without category, set a generic "Feature" category
                adj = line.split()[0]
                cat = "Feature"
            self.adjectives[adj] = cat

        verbs = [
            list(line.split())
            for line in open(os.path.join(data_path, "verbs"))
        ]
        verbs = self.split_list(verbs)
        self.irregular_verbs_past = verbs[0]
        self.irregular_verbs_present = verbs[1]
        self.preposition_verbs = verbs[2]
        self.modal = [k[0] for k in verbs[3]]
        self.adjective_verb = [k[0] for k in verbs[4]]
        self.auxiliary = [k[0] for k in verbs[5]]
        self.direct_transitive = [k[0] for k in verbs[6]]
        self.indirect_transitive = [k[0] for k in verbs[7]]
        self.state = [k[0] for k in verbs[8]]
        self.verb_need_to = [k[0] for k in verbs[9]]
        self.special_verbs = [k[0] for k in verbs[12]]

        # Action verbs such as 'see', 'hear' with no active behaviour
        self.action_verb_with_passive_behaviour = dict([(k[0], k[1])
                                                        for k in verbs[10]])
        self.goal_verbs = [k[0] for k in verbs[11]]

        self.sentence_starts = [
            tuple(line.split())
            for line in open(os.path.join(data_path, "sentence_starts"))
        ]

        nouns = [
            list(line.split())
            for line in open(os.path.join(data_path, "nouns"))
        ]
        nouns = self.split_list(nouns)
        self.special_nouns = [k[0] for k in nouns[0]]
        self.pronouns = [k[0] for k in nouns[1]]
        for i in nouns[1]:
            if i[1] == '1':
                self.complement_pronouns = self.complement_pronouns + [i[0]]
        self.demonstrative_det = [k[0] for k in nouns[2]]
        self.determinants = [k[0] for k in nouns[3]]
        self.nouns_end_s = [k[0] for k in nouns[4]]
        self.relatives = [k[0] for k in nouns[5]]
        self.composed_nouns = [k[0] for k in nouns[6]]
        self.plural_nouns = nouns[7]
        self.noun_not_composed = [k[0] for k in nouns[8]]
        self.days_list = nouns[9]
        self.months_list = nouns[10]
        self.unusable_words = [k[0] for k in nouns[11]]

        # List of diection words, E.g: LEFT, RIGHT, TOP, etc ...
        self.direction_words = [k[0] for k in nouns[12]]

        self.compound_nouns = nouns[13]

        ###
        ### ADVERBIALS

        adverbials = [
            list(line.split())
            for line in open(os.path.join(data_path, "adverbial"))
        ]
        adverbials = self.split_list(adverbials)
        self.adverbs = [k[0] for k in adverbials[0]]
        self.time_adverbs = adverbials[0]

        self.time_adverbs = [
            k[0] for k in adverbials[0] if k[1] in ["day", "hour"]
        ]
        self.time_adverbs += [
            k[0] for k in adverbials[1] if k[1] in ["day", "hour"]
        ]

        self.location_adverbs = [
            k[0] for k in adverbials[0] if k[1] == "location"
        ]
        self.location_adverbs += [
            k[0] for k in adverbials[1] if k[1] == "location"
        ]

        self.adverbs_at_end = [k[0] for k in adverbials[1]]

        for k in adverbials[2]:
            if k[1] == '1':
                self.compelement_proposals = self.compelement_proposals + [
                    k[0]
                ]
        self.proposals = [k[0] for k in adverbials[2]]

        #Preposition with an existing object_property
        # E.g: next+to => isNextTo
        self.preposition_rdf_object_property = dict([(k[0], k[3:])
                                                     for k in adverbials[2]])
        self.time_proposals = adverbials[2]
        self.subsentences = [k[0] for k in adverbials[3]]
        for k in adverbials[3]:
            if k[1] == '1':
                self.adv_sub = self.adv_sub + [k[0]]
        self.prep_change_place = [k[0] for k in adverbials[4]]

        grammatical_rules = [
            list(line.split())
            for line in open(os.path.join(data_path, "grammatical_rules"))
        ]
        grammatical_rules = self.split_list(grammatical_rules)
        self.numbers = grammatical_rules[0]
        self.det_quantifiers = grammatical_rules[1]
        self.capital_letters = [k[0] for k in grammatical_rules[2]]
        self.adjective_rules = [k[0] for k in grammatical_rules[3]]
        self.concatenate_proposals = grammatical_rules[4]
        self.change_tuples = grammatical_rules[5]
        self.adjective_numbers_digit = grammatical_rules[6]
        self.adjective_numbers = [k[0] for k in grammatical_rules[6]]
        self.be_pronoun = [k[0] for k in grammatical_rules[7]]
        self.adj_quantifiers = [k[0] for k in grammatical_rules[8]]
        for k in grammatical_rules[9]:
            self.replace_tuples = self.replace_tuples + [[k[0], k[1:]]]

        desc = ""
        for line in open(os.path.join(data_path, "thematic_roles")):
            if line.startswith("#") or not line.strip():
                continue

            desc += line

            if line.startswith("}"):  #end of block
                self.thematic_roles.add_verb(desc)
                desc = ""

        #Add action verbs to the ontology
        if self.ontology_server:
            stmts = [
                verb.capitalize() + " rdfs:subClassOf cyc:PurposefulAction"
                for verb in self.thematic_roles.verbs.keys()
                if not self.thematic_roles.verbs[verb].is_synonym()
            ]
            self.ontology_server.revise(stmts, {"method": "add"})
        """
            List of ontology classes that are used in the adjectives list
        """
        self.adjectives_ontology_classes = [
            self.adjectives[adj].lower() for adj in self.adjectives
        ]
        adj_s = []
        for k in self.adjectives_ontology_classes:
            if not k in adj_s:
                adj_s.append(k)
        self.adjectives_ontology_classes = adj_s
Esempio n. 6
0
PA7 = Boolean('pa7')
PA8 = Boolean('pa8')
PA9 = Boolean('pa9')
PA10 = Boolean('pa10')
PA11 = Boolean('pa11')
PA12 = Boolean('pa12')
PA13 = Boolean('pa13')
PA14 = Boolean('pa14')
PA15 = Boolean('pa15')
PA16 = Boolean('pa16')
PA17 = Boolean('pa17')
PA18 = Boolean('pa18')
PA19 = Boolean('pa19')

# Create a new knowledge base
kb = KB()

# GENERAL INFORMATION ABOUT THE CARDS
# This adds information which cards are Jacks
kb.add_clause(J4)
kb.add_clause(J9)
kb.add_clause(J14)
kb.add_clause(J19)
# Add here whatever is needed for your strategy.
kb.add_clause(A0)
kb.add_clause(A5)
kb.add_clause(A10)
kb.add_clause(A15)
# DEFINITION OF THE STRATEGY
# Add clauses (This list is sufficient for this strategy)
# PJ is the strategy to play jacks first, so all we need to model is all x PJ(x) <-> J(x),
Esempio n. 7
0
import sys
from kb import KB, Boolean, Integer, Constant

# Define our symbols
P = Boolean('P')
Q = Boolean('Q')
R = Boolean('R')

# Create a new knowledge base
kb = KB()

# Add clauses
kb.add_clause(P, Q)
kb.add_clause(~Q, R)
kb.add_clause(~R, ~P)
kb.add_clause(~P, ~Q)

# Print all models of the knowledge base
for model in kb.models():
    print(model)

# Print out whether the KB is satisfiable (if there are no models, it is not satisfiable)
print(kb.satisfiable())
Esempio n. 8
0
import sys
from kb import KB, Boolean, Integer, Constant

# Define our integer symbols
x = Integer('x')
y = Integer('y')

q = x == y
a = x + y > 2
b = x + y < 5
c = x + y < -2
d = x + y > -5

kb = KB()

kb.add_clause(q)
kb.add_clause(???)
kb.add_clause(???)
kb.add_clause(???)
kb.add_clause(???)

for model in kb.models():
    print(model)
Esempio n. 9
0
from kb import KB
from fact import Fact
import sys

kb = KB()

kb_file = sys.argv[1]
query_file = sys.argv[2]
answer_file = sys.argv[3]
kb_file = open(kb_file, 'r')
clauses = kb_file.readlines()
KB.CreateKB(kb, clauses)

query_file = open(query_file, 'r')
answer_file = open(answer_file, 'w')
count_query = 1
for query in query_file.readlines():
    alpha = Fact.ParseFact(query)
    alpha_str = str(alpha) + '.'
    print(str(count_query) + "/", alpha_str)
    substs = set(kb.Query(alpha))
    substs_str = ' ;\n'.join([str(subst) for subst in substs]) + '.\n'
    print(substs_str)
    answer_file.write(alpha_str + '\n')
    answer_file.write(substs_str + '\n')
    count_query += 1
Esempio n. 10
0
    def kb_consistent(self, state, move):
        # type: (State, move) -> bool

        # each time we check for consistency we initialise a new knowledge-base
        kb = KB()

        # Add general information about the game
        load.general_information(kb)

        # Add the necessary knowledge about the strategy
        load.strategy_knowledge(kb)

        # This line stores the index of the card in the deck.
        # If this doesn't make sense, refer to _deck.py for the card index mapping
        index = move[0]

        # This creates the string which is used to make the strategy_variable.
        # Note that as far as kb.py is concerned, two objects created with the same
        # string in the constructor are equivalent, and are seen as the same symbol.
        # Here we use "pj" to indicate that the card with index "index" should be played with the
        # PlayJack heuristics that was defined in class. Initialise a different variable if
        # you want to apply a different strategy (that you will have to define in load.py)

        # # Always play trump
        trump_suit = state.get_trump_suit()
        trump = trump_suit.lower()
        if trump == 'c':
            pj = Boolean('pj4')
            pq = Boolean('pq3')
            pk = Boolean('pk2')
            pt = Boolean('pt1')
            pa = Boolean('pa0')
        if trump == 'd':
            pj = Boolean('pj9')
            pq = Boolean('pq8')
            pk = Boolean('pk7')
            pt = Boolean('pt6')
            pa = Boolean('pa5')
        if trump == 'h':
            pj = Boolean('pj14')
            pq = Boolean('pq13')
            pk = Boolean('pk12')
            pt = Boolean('pt11')
            pa = Boolean('pa10')
        if trump == 's':
            pj = Boolean('pj19')
            pq = Boolean('pq18')
            pk = Boolean('pk17')
            pt = Boolean('pt16')
            pa = Boolean('pa15')

        kb.add_clause(~pj, ~pq, ~pk, ~pt, ~pa)

        # # always play Jack
        # variable_string = "pj" + str(index)
        # strategy_variable = Boolean(variable_string)

        # Add the relevant clause to the loaded knowledge base
        # kb.add_clause(~strategy_variable)

        # If the knowledge base is not satisfiable, the strategy variable is
        # entailed (proof by refutation)
        return kb.satisfiable()
PJ7 = Boolean('pj7')
PJ8 = Boolean('pj8')
PJ9 = Boolean('pj9')
PJ10 = Boolean('pj10')
PJ11 = Boolean('pj11')
PJ12 = Boolean('pj12')
PJ13 = Boolean('pj13')
PJ14 = Boolean('pj14')
PJ15 = Boolean('pj15')
PJ16 = Boolean('pj16')
PJ17 = Boolean('pj17')
PJ18 = Boolean('pj18')
PJ19 = Boolean('pj19')

# Create a new knowledge base
kb = KB()

# GENERAL INFORMATION ABOUT THE CARDS
# This adds information which cards are Jacks
kb.add_clause(J4)
kb.add_clause(J9)
kb.add_clause(J14)
kb.add_clause(J19)
# Adds information on which cards are As
kb.add_clause(J0)
kb.add_clause(J5)
kb.add_clause(J10)
kb.add_clause(J15)

# DEFINITION OF THE STRATEGY
# Add clauses (This list is sufficient for this strategy)
Esempio n. 12
0
def main(args):
    if (not args.do_train) and (not args.do_valid) and (not args.do_test):
        raise ValueError('one of train/val/test mode must be choosed.')

    if args.init_checkpoint:
        override_config(args)
    elif args.data_path is None:
        raise ValueError('one of init_checkpoint/data_path must be choosed.')

    if args.do_train and args.save_path is None:
        raise ValueError('Where do you want to save your trained model?')

    if args.save_path and not os.path.exists(args.save_path):
        os.makedirs(args.save_path)

    # Write logs to checkpoint and console
    set_logger(args)

    with open(os.path.join(args.data_path, 'entities.dict')) as fin:
        entity2id = dict()
        id2entity = dict()
        for line in fin:
            eid, entity = line.strip().split('\t')
            entity2id[entity] = int(eid)
            id2entity[int(eid)] = entity

    with open(os.path.join(args.data_path, 'relations.dict')) as fin:
        relation2id = dict()
        id2relationship = dict()
        for line in fin:
            rid, relation = line.strip().split('\t')
            relation2id[relation] = int(rid)
            id2relationship[int(rid)] = relation

    # Read regions for Countries S* datasets
    if args.countries:
        regions = list()
        with open(os.path.join(args.data_path, 'regions.list')) as fin:
            for line in fin:
                region = line.strip()
                regions.append(entity2id[region])
        args.regions = regions

    e_vocab = Dictionary(tok2ind=entity2id, ind2tok=id2entity)
    r_vocab = Dictionary(tok2ind=relation2id, ind2tok=id2relationship)
    ## TODO: add graph file
    graph = KB(os.path.join(args.data_path, 'train.txt'),
               e_vocab=e_vocab,
               r_vocab=r_vocab)

    nentity = len(entity2id)
    nrelation = len(relation2id)

    args.nentity = nentity
    args.nrelation = nrelation

    logging.info('Model: %s' % args.model)
    logging.info('Data Path: %s' % args.data_path)
    logging.info('#entity: %d' % nentity)
    logging.info('#relation: %d' % nrelation)

    train_triples = read_triple(os.path.join(args.data_path, 'train.txt'),
                                entity2id, relation2id)
    logging.info('#train: %d' % len(train_triples))
    valid_triples = read_triple(os.path.join(args.data_path, 'valid.txt'),
                                entity2id, relation2id)
    logging.info('#valid: %d' % len(valid_triples))
    test_triples = read_triple(os.path.join(args.data_path, 'test.txt'),
                               entity2id, relation2id)
    logging.info('#test: %d' % len(test_triples))
    candidate_entities = None
    if args.rerank_minerva:
        candidate_entities = defaultdict(set)
        with open(
                "/home/shdhulia//Limits-of-Path-Reasoner/outputs/FB15K-237/thisone_test/all_answers.txt"
        ) as candidate_file:
            # with open("/home/shdhulia/minerva_answers/fb.txt") as candidate_file:
            for line in candidate_file:
                pt = line.strip().split("\t")
                e1 = entity2id[pt[0]]
                r = relation2id[pt[1]]
                predicted_es = set(
                    [entity2id[p] for p in pt[2:] if p in entity2id])
                candidate_entities[(e1, r)] = set(predicted_es)

    #All true triples
    all_true_triples = train_triples + valid_triples + test_triples

    kge_model = KGEModel(
        model_name=args.model,
        nentity=nentity,
        nrelation=nrelation,
        hidden_dim=args.hidden_dim,
        gamma=args.gamma,
        double_entity_embedding=args.double_entity_embedding,
        double_relation_embedding=args.double_relation_embedding)

    logging.info('Model Parameter Configuration:')
    for name, param in kge_model.named_parameters():
        logging.info('Parameter %s: %s, require_grad = %s' %
                     (name, str(param.size()), str(param.requires_grad)))

    if args.cuda:
        kge_model = kge_model.cuda()

    if args.do_train:
        # Set training dataloader iterator
        # train_dataloader_head = DataLoader(
        #     TrainDataset(train_triples, nentity, nrelation, args.negative_sample_size, 'head-batch'),
        #     batch_size=args.batch_size,
        #     shuffle=True,
        #     num_workers=max(1, args.cpu_num//2),
        #     collate_fn=TrainDataset.collate_fn
        # )

        train_dataloader_tail = DataLoader(
            TrainDataset(train_triples,
                         nentity,
                         nrelation,
                         args.negative_sample_size,
                         'tail-batch',
                         KB=graph),
            batch_size=args.batch_size,
            shuffle=True,
            num_workers=max(1, args.cpu_num // 2),
            collate_fn=TrainDataset.collate_fn)

        # train_iterator = BidirectionalOneShotIterator(train_dataloader_head, train_dataloader_tail)
        train_iterator = OneShotIterator(train_dataloader_tail)

        # Set training configuration
        current_learning_rate = args.learning_rate
        optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad,
                                            kge_model.parameters()),
                                     lr=current_learning_rate)
        if args.warm_up_steps:
            warm_up_steps = args.warm_up_steps
        else:
            warm_up_steps = args.max_steps // 2

    if args.init_checkpoint:
        # Restore model from checkpoint directory
        logging.info('Loading checkpoint %s...' % args.init_checkpoint)
        checkpoint = torch.load(
            os.path.join(args.init_checkpoint, 'checkpoint'))
        init_step = checkpoint['step']
        kge_model.load_state_dict(checkpoint['model_state_dict'])
        if args.do_train:
            current_learning_rate = checkpoint['current_learning_rate']
            warm_up_steps = checkpoint['warm_up_steps']
            optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    else:
        logging.info('Ramdomly Initializing %s Model...' % args.model)
        init_step = 0

    step = init_step

    logging.info('Start Training...')
    logging.info('init_step = %d' % init_step)

    logging.info('batch_size = %d' % args.batch_size)
    logging.info('negative_adversarial_sampling = %d' %
                 args.negative_adversarial_sampling)
    logging.info('hidden_dim = %d' % args.hidden_dim)
    logging.info('gamma = %f' % args.gamma)
    logging.info('negative_adversarial_sampling = %s' %
                 str(args.negative_adversarial_sampling))
    if args.negative_adversarial_sampling:
        logging.info('adversarial_temperature = %f' %
                     args.adversarial_temperature)

    # Set valid dataloader as it would be evaluated during training

    if args.do_train:
        logging.info('learning_rate = %d' % current_learning_rate)

        training_logs = []

        #Training Loop
        for step in range(init_step, args.max_steps):

            log = kge_model.train_step(kge_model, optimizer, train_iterator,
                                       args)

            training_logs.append(log)

            if step >= warm_up_steps:
                current_learning_rate = current_learning_rate / 10
                logging.info('Change learning_rate to %f at step %d' %
                             (current_learning_rate, step))
                optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad,
                                                    kge_model.parameters()),
                                             lr=current_learning_rate)
                warm_up_steps = warm_up_steps * 3

            if step % args.save_checkpoint_steps == 0:
                save_variable_list = {
                    'step': step,
                    'current_learning_rate': current_learning_rate,
                    'warm_up_steps': warm_up_steps
                }
                save_model(kge_model, optimizer, save_variable_list, args)

            if step % args.log_steps == 0:
                metrics = {}
                for metric in training_logs[0].keys():
                    metrics[metric] = sum(
                        [log[metric]
                         for log in training_logs]) / len(training_logs)
                log_metrics('Training average', step, metrics)
                training_logs = []

            if args.do_valid and step % args.valid_steps == 0:
                logging.info('Evaluating on Valid Dataset...')
                metrics = kge_model.test_step(kge_model, valid_triples,
                                              all_true_triples, args)
                log_metrics('Valid', step, metrics)

        save_variable_list = {
            'step': step,
            'current_learning_rate': current_learning_rate,
            'warm_up_steps': warm_up_steps
        }
        save_model(kge_model, optimizer, save_variable_list, args)

    if args.do_valid:
        logging.info('Evaluating on Valid Dataset...')
        metrics = kge_model.test_step(kge_model,
                                      valid_triples,
                                      all_true_triples,
                                      args,
                                      candidate_entities,
                                      id2e=id2entity,
                                      id2rel=id2relationship)
        log_metrics('Valid', step, metrics)

    # if args.do_test:
    #     logging.info('Evaluating on Test Dataset...')
    #     metrics = kge_model.test_step(kge_model, test_triples, all_true_triples, args)
    #     log_metrics('Test', step, metrics)

    if args.do_test:
        logging.info('Evaluating on Test Dataset...')
        metrics = kge_model.test_step(kge_model,
                                      test_triples,
                                      all_true_triples,
                                      args,
                                      candidate_entities,
                                      id2e=id2entity,
                                      id2rel=id2relationship)
        log_metrics('Test', step, metrics)

    if args.evaluate_train:
        logging.info('Evaluating on Training Dataset...')
        metrics = kge_model.test_step(kge_model, train_triples,
                                      all_true_triples, args)
        log_metrics('Test', step, metrics)
Esempio n. 13
0
PJ7 = Boolean('pj7')
PJ8 = Boolean('pj8')
PJ9 = Boolean('pj9')
PJ10 = Boolean('pj10')
PJ11 = Boolean('pj11')
PJ12 = Boolean('pj12')
PJ13 = Boolean('pj13')
PJ14 = Boolean('pj14')
PJ15 = Boolean('pj15')
PJ16 = Boolean('pj16')
PJ17 = Boolean('pj17')
PJ18 = Boolean('pj18')
PJ19 = Boolean('pj19')

# Create a new knowledge base
kb = KB()

# GENERAL INFORMATION ABOUT THE CARDS
# This adds information which cards are Jacks
kb.add_clause(J4)
kb.add_clause(J9)
kb.add_clause(J14)
kb.add_clause(J19)
# Add here whatever is needed for your strategy.
kb.add_clause(J0)
kb.add_clause(J5)
kb.add_clause(J10)
kb.add_clause(J15)
# DEFINITION OF THE STRATEGY
# Add clauses (This list is sufficient for this strategy)
# PJ is the strategy to play jacks first, so all we need to model is all x PJ(x) <-> J(x),
Esempio n. 14
0
import sys
from kb import KB, Boolean, Integer, Constant

# Define our integer symbols
x = Integer('x')
y = Integer('y')

q = x == y
a = x + y > 2
b = x + y < 5
c = x + y < -2
d = x + y > -5

kb = KB()

kb.add_clause(q)
kb.add_clause(c, a)
kb.add_clause(d, a)
kb.add_clause(c, b)
kb.add_clause(d, b)

for model in kb.models():
    print(model)
Esempio n. 15
0
import sys
from kb import KB, Boolean, Integer, Constant

# Define our symbols
P = Boolean('P')
Q = Boolean('Q')
R = Boolean('R')

# Create P new knowledge base
kb = KB()

# KB
kb.add_clause(P, Q)
kb.add_clause(R, ~Q)
kb.add_clause(~P, ~R)

# ~alpha
kb.add_clause(P, ~Q)
kb.add_clause(~P, Q)

# Print all models of the knowledge base
for model in kb.models():
    print(model)

# Print out whether the KB is satisfiable (if there are no models, it is not satisfiable)
print(kb.satisfiable())
Esempio n. 16
0
import sys
from kb import KB, Boolean, Integer, Constant

# Define our integer symbols
x = Integer('x')
y = Integer('y')

q = x == y
a = x + y > 2
b = x + y < 5
c = x + y < -2
d = x + y > -5

kb = KB()

kb.add_clause(q)
kb.add_clause(a, c)
kb.add_clause(a, d)
kb.add_clause(b, c)
kb.add_clause(b, d)

for model in kb.models():
    print(model)

# Print out whether the KB is satisfiable (if there are no models, it is not satisfiable)
print(kb.satisfiable())
Esempio n. 17
0
Pa7 = Boolean('pa7')
Pa8 = Boolean('pa8')
Pa9 = Boolean('pa9')
Pa10 = Boolean('pa10')
Pa11 = Boolean('pa11')
Pa12 = Boolean('pa12')
Pa13 = Boolean('pa13')
Pa14 = Boolean('pa14')
Pa15 = Boolean('pa15')
Pa16 = Boolean('pa16')
Pa17 = Boolean('pa17')
Pa18 = Boolean('pa18')
Pa19 = Boolean('pa19')

# Create a new knowledge base
kb = KB()

# GENERAL INFORMATION ABOUT THE CARDS
# This adds information which cards are aacks
kb.add_clause(a0)
kb.add_clause(a5)
kb.add_clause(a10)
kb.add_clause(a15)
# Add here whatever is needed for your strategy.

# DEFINITION OF THE STRATEGY
# Add clauses (This list is sufficient for this strategy)
# Pa is the strategy to play aces first, so all we need to model is all x Pa(x) <-> a(x),
# In other words that the Pa strategy should play a card when it is an ace
kb.add_clause(~a0, Pa0)
kb.add_clause(~a5, Pa5)
Esempio n. 18
0
import sys
from kb import KB, Boolean, Integer, Constant

# Define our integer symbols
x = Integer('x')
y = Integer('y')

q = x == y
a = x + y > 2
b = x + y < 5
c = x + y < -2
d = x + y > -5

kb = KB()

kb.add_clause(q)
kb.add_clause(a, d)
kb.add_clause(a, c)
kb.add_clause(b, d)
kb.add_clause(b, c)

for model in kb.models():
    print(model)
Esempio n. 19
0
PJ7 = Boolean('pj7')
PJ8 = Boolean('pj8')
PJ9 = Boolean('pj9')
PJ10 = Boolean('pj10')
PJ11 = Boolean('pj11')
PJ12 = Boolean('pj12')
PJ13 = Boolean('pj13')
PJ14 = Boolean('pj14')
PJ15 = Boolean('pj15')
PJ16 = Boolean('pj16')
PJ17 = Boolean('pj17')
PJ18 = Boolean('pj18')
PJ19 = Boolean('pj19')

# Create a new knowledge base
kb = KB()

# GENERAL INFORMATION ABOUT THE CARDS
# This adds information which cards are Jacks
kb.add_clause(J1)
kb.add_clause(J6)
kb.add_clause(J11)
kb.add_clause(J16)
# Add here whatever is needed for your strategy.

# DEFINITION OF THE STRATEGYcb
# Add clauses (This list is sufficient for this strategy)
# PJ is the strategy to play jacks first, so all we need to model is all x PJ(x) <-> J(x),
# In other words that the PJ strategy should play a card when it is a jack
kb.add_clause(~J1, PJ1)
kb.add_clause(~J6, PJ6)
Esempio n. 20
0
PC14 = Boolean('pc14')
PC15 = Boolean('pc15')
PC16 = Boolean('pc16')
PC17 = Boolean('pc17')
PC18 = Boolean('pc18')
PC19 = Boolean('pc19')

#Trump suit
C = Boolean('c')
D = Boolean('d')
H = Boolean('h')
S = Boolean('s')


# Create a new knowledge base
kb = KB()

# GENERAL INFORMATION ABOUT THE CARDS
# This adds information which cards are Kings, Queens and Jacks


kb.add_clause(C2)
kb.add_clause(C3)
kb.add_clause(C4)
kb.add_clause(C7)
kb.add_clause(C8)
kb.add_clause(C9)
kb.add_clause(C12)
kb.add_clause(C13)
kb.add_clause(C14)
kb.add_clause(C17)
Esempio n. 21
0
class ResourcePool(object):
    default_model = "default"

    def split_list(self, word_list):

        # init
        flag = 0
        list_list_word = our_list = []
        for i in word_list:
            if i:
                if i[0].startswith("#"):
                    if flag == 0:
                        flag = 1
                        if our_list:
                            list_list_word = list_list_word + [our_list]
                            our_list = []
                    else:
                        flag = 0
                else:
                    if flag == 0:
                        our_list = our_list + [i]

        list_list_word = list_list_word + [our_list]
        return list_list_word

    def __init__(self):
        """ Empty constructor for the singleton decorator.

        Real initialization must be manually triggered by calling ResourcePool.init().
        """
        self.ontology_server = None

        self.adjectives = {}
        self.irregular_verbs_past = []
        self.irregular_verbs_present = []
        self.preposition_verbs = []
        self.modal = []
        self.adjective_verb = []
        self.special_nouns = []
        self.pronouns = []
        self.numbers = []
        self.demonstrative_det = []
        self.adverbs = []
        self.adverbs_at_end = []
        self.proposals = []
        self.preposition_rdf_object_property = {}
        self.compelement_proposals = []
        self.capital_letters = []
        self.determinants = []
        self.nouns_end_s = []
        self.relatives = []
        self.subsentences = []
        self.det_quantifiers = []
        self.adjective_rules = []
        self.composed_nouns = []
        self.plural_nouns = []
        self.auxiliary = []
        self.direct_transitive = []
        self.indirect_transitive = []
        self.state = []
        self.complement_pronouns = []
        self.concatenate_proposals = []
        self.change_tuples = []
        self.adjective_numbers = []
        self.be_pronoun = []
        self.noun_not_composed = []
        self.adj_quantifiers = []
        self.verb_need_to = []
        self.prep_change_place = []
        self.replace_tuples = []
        self.adjective_numbers_digit = []
        self.days_list = []
        self.months_list = []
        self.time_adverbs = []
        self.location_adverbs = []
        self.unusable_words = []
        self.time_proposals = []
        self.action_verb_with_passive_behaviour = {}
        self.adjectives_ontology_classes = []
        self.special_verbs = []
        self.adv_sub = []

        """list of tokens that can start a sentence"""
        self.sentence_starts = []

        """
        list of verbs that express a goal - ie, that would translate to a
        [S desires O] statement.
        """
        self.goal_verbs = []

        """
        dictionnary of all verbs for which thematic roles are known.
        """
        self.thematic_roles = ThematicRolesDict()

    def init(self, kb_host="localhost", kb_port=6969, embeddedkb=False, defaultontology=None, data_path=None):

        if not data_path:
            # try to guess the current prefix and then the data directory
            data_path = os.path.abspath(__file__).split("lib")[0].split("src")[0] + "share/dialogs/"
            logger.debug("Assuming Dialogs data dir is <%s>" % data_path)

        try:
            from kb import KB, KbError
        except ImportError:
            logger.error(
                "Python bindings to access the knowledge are not available."
                + "Please install 'pykb' and restart Dialogs."
            )

        try:
            self.ontology_server = KB(kb_host, kb_port, embeddedkb, defaultontology)
        except KbError:
            logger.error(
                "Error while trying to connect to the knowledge base on %s:%s" % (kb_host, kb_port)
                + ". Continuing without knowledge base. Amongst others, resolution won't work."
            )

        for line in open(os.path.join(data_path, "adjectives")):
            if line.startswith("#") or not line.strip():
                continue
            try:
                adj, cat = line.split()
            except ValueError:  # for adjectives without category, set a generic "Feature" category
                adj = line.split()[0]
                cat = "Feature"
            self.adjectives[adj] = cat

        verbs = [list(line.split()) for line in open(os.path.join(data_path, "verbs"))]
        verbs = self.split_list(verbs)
        self.irregular_verbs_past = verbs[0]
        self.irregular_verbs_present = verbs[1]
        self.preposition_verbs = verbs[2]
        self.modal = [k[0] for k in verbs[3]]
        self.adjective_verb = [k[0] for k in verbs[4]]
        self.auxiliary = [k[0] for k in verbs[5]]
        self.direct_transitive = [k[0] for k in verbs[6]]
        self.indirect_transitive = [k[0] for k in verbs[7]]
        self.state = [k[0] for k in verbs[8]]
        self.verb_need_to = [k[0] for k in verbs[9]]
        self.special_verbs = [k[0] for k in verbs[12]]

        # Action verbs such as 'see', 'hear' with no active behaviour
        self.action_verb_with_passive_behaviour = dict([(k[0], k[1]) for k in verbs[10]])
        self.goal_verbs = [k[0] for k in verbs[11]]

        self.sentence_starts = [tuple(line.split()) for line in open(os.path.join(data_path, "sentence_starts"))]

        nouns = [list(line.split()) for line in open(os.path.join(data_path, "nouns"))]
        nouns = self.split_list(nouns)
        self.special_nouns = [k[0] for k in nouns[0]]
        self.pronouns = [k[0] for k in nouns[1]]
        for i in nouns[1]:
            if i[1] == "1":
                self.complement_pronouns = self.complement_pronouns + [i[0]]
        self.demonstrative_det = [k[0] for k in nouns[2]]
        self.determinants = [k[0] for k in nouns[3]]
        self.nouns_end_s = [k[0] for k in nouns[4]]
        self.relatives = [k[0] for k in nouns[5]]
        self.composed_nouns = [k[0] for k in nouns[6]]
        self.plural_nouns = nouns[7]
        self.noun_not_composed = [k[0] for k in nouns[8]]
        self.days_list = nouns[9]
        self.months_list = nouns[10]
        self.unusable_words = [k[0] for k in nouns[11]]

        # List of diection words, E.g: LEFT, RIGHT, TOP, etc ...
        self.direction_words = [k[0] for k in nouns[12]]

        self.compound_nouns = nouns[13]

        ###
        ### ADVERBIALS

        adverbials = [list(line.split()) for line in open(os.path.join(data_path, "adverbial"))]
        adverbials = self.split_list(adverbials)
        self.adverbs = [k[0] for k in adverbials[0]]
        self.time_adverbs = adverbials[0]

        self.time_adverbs = [k[0] for k in adverbials[0] if k[1] in ["day", "hour"]]
        self.time_adverbs += [k[0] for k in adverbials[1] if k[1] in ["day", "hour"]]

        self.location_adverbs = [k[0] for k in adverbials[0] if k[1] == "location"]
        self.location_adverbs += [k[0] for k in adverbials[1] if k[1] == "location"]

        self.adverbs_at_end = [k[0] for k in adverbials[1]]

        for k in adverbials[2]:
            if k[1] == "1":
                self.compelement_proposals = self.compelement_proposals + [k[0]]
        self.proposals = [k[0] for k in adverbials[2]]

        # Preposition with an existing object_property
        # E.g: next+to => isNextTo
        self.preposition_rdf_object_property = dict([(k[0], k[3:]) for k in adverbials[2]])
        self.time_proposals = adverbials[2]
        self.subsentences = [k[0] for k in adverbials[3]]
        for k in adverbials[3]:
            if k[1] == "1":
                self.adv_sub = self.adv_sub + [k[0]]
        self.prep_change_place = [k[0] for k in adverbials[4]]

        grammatical_rules = [list(line.split()) for line in open(os.path.join(data_path, "grammatical_rules"))]
        grammatical_rules = self.split_list(grammatical_rules)
        self.numbers = grammatical_rules[0]
        self.det_quantifiers = grammatical_rules[1]
        self.capital_letters = [k[0] for k in grammatical_rules[2]]
        self.adjective_rules = [k[0] for k in grammatical_rules[3]]
        self.concatenate_proposals = grammatical_rules[4]
        self.change_tuples = grammatical_rules[5]
        self.adjective_numbers_digit = grammatical_rules[6]
        self.adjective_numbers = [k[0] for k in grammatical_rules[6]]
        self.be_pronoun = [k[0] for k in grammatical_rules[7]]
        self.adj_quantifiers = [k[0] for k in grammatical_rules[8]]
        for k in grammatical_rules[9]:
            self.replace_tuples = self.replace_tuples + [[k[0], k[1:]]]

        desc = ""
        for line in open(os.path.join(data_path, "thematic_roles")):
            if line.startswith("#") or not line.strip():
                continue

            desc += line

            if line.startswith("}"):  # end of block
                self.thematic_roles.add_verb(desc)
                desc = ""

        # Add action verbs to the ontology
        if self.ontology_server:
            stmts = [
                verb.capitalize() + " rdfs:subClassOf cyc:PurposefulAction"
                for verb in self.thematic_roles.verbs.keys()
                if not self.thematic_roles.verbs[verb].is_synonym()
            ]
            self.ontology_server.revise(stmts, {"method": "add"})

        """
            List of ontology classes that are used in the adjectives list
        """
        self.adjectives_ontology_classes = [self.adjectives[adj].lower() for adj in self.adjectives]
        adj_s = []
        for k in self.adjectives_ontology_classes:
            if not k in adj_s:
                adj_s.append(k)
        self.adjectives_ontology_classes = adj_s

    def mark_active(self, ids):
        if not self.ontology_server:
            return

        if isinstance(ids, basestring):
            ids = [ids]

        try:
            self.ontology_server.revise(
                [id + " rdf:type ActiveConcept" for id in ids],
                {"method": "add", "models": [ResourcePool().default_model], "lifespan": 10},
            )
        except AttributeError:
            # No ontology server
            pass

    def __del__(self):
        self.close()

    def close(self):
        if self.ontology_server:
            self.ontology_server.close()
            self.ontology_server = None
import sys
from kb import KB, Boolean, Integer, Constant

# Define our symbols
A = Boolean('A')
B = Boolean('B')
C = Boolean('C')
D = Boolean('D')

# Create a new knowledge base
kb = KB()

# Add clauses
# kb.add_clause(A, B, C)
# kb.add_clause(~A, B)
# kb.add_clause(C)
# kb.add_clause(C)

# # Question 3
# kb.add_clause(A)
# kb.add_clause(C)
# kb.add_clause(D)
# kb.add_clause(~A)

# Question 4
kb.add_clause(A, B)
kb.add_clause(B, ~C)
kb.add_clause(~C, ~A)

# This needs to be unsatisfiable for entailment
kb.add_clause(A, ~B)
Esempio n. 23
0
    def init(self, kb_host="localhost", kb_port=6969, embeddedkb=False, defaultontology=None, data_path=None):

        if not data_path:
            # try to guess the current prefix and then the data directory
            data_path = os.path.abspath(__file__).split("lib")[0].split("src")[0] + "share/dialogs/"
            logger.debug("Assuming Dialogs data dir is <%s>" % data_path)

        try:
            from kb import KB, KbError
        except ImportError:
            logger.error(
                "Python bindings to access the knowledge are not available."
                + "Please install 'pykb' and restart Dialogs."
            )

        try:
            self.ontology_server = KB(kb_host, kb_port, embeddedkb, defaultontology)
        except KbError:
            logger.error(
                "Error while trying to connect to the knowledge base on %s:%s" % (kb_host, kb_port)
                + ". Continuing without knowledge base. Amongst others, resolution won't work."
            )

        for line in open(os.path.join(data_path, "adjectives")):
            if line.startswith("#") or not line.strip():
                continue
            try:
                adj, cat = line.split()
            except ValueError:  # for adjectives without category, set a generic "Feature" category
                adj = line.split()[0]
                cat = "Feature"
            self.adjectives[adj] = cat

        verbs = [list(line.split()) for line in open(os.path.join(data_path, "verbs"))]
        verbs = self.split_list(verbs)
        self.irregular_verbs_past = verbs[0]
        self.irregular_verbs_present = verbs[1]
        self.preposition_verbs = verbs[2]
        self.modal = [k[0] for k in verbs[3]]
        self.adjective_verb = [k[0] for k in verbs[4]]
        self.auxiliary = [k[0] for k in verbs[5]]
        self.direct_transitive = [k[0] for k in verbs[6]]
        self.indirect_transitive = [k[0] for k in verbs[7]]
        self.state = [k[0] for k in verbs[8]]
        self.verb_need_to = [k[0] for k in verbs[9]]
        self.special_verbs = [k[0] for k in verbs[12]]

        # Action verbs such as 'see', 'hear' with no active behaviour
        self.action_verb_with_passive_behaviour = dict([(k[0], k[1]) for k in verbs[10]])
        self.goal_verbs = [k[0] for k in verbs[11]]

        self.sentence_starts = [tuple(line.split()) for line in open(os.path.join(data_path, "sentence_starts"))]

        nouns = [list(line.split()) for line in open(os.path.join(data_path, "nouns"))]
        nouns = self.split_list(nouns)
        self.special_nouns = [k[0] for k in nouns[0]]
        self.pronouns = [k[0] for k in nouns[1]]
        for i in nouns[1]:
            if i[1] == "1":
                self.complement_pronouns = self.complement_pronouns + [i[0]]
        self.demonstrative_det = [k[0] for k in nouns[2]]
        self.determinants = [k[0] for k in nouns[3]]
        self.nouns_end_s = [k[0] for k in nouns[4]]
        self.relatives = [k[0] for k in nouns[5]]
        self.composed_nouns = [k[0] for k in nouns[6]]
        self.plural_nouns = nouns[7]
        self.noun_not_composed = [k[0] for k in nouns[8]]
        self.days_list = nouns[9]
        self.months_list = nouns[10]
        self.unusable_words = [k[0] for k in nouns[11]]

        # List of diection words, E.g: LEFT, RIGHT, TOP, etc ...
        self.direction_words = [k[0] for k in nouns[12]]

        self.compound_nouns = nouns[13]

        ###
        ### ADVERBIALS

        adverbials = [list(line.split()) for line in open(os.path.join(data_path, "adverbial"))]
        adverbials = self.split_list(adverbials)
        self.adverbs = [k[0] for k in adverbials[0]]
        self.time_adverbs = adverbials[0]

        self.time_adverbs = [k[0] for k in adverbials[0] if k[1] in ["day", "hour"]]
        self.time_adverbs += [k[0] for k in adverbials[1] if k[1] in ["day", "hour"]]

        self.location_adverbs = [k[0] for k in adverbials[0] if k[1] == "location"]
        self.location_adverbs += [k[0] for k in adverbials[1] if k[1] == "location"]

        self.adverbs_at_end = [k[0] for k in adverbials[1]]

        for k in adverbials[2]:
            if k[1] == "1":
                self.compelement_proposals = self.compelement_proposals + [k[0]]
        self.proposals = [k[0] for k in adverbials[2]]

        # Preposition with an existing object_property
        # E.g: next+to => isNextTo
        self.preposition_rdf_object_property = dict([(k[0], k[3:]) for k in adverbials[2]])
        self.time_proposals = adverbials[2]
        self.subsentences = [k[0] for k in adverbials[3]]
        for k in adverbials[3]:
            if k[1] == "1":
                self.adv_sub = self.adv_sub + [k[0]]
        self.prep_change_place = [k[0] for k in adverbials[4]]

        grammatical_rules = [list(line.split()) for line in open(os.path.join(data_path, "grammatical_rules"))]
        grammatical_rules = self.split_list(grammatical_rules)
        self.numbers = grammatical_rules[0]
        self.det_quantifiers = grammatical_rules[1]
        self.capital_letters = [k[0] for k in grammatical_rules[2]]
        self.adjective_rules = [k[0] for k in grammatical_rules[3]]
        self.concatenate_proposals = grammatical_rules[4]
        self.change_tuples = grammatical_rules[5]
        self.adjective_numbers_digit = grammatical_rules[6]
        self.adjective_numbers = [k[0] for k in grammatical_rules[6]]
        self.be_pronoun = [k[0] for k in grammatical_rules[7]]
        self.adj_quantifiers = [k[0] for k in grammatical_rules[8]]
        for k in grammatical_rules[9]:
            self.replace_tuples = self.replace_tuples + [[k[0], k[1:]]]

        desc = ""
        for line in open(os.path.join(data_path, "thematic_roles")):
            if line.startswith("#") or not line.strip():
                continue

            desc += line

            if line.startswith("}"):  # end of block
                self.thematic_roles.add_verb(desc)
                desc = ""

        # Add action verbs to the ontology
        if self.ontology_server:
            stmts = [
                verb.capitalize() + " rdfs:subClassOf cyc:PurposefulAction"
                for verb in self.thematic_roles.verbs.keys()
                if not self.thematic_roles.verbs[verb].is_synonym()
            ]
            self.ontology_server.revise(stmts, {"method": "add"})

        """
            List of ontology classes that are used in the adjectives list
        """
        self.adjectives_ontology_classes = [self.adjectives[adj].lower() for adj in self.adjectives]
        adj_s = []
        for k in self.adjectives_ontology_classes:
            if not k in adj_s:
                adj_s.append(k)
        self.adjectives_ontology_classes = adj_s
Esempio n. 24
0
from kb import KB

# 在主程式中直接寫知識庫
code = "A<=B. B<=C&D. C<=E. D<=F. E. F. Z<=C&D&G."
kb1 = KB()
kb1.load(code) # 載入知識庫
kb1.forwardChaining() # 開始進行前向推論
Esempio n. 25
0
import sys
from kb import KB, Boolean, Integer, Constant

# Define our symbols
A = Boolean('A')
B = Boolean('B')
C = Boolean('C')

# Create a new knowledge base
kb = KB()

# Add clauses
kb.add_clause(A, B, C)
kb.add_clause(~A, B)
kb.add_clause(~B, C)
kb.add_clause(B, ~C)

# Print all models of the knowledge base
for model in kb.models():
    print(model)

# Print out whether the KB is satisfiable (if there are no models, it is not satisfiable)
print(kb.satisfiable())
Esempio n. 26
0
class Tracker(object):
    """
  This is the main program which gives a high-level view
  of all the running subsystems. It connects camera input with
  output in form of "actions" (such as keyboard shortcuts on the users behalf).
  This is done by locating a hand in an image and detecting features,
  like the number of fingers, and trying to match that data with a
  known gesture.
  """
    def __init__(self):
        """
    Configuration
    """

        # Camera settings
        self.FRAME_WIDTH = 341
        self.FRAME_HEIGHT = 256
        self.flip_camera = True  # Mirror image
        self.camera = cv2.VideoCapture(1)

        # ...you can also use a test video for input
        #video = "/Users/matthiasendler/Code/snippets/python/tracker/final/assets/test_video/10.mov"
        #self.camera = cv2.VideoCapture(video)
        #self.skip_input(400) # Skip to an interesting part of the video

        if not self.camera.isOpened():
            print "couldn't load webcam"
            return
        #self.camera.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, self.FRAME_WIDTH)
        #self.camera.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, self.FRAME_HEIGHT)

        self.filters_dir = "filters/"  # Filter settings in trackbar
        self.filters_file = "filters_default"

        # Load filter settings
        current_config = self.filters_dir + self.filters_file
        self.filters = Filters(current_config)

        # No actions will be triggered in test mode
        # (can be used to adjust settings at runtime)
        self.test_mode = False

        # Create a hand detector
        # In fact, this is a wrapper for many detectors
        # to increase detection confidence
        self.detector = Detector(self.filters.config)

        # Knowledge base for all detectors
        self.kb = KB()
        # Create gesture recognizer.
        # A gesture consists of a motion and a hand state.
        self.gesture = Gesture()

        # The action module executes keyboard and mouse commands
        self.action = Action()

        # Show output of detectors
        self.output = Output()

        self.run()

    def run(self):
        """
    In each step: Read the input image and keys,
    process it and react on it (e.g. with an action).
    """
        while True:
            img = self.get_input()
            hand = self.process(img)
            ref = self.action.get_reference_point()
            self.output.show(img, hand, ref)

    def process(self, img):
        """
    Process input
    """
        # Run detection
        hand = self.detector.detect(img)
        # Store result in knowledge base
        self.kb.update(hand)
        if not self.test_mode:
            # Try to interprete as gesture
            self.interprete(hand)
        return hand

    def interprete(self, hand):
        """
    Try to interprete the input as a gesture
    """
        self.gesture.add_hand(hand)
        operation = self.gesture.detect_gesture()
        self.action.execute(operation)

    def get_input(self):
        """
    Get input from camera and keyboard
    """
        self.get_key()
        _, img = self.camera.read()
        img = cv2.resize(img, (self.FRAME_WIDTH, self.FRAME_HEIGHT))
        if self.flip_camera:
            img = cv2.flip(img, 1)
        return img

    def get_key(self):
        """
    Read keyboard input
    """
        key = cv2.waitKey(self.filters.config["wait_between_frames"])
        if key == ord('+'):
            # Reduce program speed
            self.filters.config["wait_between_frames"] += 500
        if key == ord('-'):
            # Increase program speed
            if self.filters.config["wait_between_frames"] >= 500:
                self.filters.config["wait_between_frames"] -= 500
        #if key == ord('s'):
        # Save config
        #  self.filters.save()
        if key == ord('r'):
            # Reset all detectors
            self.detector.reset()
            self.action.reset()
        if key == ord('d'):
            # Make a screenshot
            self.output.make_screenshot()
        if key == ord('p') or key == ord(' '):
            # Pause
            cv2.waitKey()
        if key == ord('t'):
            # Test mode
            self.test_mode = not self.test_mode
        if key == ord('1'):
            self.output.toggle_estimate()
        if key == ord('2'):
            self.output.toggle_detectors()
        if key == ord('3'):
            self.output.toggle_skin()
        if key == ord('f'):
            self.toggle_filters()
        if key == 63235:  # Right arrow
            self.skip_input(20)
        if key == 27 or key == ord('q'):
            # Abort program on ESC, q or space
            exit()

    def toggle_filters(self):
        """
    Load the next filter settings
    """
        self.filters_file = self.next_filters_file()
        current_config = self.filters_dir + self.filters_file
        self.filters.set_config(current_config)

    def next_filters_file(self):
        """
    Get the next filter settings
    """
        filters = listdir(self.filters_dir)
        for i, f in enumerate(filters):
            if f == self.filters_file:
                return filters[(i + 1) % len(filters)]

    def skip_input(self, x=1):
        """
    Skip to a different part of a video sequence.
    """
        for i in range(0, x):
            self.camera.grab()
Esempio n. 27
0
                        '-p',
                        type=str,
                        default='',
                        help='file name for loading a saved knowledge base')
    parser.add_argument('--save_kb',
                        '-s',
                        type=str,
                        default='',
                        help='file name to save the knowledge base to')
    parser.add_argument('--responses',
                        '-r',
                        type=str,
                        default='',
                        help='file name for stored agent responses')
    parser.add_argument('--output',
                        '-o',
                        type=str,
                        default='',
                        help='file name for output')
    args = parser.parse_args()
    kb = KB(args.responses)
    print(
        'Input looks like:\n\tA(n) <word1> is a(n) <word2>.\n\tA(n) <word1> is not a(n) <word2>.\n'
        +
        '\t<proper noun> is a <word1>.\n\t<proper noun> is not a <word1>.\n\t'
        +
        'Is a(n) <word1> a(n) <word2>?\n\tIs <proper noun> a <word1>?\n\tIs a <word1> <proper noun>?'
    )
    print('Type \'quit\' or \'q\' to exit.\n')
    Shell(kb).start(args.input, args.prior_kb, args.save_kb, args.output)
Esempio n. 28
0
    def kb_consistent_trumpmarriage(self, state, move):
        # type: (State, move) -> bool

        # each time we check for consistency we initialise a new knowledge-base
        kb = KB()

        # Add general information about the game

        suit = State.get_trump_suit(state)

        if suit == "C":
            card1 = 2
            card2 = 3
            variable_string = "m" + str(card1) + str(card2)
            strategy_variable = Boolean(variable_string)
            kb.add_clause(strategy_variable)
        elif suit == "D":
            card1 = 7
            card2 = 8
            variable_string = "m" + str(card1) + str(card2)
            strategy_variable = Boolean(variable_string)
            kb.add_clause(strategy_variable)
        elif suit == "H":
            card1 = 12
            card2 = 13
            variable_string = "m" + str(card1) + str(card2)
            strategy_variable = Boolean(variable_string)
            kb.add_clause(strategy_variable)
        elif suit == "S":
            card1 = 17
            card2 = 18
            variable_string = "m" + str(card1) + str(card2)
            strategy_variable = Boolean(variable_string)
            kb.add_clause(strategy_variable)

        load.general_information(kb)

        # Add the necessary knowledge about the strategy
        load.strategy_knowledge(kb)

        # This line stores the index of the card in the deck.
        # If this doesn't make sense, refer to _deck.py for the card index mapping
        index = move[0]

        variable_string = "pm" + str(index)
        strategy_variable = Boolean(variable_string)

        # Add the relevant clause to the loaded knowledge base
        kb.add_clause(~strategy_variable)

        # If the knowledge base is not satisfiable, the strategy variable is
        # entailed (proof by refutation)

        return kb.satisfiable()
Esempio n. 29
0
import sys
from kb import KB, Boolean, Integer, Constant

# Define our symbols
A = Boolean('A')
B = Boolean('B')
C = Boolean('C')
D = Boolean('D')

# Create a new knowledge base
kb = KB()

# Add clauses
kb.add_clause(A, B)
kb.add_clause(~B, A)
kb.add_clause(~A, C)
kb.add_clause(~A, D)
kb.add_clause(~A, ~C, ~D)

# Print all models of the knowledge base
for model in kb.models():
    print(model)

# Print out whether the KB is satisfiable (if there are no models, it is not satisfiable)
print(kb.satisfiable())
Esempio n. 30
0
import sys
from kb import KB

kb1 = KB()
with open(sys.argv[1], encoding='utf-8') as file:
    code = file.read().replace(r'\n', '')

kb1.load(code)
kb1.forwardChaining()

while True:
    line = input('?-')
    if line == 'exit':
        break
    kb1.addFact(line.strip())
    kb1.forwardChaining()
Esempio n. 31
0
PC7 = Boolean('pc7')
PC8 = Boolean('pc8')
PC9 = Boolean('pc9')
PC10 = Boolean('pc10')
PC11 = Boolean('pc11')
PC12 = Boolean('pc12')
PC13 = Boolean('pc13')
PC14 = Boolean('pc14')
PC15 = Boolean('pc15')
PC16 = Boolean('pc16')
PC17 = Boolean('pc17')
PC18 = Boolean('pc18')
PC19 = Boolean('pc19')

# Create a new knowledge base
kb = KB()

# strategy: plays cheap card first (J, Q, K)
# This adds information which cards are Cheap
kb.add_clause(C2)
kb.add_clause(C3)
kb.add_clause(C4)
kb.add_clause(C7)
kb.add_clause(C8)
kb.add_clause(C9)
kb.add_clause(C12)
kb.add_clause(C13)
kb.add_clause(C14)
kb.add_clause(C17)
kb.add_clause(C18)
kb.add_clause(C19)