-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
looking for house in sentences in brain
- Loading branch information
1 parent
2ee1b4a
commit 2cd7045
Showing
2 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
module.exports = function(originalText){ | ||
return gladys.house.getAll() | ||
.then(function(houses){ | ||
|
||
var result = []; | ||
var replaceRegex = ''; | ||
|
||
// foreach house, we verify if the house is present in the sentence | ||
houses.forEach(function(house){ | ||
if(present(originalText, house.name)){ | ||
result.push(house); | ||
if(replaceRegex.length > 0) replaceRegex += '|'; | ||
replaceRegex += house.name; | ||
} | ||
}); | ||
|
||
var text = originalText; | ||
|
||
if(replaceRegex.length > 0) { | ||
text = originalText.replace(new RegExp(replaceRegex, 'g'), '%HOUSE%'); | ||
} | ||
|
||
return { | ||
text, | ||
houses: result | ||
}; | ||
}); | ||
}; | ||
|
||
|
||
|
||
/** | ||
* Return true if the houseName is present in the sentence | ||
*/ | ||
function present(text, type){ | ||
return (text.toLowerCase().indexOf(type.toLowerCase()) > -1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters