Skip to content

Commit

Permalink
confirm_generation function finalized and MeaningsSpider return value…
Browse files Browse the repository at this point in the history
…s changed
  • Loading branch information
mohamedmujtabaraza committed Mar 29, 2022
1 parent b3b2fbe commit 26a0935
Showing 5 changed files with 48 additions and 36 deletions.
Binary file modified src/__pycache__/app.cpython-38.pyc
Binary file not shown.
24 changes: 12 additions & 12 deletions src/app.kv
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
MDToolbar:
title: 'Vocab to Anki'
anchor_title: 'center'
right_action_items: [["refresh", lambda x: app.get_running_app().restart()]]
# right_action_items: [["refresh", lambda x: app.get_running_app().restart()]]
elevation: 10
FloatLayout:
# MDFlatButton:
@@ -38,7 +38,7 @@
id: dict_dropdown
text: "Browse"
font_style: 'Button'
pos_hint: {"center_x": 0.5, 'center_y': 0.9}
pos_hint: {"center_x": 0.5, 'center_y': 0.85}
on_release: root.open_dropdown(dict_dropdown=True)
MDTextField:
id: word_input
@@ -48,26 +48,26 @@
helper_text_mode: "on_focus"
# icon_right: "clipboard-file"
# icon_right_color: app.theme_cls.primary_color
pos_hint: {'center_x': 0.4, 'center_y': 0.75}
pos_hint: {'center_x': 0.4, 'center_y': 0.7}
size_hint_x: None
width: 240
MDIconButton:
icon: "clipboard-file"
pos_hint: {"center_x": .9, "center_y": .75}
pos_hint: {"center_x": .9, "center_y": .7}
on_release: word_input.text = Clipboard.paste()
MDLabel:
text: "Select Pronunciation Accent"
theme_text_color: 'Primary'
font_style: 'Body1'
halign: 'center'
pos_hint: {'center_x': 0.5, 'center_y': 0.63}
# MDLabel:
# text: "Select Pronunciation Accent"
# theme_text_color: 'Primary'
# font_style: 'Body1'
# halign: 'center'
# pos_hint: {'center_x': 0.5, 'center_y': 0.58}
GridLayout:
cols: 2
row_force_default: True
row_default_height: 40
col_force_default: True
col_default_width: 180
pos_hint: {'center_x': 0.26, 'center_y': 0.5}
pos_hint: {'center_x': 0.26, 'center_y': 0.45}
size_hint: (None, None)
MDLabel:
text: "English (United Kingdom)"
@@ -81,7 +81,7 @@
MDRectangleFlatButton:
text: 'Generate Anki Flashcard'
font_style: 'Button'
pos_hint: {'center_x': 0.5, 'center_y': 0.35}
pos_hint: {'center_x': 0.5, 'center_y': 0.2}
on_release: root.show_data()


32 changes: 20 additions & 12 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@ def run_spider(spider, *args):
# ----------------------------------- KIVY -------------------------------------


Window.size = (500, 500)
Window.size = (500, 400)
Builder.load_file("src/app.kv")
sm = ScreenManager()

@@ -95,11 +95,13 @@ class MeaningsPanelContent(MDBoxLayout):
def __init__(self, *args, **kwargs):
super().__init__()
menu_instance = args[0]
for meaning in args[1]['more_words']:
section_id = args[1]['cid']
more_words = args[1]['more_words'][section_id[0]]
for key, value in more_words.items():
# root.ids.meanings_screen.ids.meanings_panel
self.add_widget(OneLineListItem(
text=meaning,
on_release=lambda x, y=args[1]: menu_instance.confirm_generation(y)
text=value,
on_release=lambda x, y=section_id[0], z=(key, value): menu_instance.confirm_generation(y, z)
))


@@ -305,22 +307,25 @@ def checkbox_click(self, instance, value, tld):
if value is True:
self.tld = tld

def confirm_generation(self, meaning):
confirm_button = MDFlatButton(text="Confirm", on_release=lambda x, y=meaning: self.generate_flashcard(x, y))
def confirm_generation(self, section_id, meaning):
meaning_text = meaning[1] if type(meaning) is tuple else meaning
confirm_button = MDFlatButton(
text="Confirm", on_release=lambda x, y=section_id, z=meaning: self.generate_flashcard(x, y, z)
)
close_button = MDFlatButton(text="Close", on_release=self.close_dialog)
if self.dialog:
self.dialog.dismiss()
self.dialog = MDDialog(
title="Confirm generation",
text=f"Do you want to generate Anki flashcard for \"{meaning['word']} {meaning['gw']}\"?",
text=f"Do you want to generate Anki flashcard for \"{meaning_text}\"?",
size_hint=(0.7, 1),
buttons=[close_button, confirm_button]
)
self.dialog.open()

def generate_flashcard(self, btn, meaning):
def generate_flashcard(self, btn, section_id, meaning):
# run_spider(CambridgeSpider, gcurl, self.tld, self.timestamp)
print(meaning) # {'cid': 'cald4-1-1', 'word': 'run', 'gw': '(GO QUICKLY)', 'pos': 'verb'}
print(section_id, meaning) # {'cid': 'cald4-1-1', 'word': 'run', 'gw': '(GO QUICKLY)', 'pos': 'verb'}

def show_data(self):
# word_url = self.word_url.text
@@ -378,15 +383,17 @@ def show_data(self):
# print(CONTAINER['meanings'])
meanings_screen = self.manager.get_screen("meanings_screen")
for meaning in CONTAINER['meanings']:
section_id = meaning['cid']
word = meaning['word']
guide_word = meaning['gw']
part_of_speech = meaning['pos']
if not meaning['more_words']:
meaning_text = word + " " + guide_word
if not meaning['more_words'][section_id[0]]:
meanings_screen.ids.meanings_container.add_widget(
TwoLineListItem(
text=f"{word} {guide_word}",
text=meaning_text,
secondary_text=f"{part_of_speech}",
on_release=lambda x, y=meaning: self.confirm_generation(y)
on_release=lambda x, y=section_id[0], z=meaning_text: self.confirm_generation(y, z)
)
)
else:
@@ -447,6 +454,7 @@ def on_start(self):

class MyApp(MDApp):
def build(self):
self.title = 'Vocab to Anki'
sm.add_widget(MenuScreen(name='menu_screen'))
sm.add_widget(MeaningsScreen(name='meanings_screen'))
self.theme_cls.primary_palette = "Blue"
Binary file modified src/dict_scraper/spiders/__pycache__/cambridge.cpython-38.pyc
Binary file not shown.
28 changes: 16 additions & 12 deletions src/dict_scraper/spiders/cambridge.py
Original file line number Diff line number Diff line change
@@ -271,12 +271,13 @@ def parse(self, response):
last_true_section_id = None
for section in sections:
section_id = section.css(".cid::attr(id)").extract()
more_words = {section_id[0]: {}}

# dphrase_block = section.css(".dphrase-block").extract()
parts_of_speech = section.css(".dsense_pos").extract()
if not parts_of_speech:
in_dsense = False
# print('not in_dsense:', section_id)
print('not in_dsense:', section_id)
word = section.css(".dphrase-title b").css("::text").extract_first()
guide_word = ''
part_of_speech = response.css(f"#{section_id[0]}~ .dpos-h .dpos").css("::text").extract_first()
@@ -388,8 +389,17 @@ def parse(self, response):
# ignore this word
# else meaning found then:
# keep this word
more_words = []
if len(section_id) > 1:
extracted_meanings = section.css(".dsense_b > .ddef_block .ddef_d").css("::text").extract()
meanings_list = ''.join(extracted_meanings).split(':')[:-1]

if len(section_id) <= 1:
if len(meanings_list) > 1:
for i in range(len(meanings_list)):
more_words[section_id[0]][i + 1] = meanings_list[i]
else:
if meanings_list:
for i in range(len(meanings_list)):
more_words[section_id[0]][i + 1] = meanings_list[i]
for bid in section_id[1:]:
blue_block_title = ''.join(
section.css(f"#{bid}~ .dphrase_h b").css("::text").extract()
@@ -398,18 +408,12 @@ def parse(self, response):
blue_block_meaning = ''.join(
section.css(f"#{bid}~ .dphrase_b .ddef_d").css("::text").extract()
)[:-1]
more_words.append(blue_block_meaning)
more_words[section_id[0]][bid] = blue_block_meaning
else:
more_words.append(blue_block_title)
extracted_meanings = section.css(".dsense_b > .ddef_block .ddef_d").css("::text").extract()
meanings_list = ''.join(extracted_meanings).split(':')[:-1]
if len(meanings_list) > 1:
# print(len(extracted_meanings), meanings_list, len(meanings_list))
more_words.extend(meanings_list)

more_words[section_id[0]][bid] = blue_block_title
# if word has multiple meanings:
# create another instances of those meanings
# print('in_dsense:', section_id)
print('in_dsense:', section_id)
word = section.css(".dsense_hw").css("::text").extract_first()
guide_word = '(' + section.css(".dsense_gw span::text").extract_first() + ')'
# b = section.css("b").css("::text").extract()

0 comments on commit 26a0935

Please sign in to comment.