本文整理汇总了Python中nltk.parse.generate.generate函数的典型用法代码示例。如果您正苦于以下问题:Python generate函数的具体用法?Python generate怎么用?Python generate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了generate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_sentence
def generate_sentence(self, depth=9, num=30000):
if num > 30000:
num = 30000
gen_num = 0
done = False
sentences_list = list()
for dep in range(1, depth):
sentences = generate(self.grammar, depth=dep)
for s in sentences:
sentences_list.append(' '.join(s) + '\n')
gen_num += 1
if gen_num > num:
done = True
break
if done:
break
# sentences = generate(self.grammar, depth=depth, n=4)
# for s in sentences:
# # file.write(' '.join(s) + '\n')
# sentences_list.append(' '.join(s) + '\n')
# sentences_list = sentences_list[0:num]
random.shuffle(sentences_list)
with open(self.origin_file, 'w') as file:
for s in sentences_list:
file.write(s)
示例2: gen_grammar3_past_plural
def gen_grammar3_past_plural(verb, direct_object, count):
g1 ="""
S -> W TR SUB V '?' | WA TR SUB V DO '?'
W -> 'who' | 'what' | 'when' | 'where' | 'why' | 'how'
WA -> 'when' | 'where' | 'why' | 'how'
TR -> 'have'
SUB -> PRO
PRO -> 'they' |'you'
V -> '%s'
DO -> 'the %s'
"""%(verb, direct_object)
grammar1 = CFG.fromstring(g1)
multiplier = 0
with open('sentences.csv', 'ab') as csvwriter:
writer = csv.writer(csvwriter)
for sentence in generate(grammar1, n=999):
if sentence.find('who') == 0:
multiplier = 1
if sentence.find('what') == 0:
multiplier = 1
if sentence.find('when') == 0:
multiplier = 2
if sentence.find('where') == 0:
multiplier = 2
if sentence.find('why') == 0:
multiplier = 4
if sentence.find('how') == 0:
multiplier = 4
writer.writerow((' '.join(sentence) , multiplier*count))
示例3: Tweet_content1
def Tweet_content1():
grammar = CFG.fromstring(demo_grammar)
for sentence in generate(grammar, n=4): """generating sentence of 4 words depth"""
print(' '.join(sentence))
return sentence
示例4: gen_grammar_plural
def gen_grammar_plural(verb, direct_object, count):
try:
verb = en.verb.present_participle(verb)
except KeyError:
return
if verb != "":
g1 ="""
S -> WA TR SUB V DO '?' | W TR SUB V '?'
W -> 'who' | 'what' | 'when' | 'where' | 'why' | 'how'
WA -> 'when' | 'where' | 'why' | 'how'
TR -> 'are' | 'were'
SUB -> 'they' | 'you'
V -> '%s'
DO -> 'the %s'
"""%(verb, direct_object)
grammar1 = CFG.fromstring(g1)
multiplier = 1
with open('sentences.csv', 'ab') as csvwriter:
writer = csv.writer(csvwriter)
for sentence in generate(grammar1, n=999):
sentence = ' '.join(sentence)
if sentence.find('who') == 0:
multiplier = 1
if sentence.find('what') == 0:
multiplier = 1
if sentence.find('when') == 0:
multiplier = 2
if sentence.find('where') == 0:
multiplier = 2
if sentence.find('why') == 0:
multiplier = 4
if sentence.find('how') == 0:
multiplier = 4
writer.writerow((' '.join(sentence) , multiplier*count))
示例5: generate_text
def generate_text(grammar,N):
from nltk.grammar import CFG
import nltk.parse.generate as gen
print('Generating the first %d sentences for demo grammar:' % (N,))
print(grammar)
grammar = CFG.fromstring(grammar)
grm_list = gen.generate(grammar, n=N)
for n, sent in enumerate(grm_list):
print('%3d. %s' % (n, ' '.join(sent)))
示例6: respondQuestion
def respondQuestion(sentence, keyWord, POS):
if "Tell me" not in sentence:
grammar = ""
if POS == "NNPS" or POS == "NNS":
grammar = CFG.fromstring("""
S -> H-NP1 Adj VP'?' | Wh-NP VP'?'
H-NP1 -> 'How'
Wh-NP -> 'Who' | 'What' | 'Where' | 'What'
Adj -> 'big' | 'small' | 'happy' | 'sad' | 'large' | 'difficult' | 'emotional' | 'old' | 'healthy' | 'strong' | 'cute' | 'hungry'
NP -> Pronoun | Proper-Noun | Noun
Pronoun -> 'they' | 'those'
Proper-Noun -> '[]'
Noun -> 'the <>'
VP -> Verb NP
Verb -> 'are'
""")
elif POS == "NN" or "NNP":
grammar = CFG.fromstring("""
S -> H-NP1 Adj VP'?' | Wh-NP VP'?'
H-NP1 -> 'How'
Wh-NP -> 'Who' | 'What' | 'Where' | 'What'
Adj -> 'big' | 'small' | 'happy' | 'sad' | 'large' | 'difficult' | 'emotional' | 'old' | 'healthy' | 'strong' | 'cute' | 'hungry'
NP -> Pronoun | Proper-Noun | Noun
Pronoun -> 'it' | 'that'
Proper-Noun -> '[]'
Noun -> 'the <>'
VP -> Verb NP
Verb -> 'is'
""")
rand_sent_list = []
response = ""
for sentence in generate(grammar):
rand_sent_list.append(' '.join(sentence))
while True:
num = randint(0, len(rand_sent_list)-1)
response = rand_sent_list[num]
if "<>" in response and (POS == "NNS" or POS == "NN"):
index = response.index("<>")
response = response[:index] + keyWord + response[index+2:]
break
if "[]" in response and (POS == "NNPS" or POS == "NNP"):
index = response.index("[]")
response = response[:index] + keyWord + response[index+2:]
break
if "<>" not in response and "[]" not in response:
break
return response
else:
knowledgeRep(sentence)
示例7: generateRawTemplates
def generateRawTemplates(depth):
gram = CFG.fromstring(grammarstring)
rawTemplates = generate(gram, depth=depth)
templatefiles = []
for index, state in enumerate(rawTemplates):
filename = os.path.join("./templates","template"+str(index))
with open(filename, 'w') as templatefile:
templatefile.write(' '.join(state))
templatefiles.append(filename)
print str(len(templatefiles))+" template files generated"
return templatefiles
示例8: generate_tweet
def generate_tweet(grammar):
from nltk.grammar import CFG
import nltk.parse.generate as gen
print(grammar)
grammar = CFG.fromstring(grammar)
grm_list = gen.generate(grammar, n=SIZE) # TODO voir la taille max ? moyen de la recuperer ?
from random import randint
rd = randint(0,SIZE)
cpt = 0
for n, sent in enumerate(grm_list):
if rd == cpt:
print ("Your tweet : ")
print('%3d. %s' % (n, ' '.join(sent)))
cpt += 1
示例9: main
def main():
zen = """ Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!"""
tagged = nltk.pos_tag(nltk.word_tokenize(zen))
tagged = [(tag, word) for word, tag in tagged]
#
#tag_word_map = defaultdict(list)
#[(tag, word) for word, tag in tagged]
tags = set([tag for tag, _ in tagged])
tag_word_map = {tag: {word for key, word in tagged if key == tag} for tag in tags}
gram_head = """
S -> NNP VBZ JJR IN RB
"""
cats = ['NNP', 'VBZ', 'JJR', 'IN', 'RB']
gram = [cat + ' -> ' + '|'.join([repr(x) for x in tag_word_map[cat]]) for cat in cats]
grammar = gram_head + '\n'.join(gram)
grammar = nltk.CFG.fromstring(grammar)
poem = []
for sentence2 in generate(grammar, depth=5):
poem.append(' '.join(sentence2))
out = "\n".join(choice(poem, size=10))
print(out)
示例10: print
import nltk
from nltk.parse import generate
from nltk.grammar import Nonterminal
cfg = nltk.CFG.fromstring("""
root -> who_player has the most runs
who_player -> who
who_player -> which player
who_player -> which team player
who -> 'who'
which -> 'which'
player -> 'player'
team -> 'indian' | 'australian' | 'england' | 'sri' 'lankan'
has -> 'has'
the -> 'the'
this -> 'this'
most -> 'most'
runs -> 'runs'
""")
print(list((n,sent) for n, sent in enumerate(generate.generate(cfg, n=100, start=Nonterminal('root')), 1)))
result1 = nltk.ChartParser(cfg).parse('which england player has the most runs'.split())
result2 = nltk.ChartParser(cfg).parse(['which', 'sri', 'lankan', 'player', 'has', 'the', 'most', 'runs'])
print(list(result1))
print(list(result2))
示例11: load
from nltk.parse.generate import generate
from nltk import CFG
from nltk.data import load
for gg in [ 'grammar_2.cfg']:
grammar = load( 'file:' + gg)
for sentence in generate(grammar, depth=6, n=1000000):
print(' '.join(sentence))
示例12: output
#.........这里部分代码省略.........
# Create the grammar
#P:prepositions, DET:articles, adverbs
DET = ["'the'","'a'","'some'"]
# P = ["'in'","'at'","'since'","'for'","'to'","'past'","'to'""'by'","'in'","'at'","'on'","'under'","'below'","'over'","'above'","'into'","'from'","'of'","'on'","'at'"]
VB = ["'talks'","'does'","'has'","'cries'", "'fights'", "'traps'", "'bakes'", "'fondles'", "'cooks'", "'sees'", "'calls'", "'smells'", "'tastes'", "'hears'"]
assignments = pos_tag(tokens) # tagset='universal' for ADJ, NOUN, etc.
# pos_tags = []
pos_words = {}
pos_words['DET'] = DET
#pos_words['P'] = P
pos_words['VB'] = VB
for tuple in assignments:
word = tuple[0]
pos = tuple[1]
if pos in pos_words:
pos_words[pos].append("\'" + word + "\'")
else:
pos_words[pos] = []
pos_words[pos].append("\'" + word + "\'")
# pos_tags.append(pos)
#grammar = """
#S -> NP VP
#PP -> P NP
#NP -> Det N
#VP -> V Det N | V Det N PP
#"""
grammar = """
S -> NP VP
NP -> Det N
VP -> V Det N
"""
#Det -> 'DT'
# N -> 'NN'
# V -> 'VBZ'
# P -> 'PP'
# adverb is RB
if 'DET' in pos_words:
grammar += 'Det ->' + ' | '.join(pos_words['DET']) + '\n'
if 'P' in pos_words:
grammar += 'P ->' + ' | '.join(pos_words['P']) + '\n'
if 'NN' in pos_words:
grammar += 'N ->' + ' | '.join(pos_words['NN']) + '\n'
#change to VB for nltk
if 'VB' in pos_words:
grammar += 'V ->' + ' | '.join(pos_words['VB']) + '\n'
#if 'JJ' in pos_words:
# grammar += 'A ->' + ' | '.join(pos_words['JJ']) + '\n'
simple_grammar = CFG.fromstring(grammar)
# simple_grammar.start()
# simple_grammar.productions()
sentences = []
sentence_validity = []
for sentence in generate(simple_grammar, depth=4):
sentences.append(' '.join(sentence))
sentence_validity = get_validity(sentences)
#get_validity(sentences)
# parser = nltk.ChartParser(simple_grammar)
# tree = parser.parse(pos_tags)
story = ""
for i in range(0, 10):
tuple = sentence_validity[i]
string = tuple[1]
start_letter = string[0].upper()
story += start_letter
story += string[1:]
story += ". "
return render(request, 'makestory/output.html',
{
'imageURL_output': imageURL,
'story_output': story,
'grammar_test_output': simple_grammar,
'sentences_test_output': sentences,
}
)
else:
return fail(request)
return fail(request)
示例13: choose_line
def choose_line(some_lines):#5
return a_random.choice(#7
some_lines).lower() #5
############################################
############################################
choose = choose_line #5
g = G.fromstring(#7
this_is_the_grammar) #5
############################################
############################################
while not len(pentas):#5
for poem in generate(g, #7
start=N('five')): #5
############################################
############################################
pentas.append(#5
with_blank_spaces.join(poem))#7
fives = pentas #5
############################################
############################################
third = choose(fives) #5
first = choose(fives) #7
def display_the(poem):#5
############################################
示例14: xrange
[1. if i == b else 0. for i in xrange(len(code_for))])
# list of codes of symbols to predict
to_predict_codes = [onehot(code_for[s]) for s in to_predict]
# function to test if a symbol code is in list to predict
def in_predict_codes(code):
for i in xrange(len(to_predict_codes)):
if ((code == to_predict_codes[i]).all()):
return True
return False
# sample_strings = all strings from grammar of depth at most sample_depth
sample_strings = list(generate(grammar, depth=sample_depth))
# report #, min length and max length for strings in sample_strings
print("number of sample strings = {}".format(len(sample_strings)))
sample_lengths = [len(s) for s in sample_strings]
print("min length = {}, max length = {}".format(min(sample_lengths),
max(sample_lengths)))
# sanity check: report one random string from sample_strings
print "random sample string = {}".format(random.choice(sample_strings))
#################################
model = VanillaModel(len(code_for), READ_SIZE, len(code_for))
try:
model.cuda()
示例15: print
from nltk.parse.generate import generate #, demo_grammar
from nltk import CFG
demo_grammar = """
S -> NP VP
NP -> Det N
PP -> P NP
VP -> 'slept' | 'saw' NP | 'walked' PP
Det -> 'the' | 'a'
N -> 'man' | 'park' | 'dog'
P -> 'in' | 'with'
"""
grammar = CFG.fromstring(demo_grammar)
print(grammar)
#Join words and generate based off of grammar - for n
for sentence in generate(grammar, n=12):
print(' '.join(sentence))
'''
Notes:
Need to symbolize the grammar
Have the machine process the language
Need to integrate with Markov chain - file 'agiliq-markov.py'
'''
for sentence in generate(grammar, depth=4):
print(' '.join(sentence))