-
Notifications
You must be signed in to change notification settings - Fork 2
/
answer.coffee
35 lines (24 loc) · 900 Bytes
/
answer.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# answer.coffee
sentenceSimilarity = (stemA, stemB) ->
_.intersection(stemA, stemB).length
class @answer
constructor: () ->
@tag = new POSTagger().tag
@tokenize = new Lexer().lex
@stem = natural.PorterStemmer.tokenizeAndStem
@sentences = [ ]
@stemmedSentences = [ ]
read: (article) ->
stem = @stem
article = article.replace(/Dr./g, "Dr")
sentences = @sentences.concat article.split(".")
stemmed = _.map(sentences, (text) -> stem text )
@sentences = @sentences.concat sentences
@stemmedSentences = @stemmedSentences.concat stemmed
match: (sentence) ->
corpus = @sentences
stemmedCorpus = @stemmedSentences
stemA = @stem sentence
scores = _.map(stemmedCorpus, (stemB) -> sentenceSimilarity(stemA, stemB))
ranked = _.sortBy(_.zip(scores, corpus), (x) -> -1 * x[0])
_.filter(ranked, (score) -> (score[0] > 0))