Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to use library in webbrowser with requiring stopwords files #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 70 additions & 6 deletions lib/stopwords.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 37 additions & 4 deletions src/stopwords.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,53 @@ _ = require('lodash')

cache = {}

getStopwords = (lang) ->
switch (lang)
when 'ar' then require('../data/stopwords/stopwords-ar.txt')
when 'bg' then require('../data/stopwords/stopwords-bg.txt')
when 'cs' then require('../data/stopwords/stopwords-cs.txt')
when 'da' then require('../data/stopwords/stopwords-da.txt')
when 'de' then require('../data/stopwords/stopwords-de.txt')
when 'en' then require('../data/stopwords/stopwords-en.txt')
when 'es' then require('../data/stopwords/stopwords-es.txt')
when 'fi' then require('../data/stopwords/stopwords-fi.txt')
when 'fr' then require('../data/stopwords/stopwords-fr.txt')
when 'hu' then require('../data/stopwords/stopwords-hu.txt')
when 'id' then require('../data/stopwords/stopwords-id.txt')
when 'it' then require('../data/stopwords/stopwords-it.txt')
when 'ko' then require('../data/stopwords/stopwords-ko.txt')
when 'nb' then require('../data/stopwords/stopwords-nb.txt')
when 'nl' then require('../data/stopwords/stopwords-nl.txt')
when 'no' then require('../data/stopwords/stopwords-no.txt')
when 'pl' then require('../data/stopwords/stopwords-pl.txt')
when 'pt' then require('../data/stopwords/stopwords-pt.txt')
when 'ru' then require('../data/stopwords/stopwords-ru.txt')
when 'sv' then require('../data/stopwords/stopwords-sv.txt')
when 'th' then require('../data/stopwords/stopwords-th.txt')
when 'tr' then require('../data/stopwords/stopwords-tr.txt')
when 'zh' then require('../data/stopwords/stopwords-zh.txt')
else require('../data/stopwords/stopwords-en.txt')

getFilePath = (language) ->
path.join(__dirname, "..", "data", "stopwords", "stopwords-#{language}.txt")

# Given a language, loads a list of stop words for that language
# and then returns which of those words exist in the given content
module.exports = stopwords = (content, language = 'en') ->
filePath = getFilePath(language)
hasFs = 'existsSync' in fs
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest this line should be hasFs = fs and fs.existsSync.

The current line fails because the compiled JavaScript line treats fs as an array with length, and fs.length does not exist:

function in$(member, list) {
  for (var i = 0, length = list.length; i < length; ++i) // <-- failure at list.length
    if (i in list && list[i] === member)
      return true;
  return false;
}
hasFs = in$('existsSync', fs);

The proposed change succeeds in JavaScript as hasFs = fs && fs.existsSync. This should fix the buildbots.

I would PR to the branch myself, but I'm unfamiliar with CoffeeScript and can't build.


if hasFs
filePath = getFilePath(language)

if !fs.existsSync(filePath)
console.error("WARNING: No stopwords file found for '#{language}' - defaulting to English!")
filePath = getFilePath('en')
if !fs.existsSync(filePath)
console.error("WARNING: No stopwords file found for '#{language}' - defaulting to English!")
filePath = getFilePath('en')

if cache.hasOwnProperty(language)
stopWords = cache[language]
else if !hasFs
stopWords = getStopwords(language)
cache[language] = stopWords
else
stopWords = fs.readFileSync(filePath).toString().split('\n')
.filter((s) -> s.length > 0)
Expand Down