Skip to content
This repository has been archived by the owner on Apr 20, 2019. It is now read-only.

Commit

Permalink
Extract tweet preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianKniephoff committed May 15, 2016
1 parent 56e44a1 commit 098c808
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def __init__(self):
self.padding_index = None
self.classes = None

# TODO Do the following two methods need to be public?

def tweets_to_indices(self, tweets):
return pad_sequences(
[
Expand All @@ -73,6 +75,21 @@ def tweets_to_indices(self, tweets):
padding='post'
)

def prepare_labeled_tweets(self, tweets):
def output_for_class(class_number):
output = [0] * self.classes
output[class_number] = 1
return output

return {
'input': self.tweets_to_indices(
labeled_tweet.tweet for labeled_tweet in tweets
),
'output': np.array(
[output_for_class(labeled_tweet.label) for labeled_tweet in tweets]
)
}

def build_network(self,
initial_embeddings,
filter_configuration,
Expand Down Expand Up @@ -165,24 +182,9 @@ def infinite_generator():

generator = infinite_generator()

def labeled_tweets_to_keras(tweets):
def output_for_class(class_number):
output = [0] * self.classes
output[class_number] = 1
return output

return {
'input': self.tweets_to_indices(
labeled_tweet.tweet for labeled_tweet in tweets
),
'output': np.array(
[output_for_class(labeled_tweet.label) for labeled_tweet in tweets]
)
}

def tweet_generator():
while True: # TODO This seems redundant. Can we compose generators somehow?
yield labeled_tweets_to_keras(
yield self.prepare_labeled_tweets(
[next(generator) for _ in range(batch_size)]
)

Expand Down

0 comments on commit 098c808

Please sign in to comment.