Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: 8e302f855d
...
compare: d926761cdc
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
2  Makefile
@@ -1,4 +1,4 @@
test:
- mocha
+ mocha -u tdd
.PHONY: test
View
274 controllers/AnalysisController.js
@@ -29,32 +29,35 @@ exports.controller = function (req, res) {
// run via cron, please
self.analyze = function () {
-
ActivityItem.find({analyzed_at: {"$lt": (new Date(Date.now()-86300*1000))}, created_at: {"$gt": (new Date(Date.now()-86400*1000))}})
.sort('posted_at', -1)
.limit(30)
.run(function (err, items) {
if (err || !items) {
- console.log("No items found "+err);
+ console.log("No items found " + err);
res.send("Done");
return;
}
analyze_next();
function analyze_next() {
- if (items.length == 0) {
- return finished();;
+ if (items.length === 0) {
+ return finished();
}
var item = items.shift();
- //console.log("Analyzing item: "+item.message);
+ console.log("Analyzing item: " + item.message);
//item.ratings = ratings;
+ console.log(item);
item.analyzed_at = new Date();
item.save(function (err) {
+ console.log('Item saved!');
+ console.log(err);
item.analyze(function (err, _item) {
- //console.log("Done analyzing "+_item.topics.length);
+ console.log(err);
+ console.log("Done analyzing " + _item.topics.length);
//item.commit("ratings");
_item.save(function (err) {
- // err?
+ console.log(err);
});
analyze_next();
});
@@ -64,258 +67,5 @@ exports.controller = function (req, res) {
res.send("Done");
}
});
- }
- // end /analysis/analyze
-
-}
-
-function analyze_item (_item, cb) {
- console.log('AnalysisController analyze_item()');
-
- var error = null;
- var item = _item;
-
- var ratings = item.ratings || {};
- var characteristics = item.characteristics || [];
- var topics = item.topics || [];
- if (!ratings.overall) {
- ratings.overall = 0;
- }
- var message = item.message.toLowerCase();
-
- var url_pattern = /\(?\bhttps?:\/\/[-A-Za-z0-9+&@#\/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#\/%=~_()|]/gi;
- var urls = message.match(url_pattern);
- var hash_pattern = /#[a-zA-Z_]*/gi;
- var hashtags = message.match(hash_pattern);
-
- urls = urls || [];
- hashtags = hashtags || [];
-
- var keywords = [];
- urls.forEach(function (url) {
- domain = url.substr(url.indexOf("//")+2);
- domain = domain.substr(0, domain.indexOf("/"));
- var found = false;
- keywords.forEach(function (existing) {
- if (existing == domain) {
- found = true;
- }
- });
- if (!found) {
- keywords.push(domain);
- }
- });
- hashtags.forEach(function (tag) {
- tag = tag.substring(1);
- var found = false;
- keywords.forEach(function (existing) {
- if (existing == tag) {
- found = true;
- }
- });
- if (!found) {
- keywords.push(tag);
- }
- });
-
- message = message.remove_urls().remove_hashtags().remove_screen_names().replace_punctuation();
- var words = tokenizer.tokenize(message);//.tokenizeAndStem();
-
-
- //console.log(words);
-
- function lookup_next_word () {
- if (words.length == 0) {
- return add_topics();
- }
- word = words.shift();
- //console.log("> Word: "+word);
-
- var neither = 0;
- var noun = 0;
- var verb = 0;
-
- if (word.length > 2) {
- if (word.substring(0, word.length-3) == "ing") {
- verb = 100;
- classify_word(word, noun, verb, neither);
- } else
- if (word.match(/^[0-9]*$/)) {
- classify_word(word, noun, verb, neither);
- } else {
- wordnet.lookup(word, function(results) {
-
- results.forEach(function(result) {
- if (result.pos == "n") {
- noun++;
- } else
- if (result.pos == "v") {
- verb++;
- } else
- if (result.pos == "a" || result.pos == "r" || result.pos == "s") {
- neither++;
- }
- });
- classify_word(word, noun, verb, neither);
- });
- return;
- }
- } else {
- classify_word(word, noun, verb, neither);
- }
- }
- function classify_word (w, n, v, neither) {
- if ((n == 0 && v == 0) || neither > n+v/2) {
- console.log("I don't know what kind of word '"+w+"' is");
- } else {
- console.log("Looked up '"+w+"' and found it is a ... "+(n >= v ? "noun ("+n+":"+v+")" : "verb ("+v+":"+n+")"));
- var found = false;
- keywords.forEach(function (existing) {
- if (existing == w) {
- found = true;
- }
- });
- if (!found) {
- keywords.push(w);
- }
- }
- lookup_next_word();
- }
- lookup_next_word();
-
- function add_topics () {
- var existing_topics = [];
- console.log("Getting topics");
-
- if (keywords.length > 0) {
- Topic.find({text: {"$in": keywords}}, function (err, t) {
- existing_topics = t;
- add_new_topics();
- });
- } else {
- done_with_topics();
- }
-
- function add_new_topics () {
- var new_topics = [];
- keywords.forEach(function (keyword) {
- var found = false;
- existing_topics.forEach(function (existing) {
- if (existing.text == keyword) {
- found = true;
- }
- });
- if (!found) {
- new_topics.push(keyword);
- }
- });
-
- function add_each_topic () {
- if (new_topics.length == 0) {
- return done_adding();
- }
- topic_text = new_topics.shift();
- var topic = new Topic({text: topic_text, ratings: {overall: 0}});
- topic.save(function (err) {
- add_each_topic();
- });
- }
- add_each_topic();
- }
-
- function done_adding () {
- var topic_ids = [];
- Topic.find({text: {"$in": keywords}}, function (err, t) {
- t.forEach(function (topic) {
- topic_ids.push(topic._id);
- });
- item.topics = topic_ids;
- console.log("ADDED TOPIC IDS! "+item.topics.length);
- item.commit("topics");
-
- done_with_topics();
- });
-
- }
-
- function done_with_topics () {
- console.log("calling back... "+item.topics.length);
-
- rate_that_shit();
- }
-
- function rate_that_shit () {
- var topic_ratings = 0;
- var topic_count = 0;
- var char_ratings = 0;
- var char_count = 0;
-
- ActivityItem.findOne({_id: item.id})
- .populate("characteristics")
- .populate("topics")
- .run(function (err, _item) {
- _item.topics.forEach(function (topic) {
- if (topic.ratings.overall > 0 || topic.ratings.overall < 0) {
- topic_ratings += topic.ratings.overall;
- topic_count++;
- }
- });
- _item.characteristics.forEach(function (ch) {
- char_ratings += parseInt(ch.rating);
- char_count++;
- });
-
- topic_count = topic_count > 1 ? topic_count : 1;
- char_count = char_count > 1 ? char_count : 1;
-
- ratings.topics = topic_ratings;
- ratings.characteristics = char_ratings;
- ratings.overall = 0 + (topic_ratings/topic_count + char_ratings/char_count*2)/3;
- });
- //rate_by_topics();
- //rate_by_characteristics();
- //rate_by_behavior();
-
- //Topic.find({_id: {"$in": item.topics}}, function (
-
- item.ratings = ratings;
- item.commit("ratings");
- cb(error, item);
- }
- }
-}
-
-function get_topics (text, cb) {
- var error = null;
- var my_topics = [];
-
-
-
- cb(error, my_topics);
-}
-
-if (!String.prototype.remove_urls) {
- String.prototype.remove_urls = function () {
- var url_pattern = /\(?\bhttps?:\/\/[-A-Za-z0-9+&@#\/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#\/%=~_()|]/gi;
- return this.replace(url_pattern, "");
- }
-}
-if (!String.prototype.remove_hashtags) {
- String.prototype.remove_hashtags = function () {
- var hash_pattern = /(^|\s)#[-A-Za-z0-9_]+(\s|$)/gi;
- return this.replace(hash_pattern, "$1$2");
- }
-}
-if (!String.prototype.remove_screen_names) {
- String.prototype.remove_screen_names = function () {
- var at_pattern = /(^|\s)@[-A-Za-z0-9_]+(\s|$)/gi;
- return this.replace(at_pattern, "$1$2");
- }
-}
-if (!String.prototype.replace_punctuation) {
- String.prototype.replace_punctuation = function () {
- var alpha_pattern = /[^a-z^A-Z^0-9^-^_]/gi;
- return this.replace(alpha_pattern, " ");
- }
-}
-
+ };
+};
View
643 models/ActivityItem.js
@@ -45,102 +45,121 @@ var ActivityItemSchema = new Schema({
});
ActivityItemSchema.methods.analyze = function(cb) {
- var self = this;
- var item = self;
- var AI = this;
- (function analyze_me(item, cb) {
- var Topic = mongoose.model('Topic'),
- JunkTopic = mongoose.model('JunkTopic'),
- AI = mongoose.model('ActivityItem');
-
- var error = null;
-
- var ratings = item.ratings || {};
- var characteristics = item.characteristics || [];
- var topics = item.topics || [];
- if (!ratings.overall) {
- ratings.overall = 0;
- }
- var message = item.message.toLowerCase();
-
- var url_pattern = /\(?\bhttps?:\/\/[-A-Za-z0-9+&@#\/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#\/%=~_()|]/gi;
- var hash_pattern = /#[a-zA-Z_0-9]*/gi;
-
- var urls = message.match(url_pattern) || [];
- var hashtags = message.match(hash_pattern) || [];
-
- var keywords = [];
+ analyze_me(this, cb);
+}
- // Add URLs to list of keywords
- urls.forEach(function(url) {
- var domain = url.substr(url.indexOf("//") + 2);
- // Cut off at first slash in URL, if one exists
- domain = domain.substr(0, domain.indexOf("/")) || domain;
- merge(keywords, domain);
- });
+function analyze_me(item, cb) {
+ var Topic = mongoose.model('Topic'),
+ JunkTopic = mongoose.model('JunkTopic'),
+ AI = mongoose.model('ActivityItem');
+
+ var error = null;
+
+ var ratings = item.ratings || {};
+ var characteristics = item.characteristics || [];
+ var topics = item.topics || [];
+ if (!ratings.overall) {
+ ratings.overall = 0;
+ }
+ var message = item.message.toLowerCase();
+
+ var url_pattern = /\(?\bhttps?:\/\/[-A-Za-z0-9+&@#\/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#\/%=~_()|]/gi;
+ var hash_pattern = /#[a-zA-Z_0-9]*/gi;
+
+ var urls = message.match(url_pattern) || [];
+ var hashtags = message.match(hash_pattern) || [];
+
+ var keywords = [];
- // Add hashtags
- hashtags.forEach(function(tag) {
- tag = tag.substring(1);
- merge(keywords, tag);
- });
-
- // Natural language analysis of remainder of message
- message = message
- .remove_urls()
- .remove_hashtags()
- .remove_screen_names()
- .replace_punctuation();
- var chunks = message.split(" ");
- var new_chunks = [];
- chunks.forEach(function(chunk) {
- // Not sure the purpose of this
- if (chunk.indexOf("'") == -1) {
- new_chunks.push(chunk);
- }
+ // Add URLs to list of keywords
+ urls.forEach(function(url) {
+ var domain = url.substr(url.indexOf("//") + 2);
+ // Cut off at first slash in URL, if one exists
+ domain = domain.substr(0, domain.indexOf("/")) || domain;
+ merge(keywords, domain);
+ });
+
+ // Add hashtags
+ hashtags.forEach(function(tag) {
+ tag = tag.substring(1);
+ merge(keywords, tag);
+ });
+
+ // Natural language analysis of remainder of message
+ message = message
+ .remove_urls()
+ .remove_hashtags()
+ .remove_screen_names()
+ .replace_punctuation();
+ var chunks = message.split(" ");
+ var new_chunks = [];
+ chunks.forEach(function(chunk) {
+ // Not sure the purpose of this
+ if (chunk.indexOf("'") == -1) {
+ new_chunks.push(chunk);
+ }
+ });
+ var words = tokenizer.tokenize(message);
+
+ var ngram_length = words.length;
+ var phrases = [];
+ while (ngram_length > 1) {
+ var tmp_ngrams = NGrams.ngrams(words, ngram_length);
+ tmp_ngrams.forEach(function(phrase) {
+ phrases.push({text: phrase.join(" ")});
+ phrases.push({text: phrase.join("")});
});
- var words = tokenizer.tokenize(message);
-
- var ngram_length = words.length;
- var phrases = [];
- while (ngram_length > 1) {
- var tmp_ngrams = NGrams.ngrams(words, ngram_length);
- tmp_ngrams.forEach(function(phrase) {
- phrases.push({text: phrase.join(" ")});
- phrases.push({text: phrase.join("")});
- });
- ngram_length--;
+ ngram_length--;
+ }
+
+ var tmp_words = [];
+ words.forEach(function(word) {
+ if (word.length > 2) {
+ tmp_words.push(word);
}
-
- var tmp_words = [];
- words.forEach(function(word) {
- if (word.length > 2) {
- tmp_words.push(word);
+ });
+ words = tmp_words;
+
+ check_phrases();
+
+ // Check topics for phrase matches
+ function check_phrases() {
+ //console.log(phrases);
+ Topic.find()
+ .or(phrases)
+ .run(function(err, topics) {
+ if (!err && topics) {
+ topics.forEach(function(topic) {
+ merge(words, topic.text);
+ });
}
+ remove_topics_from_words();
});
- words = tmp_words;
-
- check_phrases();
+ }
+
+ function remove_topics_from_words () {
+ var tmp_words = [];
- // Check topics for phrase matches
- function check_phrases() {
- //console.log(phrases);
- Topic.find()
- .or(phrases)
- .run(function(err, topics) {
- if (!err && topics) {
- topics.forEach(function(topic) {
- merge(words, topic.text);
- });
+ JunkTopic.find({text: {"$in": words}}, function(err, topics) {
+ if (err || !topics) {
+ topics = [];
+ }
+ words.forEach(function(word) {
+ var found = false;
+ topics.forEach(function(topic) {
+ if (topic.text == word) {
+ found = true;
+ }
+ });
+ if (!found && word.length > 3) {
+ tmp_words.push(word);
}
- remove_topics_from_words();
});
- }
-
- function remove_topics_from_words () {
- var tmp_words = [];
+ words = tmp_words;
+ tmp_words = [];
+ //console.log("okay, let's go! lookup_next_word()!");
- JunkTopic.find({text: {"$in": words}}, function(err, topics) {
+ Topic.find({text: {"$in": words}}, function(err, topics) {
if (err || !topics) {
topics = [];
}
@@ -151,266 +170,246 @@ ActivityItemSchema.methods.analyze = function(cb) {
found = true;
}
});
- if (!found && word.length > 3) {
+ if (found) {
+ keywords.push(word);
+ } else {
tmp_words.push(word);
}
});
+
words = tmp_words;
- tmp_words = [];
- //console.log("okay, let's go! lookup_next_word()!");
-
- Topic.find({text: {"$in": words}}, function(err, topics) {
- if (err || !topics) {
- topics = [];
- }
- words.forEach(function(word) {
- var found = false;
- topics.forEach(function(topic) {
- if (topic.text == word) {
- found = true;
- }
- });
- if (found) {
- keywords.push(word);
- } else {
- tmp_words.push(word);
- }
- });
-
- words = tmp_words;
-
- lookup_next_word();
- });
-
+
+ lookup_next_word();
});
+
+ });
+ }
+
+ function lookup_next_word() {
+ if (words.length == 0) {
+ return add_topics();
}
-
- function lookup_next_word() {
- if (words.length == 0) {
- return add_topics();
- }
- word = words.shift();
+ word = words.shift();
- var neither = 0;
- var noun = 0;
- var verb = 0;
+ var neither = 0;
+ var noun = 0;
+ var verb = 0;
- if (word.length > 3) {
- // Numbers
- if (word.match(/^[0-9]*$/)) {
- classify_word(word, noun, verb, neither);
- // Everything else
- } else {
- wordnet.lookup(word, function(results) {
- results.forEach(function(result) {
- if (result.pos == "n") {
- noun++;
- } else if (result.pos == "v") {
- verb++;
- } else if (result.pos == "a" ||
- result.pos == "r" ||
- result.pos == "s") {
- neither++;
- }
- });
- classify_word(word, noun, verb, neither);
- });
- return;
- }
- // Automatically junk any word under 4 characters
+ if (word.length > 3) {
+ // Numbers
+ if (word.match(/^[0-9]*$/)) {
+ classify_word(word, noun, verb, neither);
+ // Everything else
} else {
- save_junk_topic(word);
- lookup_next_word();
+ wordnet.lookup(word, function(results) {
+ results.forEach(function(result) {
+ if (result.pos == "n") {
+ noun++;
+ } else if (result.pos == "v") {
+ verb++;
+ } else if (result.pos == "a" ||
+ result.pos == "r" ||
+ result.pos == "s") {
+ neither++;
+ }
+ });
+ classify_word(word, noun, verb, neither);
+ });
+ return;
}
+ // Automatically junk any word under 4 characters
+ } else {
+ save_junk_topic(word);
+ lookup_next_word();
}
+ }
- function classify_word(w, n, v, neither) {
- if (n === 0 && neither > v) {
- save_junk_topic(w);
- } else {
- merge(keywords, w);
- }
- lookup_next_word();
+ function classify_word(w, n, v, neither) {
+ if (n === 0 && neither > v) {
+ save_junk_topic(w);
+ } else {
+ merge(keywords, w);
}
+ lookup_next_word();
+ }
+
+ function save_junk_topic(word) {
+ JunkTopic.findOne({text: word}, function(err, topic) {
+ if (err || (topic && topic.text == word)) {
+ return;
+ }
+ var junker = new JunkTopic({text: word});
+ junker.save(function(err) {
+ // err?
+ });
+ });
+ }
+
+ function add_topics() {
+ var existing_topics = [];
+ //console.log("Getting topics");
- function save_junk_topic(word) {
- JunkTopic.findOne({text: word}, function(err, topic) {
- if (err || (topic && topic.text == word)) {
- return;
- }
- var junker = new JunkTopic({text: word});
- junker.save(function(err) {
- // err?
- });
+ if (keywords.length > 0) {
+ Topic.find({text: {"$in": keywords}}, function (err, t) {
+ existing_topics = t;
+ add_new_topics();
});
+ } else {
+ done_with_topics();
}
- function add_topics() {
- var existing_topics = [];
- //console.log("Getting topics");
-
- if (keywords.length > 0) {
- Topic.find({text: {"$in": keywords}}, function (err, t) {
- existing_topics = t;
- add_new_topics();
- });
- } else {
- done_with_topics();
- }
-
- function add_new_topics() {
- var new_topics = [];
- keywords.forEach(function (keyword) {
- var found = false;
- existing_topics.forEach(function (existing) {
- if (existing.text == keyword) {
- found = true;
- }
- });
- if (!found) {
- new_topics.push(keyword);
+ function add_new_topics() {
+ var new_topics = [];
+ keywords.forEach(function (keyword) {
+ var found = false;
+ existing_topics.forEach(function (existing) {
+ if (existing.text == keyword) {
+ found = true;
}
});
-
- function add_each_topic() {
- if (new_topics.length == 0) {
- return done_adding();
- }
- topic_text = new_topics.shift();
- var topic = new Topic({text: topic_text, ratings: {overall: 0}});
- topic.save(function(err) {
- add_each_topic();
- });
+ if (!found) {
+ new_topics.push(keyword);
}
- add_each_topic();
- }
+ });
- function unique_ids (ids) {
- var unique = [];
+ function add_each_topic() {
+ if (new_topics.length == 0) {
+ return done_adding();
+ }
+ topic_text = new_topics.shift();
+ var topic = new Topic({text: topic_text, ratings: {overall: 0}});
+ topic.save(function(err) {
+ add_each_topic();
+ });
+ }
+ add_each_topic();
+ }
+
+ function unique_ids (ids) {
+ var unique = [];
- ids.forEach(function (id) {
- var found = false;
- unique.forEach(function (u) {
- if (String(u) == String(id)) {
- found = true;
- }
- });
- if (!found) {
- unique.push(id);
+ ids.forEach(function (id) {
+ var found = false;
+ unique.forEach(function (u) {
+ if (String(u) == String(id)) {
+ found = true;
}
});
- return unique;
- }
-
- function done_adding() {
- var topic_ids = [];
- var topic_texts = [];
- Topic.find({text: {"$in": keywords}}, function(err, t) {
- t.forEach(function(topic) {
- topic_ids.push(topic.id);
- topic_texts.push(topic.text);
+ if (!found) {
+ unique.push(id);
+ }
+ });
+ return unique;
+ }
+
+ function done_adding() {
+ var topic_ids = [];
+ var topic_texts = [];
+ Topic.find({text: {"$in": keywords}}, function(err, t) {
+ t.forEach(function(topic) {
+ topic_ids.push(topic.id);
+ topic_texts.push(topic.text);
+ });
+ if (!item.topics || !(item.topics.length > 0)) {
+ item.topics = topic_ids;
+ } else {
+ topic_ids.forEach(function(new_topic) {
+ merge(item.topics, new_topic);
});
- if (!item.topics || !(item.topics.length > 0)) {
- item.topics = topic_ids;
- } else {
- topic_ids.forEach(function(new_topic) {
- merge(item.topics, new_topic);
- });
- }
- //console.log("ADDED TOPIC IDS! "+item.topics.length);
- //console.log(self.message);
- //console.log("Topics: "+topic_texts.join(", "));
- item.topics = unique_ids(item.topics);
- item.commit("topics");
- item.save(function(err) {
- t.forEach(function(topic) {
- topic.save(function(err) {
- // err
- });
+ }
+ //console.log("ADDED TOPIC IDS! "+item.topics.length);
+ //console.log(self.message);
+ //console.log("Topics: "+topic_texts.join(", "));
+ item.topics = unique_ids(item.topics);
+ item.commit("topics");
+ item.save(function(err) {
+ t.forEach(function(topic) {
+ topic.save(function(err) {
+ // err
});
});
-
- done_with_topics();
});
+
+ done_with_topics();
+ });
- }
+ }
+
+ function done_with_topics() {
+ //console.log("calling back... "+item.topics.length);
- function done_with_topics() {
- //console.log("calling back... "+item.topics.length);
-
- rate_that_shit();
- }
+ rate_that_shit();
+ }
+
+ function rate_that_shit() {
+ var topic_ratings = 0;
+ var topic_count = 0;
+ var char_ratings = 0;
+ var char_count = 0;
+ var user_rating = 0;
+ var factors = [];
- function rate_that_shit() {
- var topic_ratings = 0;
- var topic_count = 0;
- var char_ratings = 0;
- var char_count = 0;
- var user_rating = 0;
- var factors = [];
-
- AI.findOne({_id: item.id})
- .populate("characteristics")
- .populate("topics")
- .populate("user")
- .run(function(err, _item) {
- if (!err && _item) {
- _item.topics.forEach(function(topic) {
- if (topic.ratings.overall > 0 || topic.ratings.overall < 0) {
- if (parseInt(topic.ratings.overall) != 0) {
- topic_ratings += parseFloat(topic.ratings.overall);
- topic_count++;
- }
+ AI.findOne({_id: item.id})
+ .populate("characteristics")
+ .populate("topics")
+ .populate("user")
+ .run(function(err, _item) {
+ if (!err && _item) {
+ _item.topics.forEach(function(topic) {
+ if (topic.ratings.overall > 0 || topic.ratings.overall < 0) {
+ if (parseInt(topic.ratings.overall) != 0) {
+ topic_ratings += parseFloat(topic.ratings.overall);
+ topic_count++;
}
- });
- if (topic_ratings != 0) {
- factors.push(topic_ratings/topic_count);
- }
-
- _item.characteristics.forEach(function(ch) {
- if (parseFloat(ch.ratings.overall) != 0) {
- char_ratings += parseFloat(ch.ratings.overall)*.2;
- char_count++;
- }
- });
- if (char_ratings != 0) {
- factors.push(char_ratings/char_count);
- }
-
- user_rating = parseInt(_item.user.closeness) || 0;
- if (_item.user.ratings && parseFloat(_item.user.ratings.overall) != 0) {
- user_rating += parseFloat(_item.user.ratings.overall);
- }
- if (user_rating != 0) {
- // it's so nice, i wanna push the same score twice
- factors.push(user_rating);
- factors.push(user_rating);
}
+ });
+ if (topic_ratings != 0) {
+ factors.push(topic_ratings/topic_count);
}
- topic_count = topic_count > 1 ? topic_count : 1;
- char_count = char_count > 1 ? char_count : 1;
- ratings.user = user_rating;
- ratings.topics = topic_ratings / topic_count;
- ratings.characteristics = char_ratings / char_count;
- var sum = 0;
- factors.forEach(function (f) { sum += f; });
- ratings.overall = factors.length > 0 ? sum/factors.length : 0;
- //console.log("RATINGS");
- //console.log(ratings);
- item.ratings = ratings;
- item.commit("ratings");
- cb(error, item);
+ _item.characteristics.forEach(function(ch) {
+ if (parseFloat(ch.ratings.overall) != 0) {
+ char_ratings += parseFloat(ch.ratings.overall)*.2;
+ char_count++;
+ }
+ });
+ if (char_ratings != 0) {
+ factors.push(char_ratings/char_count);
+ }
- });
- //rate_by_topics();
- //rate_by_characteristics();
- //rate_by_behavior();
+ user_rating = parseInt(_item.user.closeness) || 0;
+ if (_item.user.ratings && parseFloat(_item.user.ratings.overall) != 0) {
+ user_rating += parseFloat(_item.user.ratings.overall);
+ }
+ if (user_rating != 0) {
+ // it's so nice, i wanna push the same score twice
+ factors.push(user_rating);
+ factors.push(user_rating);
+ }
+ }
+ topic_count = topic_count > 1 ? topic_count : 1;
+ char_count = char_count > 1 ? char_count : 1;
+ ratings.user = user_rating;
+ ratings.topics = topic_ratings / topic_count;
+ ratings.characteristics = char_ratings / char_count;
+ var sum = 0;
+ factors.forEach(function (f) { sum += f; });
+ ratings.overall = factors.length > 0 ? sum/factors.length : 0;
- //Topic.find({_id: {"$in": item.topics}}, function (
- }
+ //console.log("RATINGS");
+ //console.log(ratings);
+ item.ratings = ratings;
+ item.commit("ratings");
+ cb(error, item);
+
+ });
+ //rate_by_topics();
+ //rate_by_characteristics();
+ //rate_by_behavior();
+
+ //Topic.find({_id: {"$in": item.topics}}, function (
}
- })(item, cb);
+ }
}
@@ -438,33 +437,6 @@ ActivityItemSchema.pre('save', function(next) {
next();
}
});
-
- /** /
- var regex = /\(?\bhttp:\/\/[-A-Za-z0-9+&@#\/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#\/%=~_()|]/gi;
- var matches = self.message.match(regex);
- if (matches) {
- console.log(matches);
- function unshorten_next () {
- if (matches.length == 0) {
- return finished();
- }
- var match = matches.pop();
- unshorten(match, function (url) {
- console.log("Unshortened: "+match+" => "+url);
- if (match != url) {
- self.message.replace(match, url);
- }
- unshorten_next();
- });
- }
- unshorten_next();
- function finished () {
- next();
- }
- } else {
- next();
- }
- /**/
});
function unshorten_urls(_message, cb) {
@@ -527,4 +499,5 @@ if (!String.prototype.replace_punctuation) {
mongoose.model('ActivityItem', ActivityItemSchema);
exports.ActivityItem = ActivityItemSchema;
+exports.analyze_me = analyze_me;
View
32 test/activity-item.js
@@ -1,23 +1,25 @@
-// suite('Array', function(){
-// setup(function(){
-// // ...
-// });
+var assert = require('assert')
+ , analyze_me = require('../models/ActivityItem').analyze_me
+ , mongoose = require('mongoose');
+
+suite('Natural Language', function() {
+ setup(function() {
+ mongoose.connect('mongodb://localhost/testdb');
+ });
+
+ suite('natural', function() {
+ });
+});
-// suite('#indexOf()', function(){
-// test('should return -1 when not present', function(){
-// assert.equal(-1, [1,2,3].indexOf(4));
-// });
-// });
-// });
-describe('Array', function(){
- before(function(){
+suite('Array', function(){
+ setup(function(){
// ...
});
- describe('#indexOf()', function(){
- it('should return -1 when not present', function(){
- [1,2,3].indexOf(4).should.equal(-1);
+ suite('#indexOf()', function(){
+ test('should return -1 when not present', function(){
+ assert.equal(-1, [1,2,3].indexOf(4));
});
});
});
View
5 test/examples.txt
@@ -0,0 +1,5 @@
+Example Tweets
+--------------
+
+Proud to announce our social network: http://www.cowerful.com/ - A resource for bovine enthusiasts looking to make moo friends.
+

No commit comments for this range

Something went wrong with that request. Please try again.