當前位置: 首頁>>代碼示例>>Python>>正文


Python nltk.ChartParser方法代碼示例

本文整理匯總了Python中nltk.ChartParser方法的典型用法代碼示例。如果您正苦於以下問題:Python nltk.ChartParser方法的具體用法?Python nltk.ChartParser怎麽用?Python nltk.ChartParser使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在nltk的用法示例。


在下文中一共展示了nltk.ChartParser方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: to_one_hot

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import ChartParser [as 別名]
def to_one_hot(smiles, MaxNumSymbols, check=True):
    """ Encode a list of smiles strings to one-hot vectors """
    assert type(smiles) == list
    prod_map = {}
    for ix, prod in enumerate(zinc_grammar.GCFG.productions()):
        prod_map[prod] = ix
    tokenize = get_zinc_tokenizer(zinc_grammar.GCFG)
    tokens = list(map(tokenize, smiles))
    parser = nltk.ChartParser(zinc_grammar.GCFG)
    parse_trees = [next(parser.parse(t)) for t in tokens]
    productions_seq = [tree.productions() for tree in parse_trees]
    
    #if check:
    #    print(productions_seq)
        
    indices = [np.array([prod_map[prod] for prod in entry], dtype=int) for entry in productions_seq]
    one_hot = np.zeros((len(indices), MaxNumSymbols, NCHARS), dtype=np.float32)
    for i in range(len(indices)):
        num_productions = len(indices[i])
        one_hot[i][np.arange(num_productions),indices[i]] = 1.
        one_hot[i][np.arange(num_productions, MaxNumSymbols),-1] = 1.
    return one_hot 
開發者ID:aspuru-guzik-group,項目名稱:selfies,代碼行數:24,代碼來源:GrammarVAE_codes.py

示例2: encode

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import ChartParser [as 別名]
def encode(smiles):
    GCFG = smiles_grammar.GCFG
    tokenize = get_smiles_tokenizer(GCFG)
    tokens = tokenize(smiles)
    parser = nltk.ChartParser(GCFG)
    parse_tree = parser.parse(tokens).__next__()
    productions_seq = parse_tree.productions()
    productions = GCFG.productions()
    prod_map = {}
    for ix, prod in enumerate(productions):
        prod_map[prod] = ix
    indices = np.array([prod_map[prod] for prod in productions_seq], dtype=int)
    return indices 
開發者ID:BenevolentAI,項目名稱:guacamol_baselines,代碼行數:15,代碼來源:cfg_util.py

示例3: parse

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import ChartParser [as 別名]
def parse(sent):
    parser = nltk.ChartParser(grammar)
    tokens = nltk.wordpunct_tokenize(sent)
    return parser.parse(tokens) 
開發者ID:foxbook,項目名稱:atap,代碼行數:6,代碼來源:parse.py

示例4: load

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import ChartParser [as 別名]
def load(self, filepath):
        cfg_string = ''.join(list(open(filepath).readlines()))

        # parse from nltk
        cfg_grammar = nltk.CFG.fromstring(cfg_string)
        # self.cfg_parser = cfg_parser = nltk.RecursiveDescentParser(cfg_grammar)
        self.cfg_parser = cfg_parser = nltk.ChartParser(cfg_grammar)


        # our info for rule macthing
        self.head_to_rules = head_to_rules = {}
        self.valid_tokens = valid_tokens = set()
        rule_ranges = {}
        total_num_rules = 0
        first_head = None
        for line in cfg_string.split('\n'):
            if len(line.strip()) > 0:
                head, rules = line.split('->')
                head = Nonterminal(head.strip())  # remove space
                rules = [_.strip() for _ in rules.split('|')]  # split and remove space
                rules = [tuple([Nonterminal(_) if not _.startswith("'") else _[1:-1] for _ in rule.split()]) for rule in rules]
                head_to_rules[head] = rules

                for rule in rules:
                    for t in rule:
                        if isinstance(t, str):
                            valid_tokens.add(t)

                if first_head is None:
                    first_head = head

                rule_ranges[head] = (total_num_rules, total_num_rules + len(rules))
                total_num_rules += len(rules)

        self.first_head = first_head

        self.rule_ranges = rule_ranges
        self.total_num_rules = total_num_rules 
開發者ID:Hanjun-Dai,項目名稱:sdvae,代碼行數:40,代碼來源:cfg_parser.py

示例5: get_parser

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import ChartParser [as 別名]
def get_parser(production_file):
    prods = [_.strip() for _ in open(production_file).readlines()] + ['Nothing -> None']
    string = '\n'.join(prods)
    GCFG = nltk.CFG.fromstring(string)
    parser = nltk.ChartParser(GCFG)
    return parser 
開發者ID:Hanjun-Dai,項目名稱:sdvae,代碼行數:8,代碼來源:evaluate.py

示例6: load

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import ChartParser [as 別名]
def load(self, filepath):
        cfg_string = ''.join(list(open(filepath).readlines()))

        # parse from nltk
        cfg_grammar = nltk.CFG.fromstring(cfg_string)
        # self.cfg_parser = cfg_parser = nltk.RecursiveDescentParser(cfg_grammar)
        self.cfg_parser = cfg_parser = nltk.ChartParser(cfg_grammar)

        # our info for rule macthing
        self.head_to_rules = head_to_rules = {}
        self.valid_tokens = valid_tokens = set()
        rule_ranges = {}
        total_num_rules = 0
        first_head = None
        for line in cfg_string.split('\n'):
            if len(line.strip()) > 0:
                head, rules = line.split('->')
                head = Nonterminal(head.strip())    # remove space
                rules = [_.strip() for _ in rules.split('|')]    # split and remove space
                rules = [
                    tuple([Nonterminal(_) if not _.startswith("'") else _[1:-1] for _ in rule.split()])
                    for rule in rules
                ]
                head_to_rules[head] = rules

                for rule in rules:
                    for t in rule:
                        if isinstance(t, str):
                            valid_tokens.add(t)

                if first_head is None:
                    first_head = head

                rule_ranges[head] = (total_num_rules, total_num_rules + len(rules))
                total_num_rules += len(rules)

        self.first_head = first_head

        self.rule_ranges = rule_ranges
        self.total_num_rules = total_num_rules 
開發者ID:Hanjun-Dai,項目名稱:sdvae,代碼行數:42,代碼來源:cfg_parser.py

示例7: SizeOneHot

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import ChartParser [as 別名]
def SizeOneHot(smiles, check=True):
    """ Encode a list of smiles strings to one-hot vectors """
    assert type(smiles) == list
    prod_map = {}
    for ix, prod in enumerate(zinc_grammar.GCFG.productions()):
        prod_map[prod] = ix
    tokenize = get_zinc_tokenizer(zinc_grammar.GCFG)
    tokens = list(map(tokenize, smiles))
    parser = nltk.ChartParser(zinc_grammar.GCFG)
    parse_trees = [next(parser.parse(t)) for t in tokens]
    productions_seq = [tree.productions() for tree in parse_trees]
    
    indices = [np.array([prod_map[prod] for prod in entry], dtype=int) for entry in productions_seq]
    return len(indices[0])


# SINGLE EXAMPLE
#smile = [L[0]]
##smile = ['C']
#one_hot_single =  to_one_hot(smile, )
#print(one_hot_single.shape)
#print(one_hot_single)


# GOING THROUGH ALL OF ZINC....

#OH = np.zeros((len(L),MAX_LEN,NCHARS))
#for i in range(0, len(L), 100):
#    print('Processing: i=[' + str(i) + ':' + str(i+100) + ']')
#    onehot = to_one_hot(L[i:i+100], False)
#    OH[i:i+100,:,:] = onehot
#
#h5f = h5py.File('zinc_grammar_dataset.h5','w')
#h5f.create_dataset('data', data=OH)
#h5f.close() 
開發者ID:aspuru-guzik-group,項目名稱:selfies,代碼行數:37,代碼來源:GrammarVAE_codes.py

示例8: __init__

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import ChartParser [as 別名]
def __init__(self, cfg_grammar=None, test_file=None):
        super().__init__()
        self.name = 'cfg'
        if cfg_grammar is None:
            cfg_grammar = """
              S -> S PLUS x | S SUB x |  S PROD x | S DIV x | x | '(' S ')'
              PLUS -> '+'
              SUB -> '-'
              PROD -> '*'
              DIV -> '/'
              x -> 'x' | 'y'
            """
        self.grammar = nltk.CFG.fromstring(cfg_grammar)
        self.parser = nltk.ChartParser(self.grammar)
        self.test_file = test_file 
開發者ID:geek-ai,項目名稱:Texygen,代碼行數:17,代碼來源:Cfg.py


注:本文中的nltk.ChartParser方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。