Exemple #1
0
import math
import pprint
import torch
import torch.nn as nn
import torch.nn.functional as F
from pcfg import PCFG


def p(s):
    print(s)
    return s


gen_a = lambda terminals_per_category: PCFG.fromstring(
    " A    -> " + " | ".join([
        "\"a" + str(i) + "\" [" + str(1 / terminals_per_category) + "]"
        for i in range(terminals_per_category)
    ]))

generate_test_grammar = lambda p1, terminals_per_category: PCFG.fromstring(
    " S  -> NP-s VP-s [" + str((1 - p1) / 2) + "] | NP-s S VP-s [" + str(
        p1 / 2) + "] | NP-p VP-p [" + str(
            (1 - p1) / 2) + "] | NP-p S VP-p [" + str(p1 / 2) + "]\n" +
    " NP-s -> N-s     [" + str(1) + "]\n" + " NP-p -> N-p     [" + str(1) +
    "]\n" + " VP-s -> V-s     [" + str(1) + "]\n" + " VP-p -> V-p     [" + str(
        1) + "]\n" + " N-s  -> " + " | ".join([
            "\"n" + str(i) + "-s\" [" + str(1 / terminals_per_category) + "]"
            for i in range(terminals_per_category)
        ]) + "\n" + " N-p  -> " + " | ".join([
            "\"n" + str(i) + "-p\" [" + str(1 / terminals_per_category) + "]"
            for i in range(terminals_per_category)
Exemple #2
0
from typing import TextIO

from pcfg import PCFG
import os

BASEPATH: str = os.path.dirname(__file__)

f: TextIO
with open(os.path.join(BASEPATH, "subject_adjectives.txt")) as f:
    subject_adjectives: PCFG = PCFG.fromstring(f.read())

n: int = int(input("How many sentences do you want generated? "))
sentence: str
for sentence in subject_adjectives.generate(n):
    print()
    print(sentence.capitalize())