Пример #1
0
 def model_plan(self, g, greedy=True):
     predict = list(
         self.executor.predict([self.convert_graph(g)], greedy=greedy))[0]
     plan = " ".join(chain.from_iterable(predict))
     for d, r in get_relations(plan):
         plan = plan.replace(d + " " + r, d + " " + r.replace("_", " "))
     return plan
Пример #2
0
    def fix_out(self, plan: str):
        if not plan:
            return None

        for d, r in get_relations(plan):
            plan = plan.replace(d + ' ' + r + ' [ ', d + '_' + r + '_')

        return plan.split(" ")
Пример #3
0
 def eval(self, plan: str):
     matches = get_relations(plan)
     scores = []
     for match in matches:
         relation = "UNK" if match[1] not in self.probs else match[1]
         scores.append(self.probs[relation] if match[0] == ">" else (
             1 - self.probs[relation]))
     return scores
Пример #4
0
    def convert_plan(self, p: str):
        relations = get_relations(p)
        for d, r in relations:
            p = p.replace(r, self.convert_relation(r))

        while "]]" in p:
            p = p.replace("]]", "] ]")

        p = p.replace("].", "] .")

        return re.sub("\[(\w)", r"[ \1", p)
Пример #5
0
    def __init__(self, plans: List[str]):
        split = defaultdict(Counter)

        for plan in plans:
            matches = get_relations(plan)
            split[len(matches)][self.split(plan)] += 1

        self.probs = {}
        for e, c in split.items():
            total = sum([x for x in c.values()]) + 1  # Smoothing
            self.probs[e] = {p: n / total for p, n in c.items()}
            self.probs[e]["UNK"] = 1 / total
Пример #6
0
    def eval(self, plan: str):
        matches = get_relations(plan)
        split = self.split(plan)

        relations = len(matches)

        if relations not in self.probs:
            return 1  # Never encountered such size

        if split not in self.probs[relations]:
            return self.probs[relations]["UNK"]  # Never encountered such split

        return self.probs[relations][split]
Пример #7
0
    def __init__(self, plans: List[str]):
        direction = defaultdict(Counter)

        for plan in plans:
            matches = get_relations(plan)
            forward = len(list(filter(lambda a: a[0] == ">", matches)))

            direction[len(matches)][forward / (len(matches) + 1)] += 1

        self.probs = {}
        for e, c in direction.items():
            total = sum([x for x in c.values()]) + 1  # Smoothing
            self.probs[e] = {p: n / total for p, n in c.items()}
            self.probs[e]["UNK"] = 1 / total
Пример #8
0
    def eval(self, plan: str):
        matches = get_relations(plan)
        forward = len(list(filter(lambda a: a[0] == ">", matches)))

        relations = len(matches)
        direction = forward / (relations + 1)

        if relations not in self.probs:
            return 1  # Never encountered such size

        if direction not in self.probs[relations]:
            return self.probs[relations][
                "UNK"]  # Never encountered such percentage

        return self.probs[relations][direction]
Пример #9
0
    def __init__(self, plans: List[str]):
        adjacent = defaultdict(Counter)

        for plan in plans:
            for p in plan.split("."):
                matches = get_relations(p)

                for i in range(len(matches) - 1):
                    adjacent[matches[i][1]][matches[i + 1][1]] += 1
                if len(matches) > 0:
                    adjacent[matches[-1][1]]["EOS"] += 1

        self.probs = {}
        for e, c in adjacent.items():
            total = sum([x for x in c.values()]) + 1  # Smoothing
            self.probs[e] = {p: n / total for p, n in c.items()}
            self.probs[e]["UNK"] = 1 / total
Пример #10
0
    def __init__(self, plans: List[str]):
        matches = get_relations("\n".join(plans))

        direction = {}

        for match in matches:
            if match[1] not in direction:
                direction[match[1]] = {"f": 0, "b": 0}
            direction[match[1]]["f" if match[0] == ">" else "b"] += 1

        self.probs = {
            e: d["f"] / (d["f"] + d["b"])
            for e, d in direction.items()
        }
        self.probs = {e: d if d != 1 else 0.999 for e, d in self.probs.items()}
        self.probs = {e: d if d != 0 else 0.001 for e, d in self.probs.items()}
        self.probs["UNK"] = np.mean(list(self.probs.values()))
Пример #11
0
    def eval(self, plan: str):
        def get_prob(r1, r2):
            if r1 not in self.probs:
                return 1  # Never encountered such edge

            if r2 not in self.probs[r1]:
                return self.probs[r1][
                    "UNK"]  # Never encountered such adjacency

            return self.probs[r1][r2]

        scores = []

        for p in plan.split("."):
            matches = get_relations(p)

            for i in range(len(matches) - 1):
                scores.append(get_prob(matches[i][1], matches[i + 1][1]))
            scores.append(get_prob(matches[-1][1], "EOS"))

        return scores
Пример #12
0
 def convert_plan(self, p: str):
     relations = get_relations(p)
     for d, r in relations:
         p = p.replace(r, self.convert_relation(r))
     return p
Пример #13
0
 def split(self, plan):
     return "-".join([str(len(get_relations(p))) for p in plan.split(".")])