Skip to content

Commit

Permalink
Merge pull request #3 from kamoo1/dev
Browse files Browse the repository at this point in the history
feat: new features
  • Loading branch information
kamoo1 authored Nov 4, 2024
2 parents b8521f1 + 4b1f82b commit 0589971
Show file tree
Hide file tree
Showing 7 changed files with 1,011 additions and 789 deletions.
27 changes: 22 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
SHELL := /bin/bash
PATH_SRC := ankiwordfreq
PATH_VENDORS := $(PATH_SRC)/vendors
ANKI_ADDON_PATH := C:/Users/$(USER)/AppData/Roaming/Anki2/addons21/ankiwordfreq
ANKI_ADDON_PATH_WIN := C:/Users/$(USER)/AppData/Roaming/Anki2/addons21/ankiwordfreq
ANKI_ADDON_PATH_LINUX := $$HOME/.local/share/Anki2/addons21/ankiwordfreq
ANKI_ADDON_PATH_MACOS := $$HOME/Library/Application\ Support/Anki2/addons21/ankiwordfreq

ifeq ($(shell uname), Linux)
ANKI_ADDON_PATH := $(ANKI_ADDON_PATH_LINUX)
else ifeq ($(shell uname), Darwin)
ANKI_ADDON_PATH := $(ANKI_ADDON_PATH_MACOS)
else
ANKI_ADDON_PATH := $(ANKI_ADDON_PATH_WIN)
endif


TOP_DEPS := wordfreq
PLATFORMS := win_amd64 manylinux2014_x86_64 macosx_10_9_x86_64
PLATFORMS := win_amd64 manylinux2014_x86_64 macosx_10_13_x86_64
CALL_GFD := "scripts/get_full_deps.sh"
CALL_MV := "scripts/make_vendor.sh"
CALL_FMD := "scripts/fix_mecab_dll.sh"
Expand All @@ -13,14 +25,14 @@ all:
mkdir -p dist && \
$(MAKE) vendors && \
FILE=$$(poetry version | tr ' ' '_').ankiaddon && \
cd $(PATH_SRC) && zip -r ../dist/$$FILE *
cd $(PATH_SRC) && zip -q -r ../dist/$$FILE *

.PHONY: cjk
cjk:
mkdir -p dist && \
$(MAKE) vendors && \
FILE=$$(poetry version | tr ' ' '_')_cjk.ankiaddon && \
cd $(PATH_SRC) && zip -r ../dist/$$FILE *
cd $(PATH_SRC) && zip -q -r ../dist/$$FILE *



Expand All @@ -29,6 +41,7 @@ vendors:
rm -rf $(PATH_VENDORS) && mkdir -p $(PATH_VENDORS) && \
DEPS=$$($(CALL_GFD) $(TOP_DEPS)) && \
for dep in $$DEPS; do \
echo -e "\n\n" && \
$(CALL_MV) $(PATH_VENDORS) "$$dep" $(PLATFORMS); \
done && \
$(CALL_FMD) $(PATH_VENDORS)
Expand All @@ -37,5 +50,9 @@ vendors:

.PHONY: link
link:
rm -rf $(ANKI_ADDON_PATH)
$(MAKE) unlink
ln -s -f $(PWD)/$(PATH_SRC) $(ANKI_ADDON_PATH)

.PHONY: unlink
unlink:
rm -rf $(ANKI_ADDON_PATH)
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ Supported languages listed [here](https://github.com/rspeer/wordfreq/blob/ce5307
1. Select target cards in the browser, they should have a field named "Front" (also configurable).
1. Right click and choose your language under "Word Frequency" tab to update the frequency field.

## Config
| Field | Description |
| --- | --- |
| `input_field` | The name of the field containing the text to be analyzed. |
| `output_field` | The name of the field to be updated with the word frequency. |
| `output_is_inverted` | Whether the frequency should be inverted, i.e. {output_upper_bound} - {frequency}. |
| `output_upper_bound` | The maximum frequency value, anything above 8 is safe. |
| `listed_languages` | A list of [language codes](https://github.com/rspeer/wordfreq/blob/ce5307748723ddfb47eec26c3ece2eb8216c897a/README.md#sources-and-supported-languages) you want to display in Anki Word Frequency menu, e.g. `["en", "zh", "de"]`. An empty list will display all available options. |

## Known Issues
- For Chinese Japanese and Korean (CJK) support, you can find a CJK version in [GitHub releases](https://github.com/kamoo1/anki-word-freq/releases). It's too large for AnkiWeb.
- Tested on Windows, should support most variants of Linux and MacOS.
- Tested on Windows and Linux, should be compatible with macOS.
- Some custom tokenizers in the dependencies write logs to *stderr* (e.g. `jieba`), this will get displayed in a error popup window in Anki, but can be safely ignored.
22 changes: 19 additions & 3 deletions ankiwordfreq/awf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__all__ = ("main",)
from logging import getLogger
from enum import Enum
from typing import Callable
from functools import partial
Expand All @@ -15,6 +16,8 @@
from aqt.qt import QAction, QMenu
from anki.utils import pointVersion

logger = getLogger("awf")

# TODO: future-proof
if pointVersion() >= 231000:
VER_2310 = True
Expand All @@ -26,7 +29,9 @@
import mecab_ko_dic
import jieba
import ipadic
except ImportError:
except ImportError as e:
logger.error(e)
logger.warning("Not CJK")
IS_CJK = False
else:
IS_CJK = True
Expand Down Expand Up @@ -92,8 +97,13 @@ def add_word_freq(browser: Browser, lang_code: str, *_) -> None:
note = mw.col.get_note(nid)
front = note[input_field]
freq = zipf_frequency(front, lang_code)
note[output_field] = "{:.2f}".format(freq)
note.flush()
output = (
config["output_upper_bound"] - freq
if config["output_is_inverted"] is True
else freq
)
note[output_field] = "{:.2f}".format(output)
mw.col.update_note(note)

mw.progress.finish()
mw.reset()
Expand All @@ -105,12 +115,18 @@ def make_add_word_freq(browser: Browser, lang_code: str) -> Callable[[], None]:


def setup_menu(browser: Browser) -> None:
config = mw.addonManager.getConfig(__name__)
listed_langs = [
lang.lower() for lang in config["listed_languages"]
]
menu_notes = browser.form.menu_Notes
menu_notes.addSeparator()
menu_awf = QMenu("Word Frequency", parent=menu_notes)
menu_awf.setObjectName("menu_awf")

for lang in Lang:
if listed_langs and lang.value not in listed_langs:
continue
act = QAction(f"Add Frequency ({lang.name})", parent=menu_awf)
act.setObjectName(f"menu_awf_{lang.name}")
qconnect(act.triggered, make_add_word_freq(browser, lang.value))
Expand Down
5 changes: 4 additions & 1 deletion ankiwordfreq/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"input_field": "Front",
"output_field": "Frequency"
"output_field": "Frequency",
"output_is_inverted": false,
"output_upper_bound": 10,
"listed_languages": []
}
Loading

0 comments on commit 0589971

Please sign in to comment.