Skip to content

Commit

Permalink
Add support for the Hebrew language
Browse files Browse the repository at this point in the history
  • Loading branch information
miso-belica committed Nov 1, 2020
1 parent 695d723 commit 4b39817
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.9.0 (???)
- Added support for Hebrew language [#150](https://github.com/miso-belica/sumy/issues/150).

## 0.8.1 (2019-05-19)
- Open files for `PlaintextParser` in UTF-8 encoding [#123](https://github.com/miso-belica/sumy/pull/123)

Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"Japanese": ["tinysegmenter"],
"Chinese": ["jieba"],
"Korean": ["konlpy"],
"Hebrew": ["hebrew_tokenizer"],
},
packages=find_packages(),
package_data={"sumy": [
Expand Down Expand Up @@ -84,6 +85,7 @@
"Natural Language :: Portuguese",
"Natural Language :: Slovak",
"Natural Language :: Spanish",
"Natural Language :: Hebrew",

"Topic :: Education",
"Topic :: Internet",
Expand Down
194 changes: 194 additions & 0 deletions sumy/data/stopwords/hebrew.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
אבל
או
אולי
אותה
אותו
אותי
אותך
אותם
אותן
אותנו
אז
אחר
אחרות
אחרי
אחריכן
אחרים
אחרת
אי
איזה
איך
אין
איפה
איתה
איתו
איתי
איתך
איתכם
איתכן
איתם
איתן
איתנו
אך
אל
אלה
אלו
אם
אנחנו
אני
אס
אף
אצל
אשר
את
אתה
אתכם
אתכן
אתם
אתן
באיזומידה
באמצע
באמצעות
בגלל
בין
בלי
במידה
במקוםשבו
ברם
בשביל
בשעהש
בתוך
גם
דרך
הוא
היא
היה
היכן
היתה
היתי
הם
הן
הנה
הסיבהשבגללה
הרי
ואילו
ואת
זאת
זה
זות
יהיה
יוכל
יוכלו
יותרמדי
יכול
יכולה
יכולות
יכולים
יכל
יכלה
יכלו
יש
כאן
כאשר
כולם
כולן
כזה
כי
כיצד
כך
ככה
כל
כלל
כמו
כן
כפי
כש
לא
לאו
לאיזותכלית
לאן
לבין
לה
להיות
להם
להן
לו
לי
לכם
לכן
למה
למטה
למעלה
למקוםשבו
למרות
לנו
לעבר
לעיכן
לפיכך
לפני
מאד
מאחורי
מאיזוסיבה
מאין
מאיפה
מבלי
מבעד
מדוע
מה
מהיכן
מול
מחוץ
מי
מכאן
מכיוון
מלבד
מן
מנין
מסוגל
מעט
מעטים
מעל
מצד
מקוםבו
מתחת
מתי
נגד
נגר
נו
עד
עז
על
עלי
עליה
עליהם
עליהן
עליו
עליך
עליכם
עלינו
עם
עצמה
עצמהם
עצמהן
עצמו
עצמי
עצמם
עצמן
עצמנו
פה
רק
שוב
של
שלה
שלהם
שלהן
שלו
שלי
שלך
שלכה
שלכם
שלכן
שלנו
שם
תהיה
תחת
3 changes: 2 additions & 1 deletion sumy/nlp/stemmers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class Stemmer(object):
SPECIAL_STEMMERS = {
'czech': czech_stemmer,
'slovak': czech_stemmer,
'hebrew': null_stemmer,
'chinese': null_stemmer,
'japanese': null_stemmer,
'korean': null_stemmer
'korean': null_stemmer,
}

def __init__(self, language):
Expand Down
34 changes: 29 additions & 5 deletions sumy/nlp/tokenizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import division, print_function, unicode_literals

import re
import string
import zipfile

import nltk
Expand All @@ -13,10 +14,30 @@


class DefaultWordTokenizer(object):
"""NLTK tokenizer"""
def tokenize(self, text):
return nltk.word_tokenize(text)


class HebrewWordTokenizer:
"""https://github.com/iddoberger/awesome-hebrew-nlp"""
_TRANSLATOR = str.maketrans("", "", string.punctuation)

@classmethod
def tokenize(cls, text):
try:
from hebrew_tokenizer import tokenize
from hebrew_tokenizer.groups import Groups
except ImportError:
raise ValueError("Hebrew tokenizer requires hebrew_tokenizer. Please, install it by command 'pip install hebrew_tokenizer'.")

text = text.translate(cls._TRANSLATOR)
return [
word for token, word, _, _ in tokenize(text)
if token in (Groups.HEBREW, Groups.HEBREW_1, Groups.HEBREW_2)
]


class JapaneseWordTokenizer:
def tokenize(self, text):
try:
Expand Down Expand Up @@ -73,15 +94,17 @@ class Tokenizer(object):
}

SPECIAL_SENTENCE_TOKENIZERS = {
'hebrew': nltk.RegexpTokenizer(r'\.\s+', gaps=True),
'japanese': nltk.RegexpTokenizer('[^ !?。]*[!?。]'),
'chinese': nltk.RegexpTokenizer('[^ !?。]*[!?。]'),
'korean': KoreanSentencesTokenizer()
'korean': KoreanSentencesTokenizer(),
}

SPECIAL_WORD_TOKENIZERS = {
'hebrew': HebrewWordTokenizer(),
'japanese': JapaneseWordTokenizer(),
'chinese': ChineseWordTokenizer(),
'korean': KoreanWordTokenizer()
'korean': KoreanWordTokenizer(),
}

def __init__(self, language):
Expand All @@ -102,10 +125,11 @@ def _get_sentence_tokenizer(self, language):
try:
path = to_string("tokenizers/punkt/%s.pickle") % to_string(language)
return nltk.data.load(path)
except (LookupError, zipfile.BadZipfile):
except (LookupError, zipfile.BadZipfile) as e:
raise LookupError(
"NLTK tokenizers are missing. Download them by following command: "
'''python -c "import nltk; nltk.download('punkt')"'''
"NLTK tokenizers are missing or the language is not supported.\n"
"""Download them by following command: python -c "import nltk; nltk.download('punkt')"\n"""
"Original error was:\n" + str(e)
)

def _get_word_tokenizer(self, language):
Expand Down

0 comments on commit 4b39817

Please sign in to comment.