-
-
Notifications
You must be signed in to change notification settings - Fork 1
Add basic builtin function WordTranslation
#38
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b5a5a05
Add basic WordTranslation
rocky 41c40ec
Use github mathics-scanner in CI
rocky 8a5a21c
Try to fix up build
rocky f74dc1d
Improve bcp47 loading?
rocky d3a52ff
Fix up CI?
rocky b4ec2e7
Add argument checking to WordTranslation
rocky 4a57a05
Use class variable requires
rocky 40278ef
Merge branch 'master' into add-WordTranslation
rocky 75afa87
Merge stuff
rocky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| *~ | ||
| .python-version | ||
| /.nltk_data | ||
| /.python-version | ||
| /ChangeLog | ||
| /ChangeLog-spell-corrected | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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,21 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Load bcp47 which is needed to support Mathics3 builtin-function WordTranslation. | ||
| """ | ||
| import os | ||
|
|
||
| import nltk | ||
|
|
||
| # choose a local data dir so we don't require system-wide write access | ||
| data_dir = os.environ.get("NLTK_DATA", os.path.join(os.getcwd(), ".nltk_data")) | ||
| os.makedirs(data_dir, exist_ok=True) | ||
|
|
||
| # ensure nltk knows about it | ||
| if data_dir not in nltk.data.path: | ||
| nltk.data.path.append(data_dir) | ||
|
|
||
| # only download if missing | ||
| try: | ||
| nltk.data.find("corpora/bcp47") | ||
| except LookupError: | ||
| nltk.download("bcp47", download_dir=data_dir, quiet=False) |
This file contains hidden or 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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,116 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| Language Translation | ||
|
|
||
| """ | ||
|
|
||
| # This is under Text Normalization in WR. But also in Natural Language Processing, | ||
| # and Linguistic Data. I put here because is the only module that uses langid and pycountry | ||
| # modules. | ||
| # | ||
| # TODO: WordTranslation, TextTranslation | ||
|
|
||
| from typing import Union | ||
|
|
||
| import langid # see https://github.com/saffsd/langid.py | ||
| import pycountry | ||
| from mathics.core.atoms import String | ||
| from mathics.core.builtin import Builtin | ||
| from mathics.core.evaluation import Evaluation | ||
| from mathics.core.list import ListExpression | ||
| from mathics.core.symbols import Symbol | ||
| from mathics.core.systemsymbols import SymbolFailed | ||
| from nltk.corpus import wordnet as wn | ||
| from nltk.corpus.reader.wordnet import WordNetError | ||
|
|
||
| try: | ||
| import nltk.langnames as lgn | ||
| except ImportError: | ||
| have_lng = False | ||
| else: | ||
| have_lng = True | ||
|
|
||
| sort_order = "Language Translation" | ||
|
|
||
|
|
||
| class LanguageIdentify(Builtin): | ||
| """ | ||
| <url>:WMA link: | ||
| https://reference.wolfram.com/language/ref/LanguageIdentify.html</url> | ||
|
|
||
| <dl> | ||
| <dt>'LanguageIdentify'[$text$] | ||
| <dd>returns the name of the language used in $text$. | ||
| </dl> | ||
|
|
||
| >> LanguageIdentify["eins zwei drei"] | ||
| = German | ||
| """ | ||
|
|
||
| summary_text = "determine the predominant human language in a string" | ||
|
|
||
| def eval(self, text: String, evaluation: Evaluation) -> Union[Symbol, String]: | ||
| "LanguageIdentify[text_String]" | ||
|
|
||
| # an alternative: https://github.com/Mimino666/langdetect | ||
|
|
||
| code, _ = langid.classify(text.value) | ||
| language = pycountry.languages.get(alpha_2=code) | ||
| if language is None: | ||
| return SymbolFailed | ||
| return String(language.name) | ||
|
|
||
|
|
||
| if have_lng: | ||
|
|
||
| # FIXME generalize | ||
| class WordTranslation(Builtin): | ||
| """ | ||
| <url>:WMA link: | ||
| https://reference.wolfram.com/language/ref/WordTranslation.html</url> | ||
|
|
||
| <dl> | ||
| <dt>'WordTranslation'[word, $lang$] | ||
| <dd>returns a list of translation for $word$ into $lang$. | ||
| </dl> | ||
|
|
||
| >> WordTranslation["dog", "French"] | ||
| = ... | ||
| """ | ||
|
|
||
| # Set checking that the number of arguments required is one. | ||
| eval_error = Builtin.generic_argument_error | ||
| expected_args = 2 | ||
|
|
||
| summary_text = "give word translations" | ||
|
|
||
| def eval( | ||
| self, word: String, lang: String, evaluation: Evaluation | ||
| ) -> ListExpression: | ||
| "WordTranslation[word_String, lang_String]" | ||
| return eval_WordTranslation(word.value, lang.value) | ||
|
|
||
| def eval_WordTranslation(word: str, language_name: str): | ||
| """ | ||
| Return a list of translations of `word` in English to `language_name`. | ||
| """ | ||
|
|
||
| # Convert "language_name" using NLTK's langnames utility | ||
| # to its 3-letter ISO 639-3 code. | ||
| iso_code = lgn.langcode(language_name, typ=3) | ||
|
|
||
| if iso_code is None: | ||
| return SymbolFailed | ||
|
|
||
| synsets = wn.synsets(word) | ||
| translations = set() | ||
|
|
||
| for ss in synsets: | ||
| # Pass the converted code to WordNet | ||
| try: | ||
| for lemma in ss.lemmas(lang=iso_code): | ||
| translations.add(lemma.name()) | ||
| except WordNetError: | ||
| return SymbolFailed | ||
|
|
||
| return ListExpression(*[String(word) for word in translations]) | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this conditional load, we could use the attribute 'requires'. See for example https://github.com/Mathics3/mathics-core/blob/c5388232f77b053012f20b7fe4ab2e53932d3c16/mathics/builtin/image/misc.py#L240
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks - done.