给定NLTK语法,如何使用整数数组表示句子?
我正在使用NLTK从特定语法生成一些句子。我想生成一个整数数组来表示生成的句子(表型)的基因组。
有了整数的表示,我将在遗传算法中进化基因组,执行一些突变以获得更好的句子。
例如,
from nltk import CFG
from nltk.parse.generate import generate, demo_grammar
g = CFG.fromstring(demo_grammar)
sentence = next(generate(g, n=1))
print(sentence) # ex: ['the', 'man', 'saw', 'the', 'park']
convert_to_genotype(sentence) # returns [253, 69, 221, 97, 190, 254, 67, 137, 95, 72, 54, 232, 11, 136] for example.
如何创建convert_to_genotype
函数?
经过一些研究,我创建了一个实现,为给定的基因组创建表型。这就是我想要的,通过语法规则来进化我的个体。
import nltk
from nltk import CFG
GRAMMAR = CFG.fromstring("""
string -> letter | letter string
letter -> vowel | consonant | char
char -> ' '|'!'|'?'|','|'.'
vowel -> lower_vowel | upper_vowel
lower_vowel -> 'a'|'e'|'o'|'i'|'u'
upper_vowel -> 'A'|'E'|'I'|'O'|'U'
consonant -> lower_consonant | upper_consonant
lower_consonant -> 'b'|'c'|'d'|'f'|'g'|'h'|'j'|'k'|'l'|'m'|'n'|'p'|'q'|'r'|'s'|'t'|'v'|'w'|'x'|'y'|'z'
upper_consonant -> 'B'|'C'|'D'|'F'|'G'|'H'|'J'|'K'|'L'|'M'|'N'|'P'|'Q'|'R'|'S'|'T'|'V'|'W'|'X'|'Y'|'Z'
""")
def genome_to_grammar(array):
sb = []
stack = [GRAMMAR.start()]
index = 0
wraps = 0
while stack:
symbol = stack.pop()
if isinstance(symbol, str):
sb.append(symbol)
else:
rules = [i for i in GRAMMAR.productions() if i.lhs().symbol() == symbol.symbol()]
rule_index = 0
if len(rules) > 1:
rule_index = array[index] % len(rules)
index += 1
if index >= len(array):
index = 0
wraps += 1
if wraps > 10:
return None
rule = rules[rule_index]
for production in reversed(rule.rhs()):
stack.append(production)
return ''.join(sb)
genome = [253, 69, 221, 97, 190, 254, 67, 137, 95, 72, 54, 232, 11, 136]
print(genome_to_grammar(genome))