Commit 0dcf706a authored by anne laforet's avatar anne laforet

Merge branch 'master' of activearchives.org:git/1943

parents 97349ee4 9184a2a3
......@@ -41,96 +41,103 @@ tiggy, rabbit, jemima, jeremy, robinson, pigling]
# functions
# random choice of tale
def select(texts):
txt = choice(texts)
return txt
item = choice(texts)
#print "selected txt", item
return item
# gets title from tale
def title(txt):
txt = open(txt).read().decode("utf-8")
nametxt = txt.splitlines()[0]
return nametxt
name = txt.splitlines()[0]
#print "name", name
return name
# gets url from tale
def source(txt):
txt = open(txt).read().decode("utf-8")
url = txt.splitlines()[1]
#print "url", url
return url
# splits text into list of words
def tokenize(txt):
txt = open(txt).read().decode("utf-8")
tokens = ' '.join(line.replace('\n', '') for line in txt)
basket = ' '.join(line.replace('\n', '') for line in txt)
#turn into wordlist
tokens = word_tokenize(txt)
return tokens
basket = word_tokenize(txt)
#print "tokens", basket
return basket
# splits text into list of sentences
def sentences(txt):
txt = ''.join(open(txt).readlines())
phrases = re.split(r' *[\.\?!][\'"\)\]]* *', txt)
return phrases
basket = re.split(r' *[\.\?!][\'"\)\]]* *', txt)
#print "sentences", basket
return basket
# Select sentences that contain keyword
def sentences_with_key(sentences, pick):
selected_sentences = []
def sentences_with_key(sentences, word):
basket = []
for sentence in sentences:
wordlist = sentence.split(" ")
if pick in wordlist:
if word in wordlist:
sentence = sentence+'.'
selected_sentences.append(sentence)
return selected_sentences
# replace vowels by numbers
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
basket.append(sentence)
print "sentences_with_key", basket
return basket
# Replacements
reps = {'a':'4', 'e':'3', 'i':'1', 'o':'0', 'u':'8'}
def frequency(msg):
# captures last line in chat
# picks a random tale
txt = select(texts)
#print "freq text", txt
# captures title of tale
nametxt = title(txt)
#print "freq titel", nametxt
# captures url of tale
url = source(txt)
urltxt = source(txt)
#print "freq url", urltxt
# transforms txt in wordlist
tokens = tokenize(txt)
#print "freq tokens", tokens
# transforms txt in list of sentences
phrases = sentences(txt)
#print " freq phrases", phrases
# turn list of tokenized words into nltk.Text
fin = nltk.Text(tokens)
# FreqDist
fdist1 = FreqDist(fin)
print(fdist1)
#print(fdist1)
# splits chat message in wordlist
words = msg.split()
# selects from chat message words between 2 and 10 characters
selected = []
selected = [x for x in words if len(x)>3 and len(x)<10 and x in fdist1]
selected = [x for x in words if len(x)>2 and len(x)<10 and x in fdist1]
print "freq selected words from msg", selected
if len(selected)>0:
pick = choice(selected)
print "pick", pick
# Counts the frequency of the selected word
amount = fdist1[pick]
print(amount)
print "amount", amount
# Selects sentences in which selected word appears
selected_sentences = sentences_with_key(phrases, pick)
print "selected sentences", selected_sentences
# Shuffles the selected sentences
shuffle(selected_sentences)
# Prints the results
examples = []
basket = []
if amount > 0:
pick = pick.upper()
message1 = "In " + nametxt + " I used " + str(amount) + " times the word " + pick + ". Like in here: "
print message1
examples.append(message1)
basket.append(message1)
for sentence in selected_sentences[:4]:
message = (sentence.replace("\n", " ").replace(pick, pick.upper()))
examples.append(message)
return examples
basket.append(message)
return basket
# print message 2
......
......@@ -41,71 +41,70 @@ tiggy, rabbit, jemima, jeremy, robinson, pigling]
# functions
# random choice of tale
def select(texts):
txt = choice(texts)
return txt
item = choice(texts)
#print "selected txt", item
return item
# gets title from tale
def title(txt):
txt = open(txt).read().decode("utf-8")
nametxt = txt.splitlines()[0]
return nametxt
name = txt.splitlines()[0]
#print "name", name
return name
# gets url from tale
def source(txt):
txt = open(txt).read().decode("utf-8")
url = txt.splitlines()[1]
#print "url", url
return url
# splits text into list of words
def tokenize(txt):
txt = open(txt).read().decode("utf-8")
tokens = ' '.join(line.replace('\n', '') for line in txt)
basket = ' '.join(line.replace('\n', '') for line in txt)
#turn into wordlist
tokens = word_tokenize(txt)
return tokens
basket = word_tokenize(txt)
#print "tokens", basket
return basket
# splits text into list of sentences
def sentences(txt):
txt = ''.join(open(txt).readlines())
phrases = re.split(r' *[\.\?!][\'"\)\]]* *', txt)
return phrases
basket = re.split(r' *[\.\?!][\'"\)\]]* *', txt)
#print "sentences", basket
return basket
# Select sentences that contain keyword
def sentences_with_pick(sentences, pick):
selected_sentences = []
def sentences_with_key(sentences, word):
basket = []
for sentence in sentences:
wordlist = sentence.split(" ")
if pick in wordlist:
if word in wordlist:
sentence = sentence+'.'
selected_sentences.append(sentence)
return selected_sentences
# replace vowels by numbers
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
# Replacements
reps = {'a':'4', 'e':'3', 'i':'1', 'o':'0', 'u':'8'}
basket.append(sentence)
print "sentences_with_key", basket
return basket
def length(msg):
# captures last line in chat
# splits chat message in wordlist
words = msg.split()
# selects from chat message words between 2 and 10 characters
selected = []
basket = []
for word in words:
if len(word) > 3 and len(word) < 10:
selected.append(word)
#print(selected)
if len(word) > 2 and len(word) < 12:
basket.append(word)
print "basket words message", basket
# picks a random word
w = choice(selected)
w = choice(basket)
print "length word", w
w = w.upper()
#print(w)
print "length word capitals", w
# define length keyword
length = len(w)
#print(length)
print "lenght", length
# picks a random tale
txt = select(texts)
# captures title of tale
......@@ -120,28 +119,31 @@ def length(msg):
fin = nltk.Text(tokens)
# selecteer op lengte van woorden
T = set(fin)
words = [x for x in T if len(x) == length]
sorted_words = sorted(words)
basket = [x for x in T if len(x) == length]
sorted_words = sorted(basket)
# picks word of sorted_words
pick = choice(sorted_words)
#print(pick)
print "picked word from tale with right lenght", pick
# Selects sentences in which selected word appears
selected_sentences = sentences_with_pick(phrases, pick)
selected_sentences = sentences_with_key(phrases, pick)
print "length selected sentences with key", selected_sentences
# Shuffles the selected sentences
shuffle(selected_sentences)
# Prints the results
basket = []
if len(selected_sentences) > 0:
pick = pick.upper()
message1 = "The word, " + w + ", counts " + str(length) + " characters. In " + nametxt + " I used the following words of " + str(length) + " characters each: " + ', '.join(sorted_words[:4])
#print(message1)
basket.append(message1)
printsentence = selected_sentences[0]
#print(printsentence)
pick = pick.upper()
message2 = "Take the word, " + pick + ", for example, I used it in this sentence: " + printsentence.replace(pick, pick.upper())
print(message2)
message3 = "Source: " + nametxt
print(message3)
return message1, message2, message3
#print(message2)
basket.append(message2)
#print(message3)
return basket
#msg = e.arguments[0]
......@@ -173,10 +175,9 @@ class MyBot(irc.bot.SingleServerIRCBot):
# captures last line in chat
print e.arguments
msg = e.arguments[0]
message1, message2, message3 = length(msg)
self.say(c, message1)
self.say(c, message2)
self.say(c, message3)
messages = length(msg)
for message in messages:
self.say(c, message)
# Launch bot
if __name__ == "__main__":
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment