def create_syllable_guide(self):
     syllable_count = SortedStringTrie()
     syllable_pronounciation = SortedStringTrie()
     for line in self.dictionary:
         index_space = line.find(" ")
         word = line[:index_space]
         pronounciation = line[index_space + 2:]
         syllables = self.count_syllables(pronounciation)
         syllable_count.__setitem__(word, syllables)
         syllable_pronounciation.__setitem__(word, pronounciation)
     return syllable_count, syllable_pronounciation
Beispiel #2
0
 def setUp(self):
     """
     测试前准备环境的搭建(setUp)
     :return: 
     """
     self.words = 'an ant all allot alloy aloe are ate be'.split()
     self.trie = SortedStringTrie(zip(self.words, range(len(self.words))))
Beispiel #3
0
 def setUp(self):
     self.words = 'an ant all allot alloy aloe are ate be'.split()
     self.trie = SortedStringTrie(zip(self.words, range(len(self.words))))
class application(tkinter.Frame):
    top_frame = None
    homepage_frame = None
    results_frame = None
    results_text = None
    __s_engine = search_engine.crawler()
    bg_image = os.path.join('resources', 'background_1.jpg')
    search_history = os.path.join('data', 'search_history.txt')
    search_freq = os.path.join('data', 'search_freq.txt')
    history = SortedStringTrie()

    def __init__(self, master):
        #for GUI
        tkinter.Frame.__init__(self, master)
        self.master = master
        self.search_query = tkinter.StringVar()
        self.search_query.trace("w", self.update_listbox)
        self.initUI()
        #for loading search history
        self.__restore_history()
        #for loading data into crawler
        self.__s_engine.restore()
        self.__s_engine.calc_pagerank(30)

    def initUI(self):
        self.master.title("SPIDER")
        self.master.state('zoomed')
        self.grid()

        #set icon
        icon = tkinter.PhotoImage(file=os.path.join('resources', "logo_t.png"))
        self.master.tk.call('wm', 'iconphoto', self.master._w, icon)

        application.top_frame = tkinter.Frame(self.master,
                                              bg='black',
                                              bd=3,
                                              highlightcolor='blue')
        application.top_frame.grid(row=0)

        #tool bar on top of master
        self.tabs = tkinter.Label(application.top_frame,
                                  text="reserved for tabs")
        self.tabs.grid(row=0)

        self.options = tkinter.PhotoImage(
            file=os.path.join('resources', "options_3.png"))
        self.options = self.options.subsample(20, 20)
        self.options_button = tkinter.Button(application.top_frame,
                                             relief='flat',
                                             image=self.options,
                                             command=self.hide)
        self.options_button.grid(row=1, column=8)

        self.bookmark = tkinter.PhotoImage(
            file=os.path.join('resources', "bookmark_1.png"))
        self.bookmark = self.bookmark.subsample(40, 40)
        self.bookmark_button = tkinter.Button(application.top_frame,
                                              relief='flat',
                                              bg='white',
                                              image=self.bookmark,
                                              command=self.hide)
        self.bookmark_button.grid(row=1, column=7)

        self.search_box1 = tkinter.Entry(application.top_frame,
                                         textvariable=self.search_query,
                                         bg='white',
                                         fg='black',
                                         selectborderwidth=2,
                                         width=200)
        self.search_box1.grid(row=1, column=0)
        self.search_box1.bind('<Return>', self.__get)
        #self.search_box.bind('<Configure>',self._resize_search_box)

        #homepage
        self.homepage_frame = tkinter.Frame()
        self.homepage = self.create_homepage()
        self.homepage_frame.grid(row=2)

        #results page
        self.results_frame = tkinter.Frame()

    def hide(self):
        self.frame.pack_forget()

    '''def _resize_search_box(self,event):
        new_width = event.width
        self.search_box.destroy()
        #if new_width > 70:
        self.search_box = tkinter.Entry(application.top_frame,bg = 'white',fg = 'black',selectborderwidth = 1,bd = 10,font = ("COPRGTL.TTF"),relief = 'flat',width = new_width)
        #else:
         #   self.search_box = tkinter.Entry(application.top_frame,bg = 'white',fg = 'black',selectborderwidth = 1,bd = 4,font = ("COPRGTL.TTF"),relief = 'flat',width = 77)'''

    def create_homepage(self):

        background_image = Image.open(self.bg_image)
        background_image = background_image.resize((1536, 841))
        self.background_photo = ImageTk.PhotoImage(background_image)
        self.background_label = tkinter.Label(self.homepage_frame,
                                              image=self.background_photo)
        self.background_label.grid(row=1)

        #name
        self.name = tkinter.Label(self.homepage_frame,
                                  text="   ADS ",
                                  bg='black',
                                  fg='white',
                                  font=("SHOWG.TTF", 60))
        self.name.grid()
        self.name.place(x=620, y=275)
        #search box
        self.search_box = tkinter.Entry(self.homepage_frame,
                                        textvariable=self.search_query,
                                        bg='white',
                                        fg='black',
                                        selectborderwidth=1,
                                        bd=6,
                                        font=("COPRGTL.TTF", 16),
                                        relief='flat',
                                        width=40)
        self.search_box.bind('<Return>', self.__get)
        self.search_box.grid()
        self.search_box.place(x=525, y=400)  #centre 541,403.5
        #search image
        self.search_image = tkinter.PhotoImage(file=os.path.join(
            'resources', "search_4.png"))  #1-16 /4 - 8
        self.search_image = self.search_image.subsample(8, 8)
        self.search_button = tkinter.Button(self.homepage_frame,
                                            image=self.search_image,
                                            bg='white',
                                            relief='flat')
        self.search_button.grid(row=1)
        self.search_button.place(x=1020, y=400)
        #listbox
        self.listbox = tkinter.Listbox(self.homepage_frame,
                                       selectborderwidth=2,
                                       width=0,
                                       height=0)
        #self.listbox.bind('<Configure>',self.resize_listbox)
        self.listbox.grid(row=1)
        self.listbox.place(x=525, y=439)

    def __get(self, event):
        input_str = self.search_box.get()
        if input_str in self.history:
            self.history[input_str] += 0.1
        else:
            self.history[input_str] = 0.1
        results = self.__s_engine.query(input_str)
        self.homepage_frame.grid_forget()
        self.resultspage = self.create_resultspage(results)
        self.results_frame.grid()

    def update_listbox(self, *args):
        text = self.search_query.get()
        if text.isprintable():
            relevent_words = self.__from_history(
                text)  #search the term from history ;return a list
        self.listbox.delete(0, self.listbox.size())
        for each_item in relevent_words:
            self.listbox.insert(self.listbox.size(), each_item)

    def __from_history(self, query):
        if query in self.history:
            return sorted(self.history, key=self.history.values)
        return

    def create_resultspage(self, results):
        if self.results_text:
            self.results_text.grid_forget()
        self.results_text = tkinter.Label(self.results_frame, text="results\n")
        self.results_text.grid()
        self.results = tkinter.Label(self.results_frame, font=('', 20))
        self.results.grid(row=1, column=0)
        s = ''
        for each_url in results:
            s += each_url + '\n'
        #   arr = box(self.results_frame,each_url)
        self.results.config(text=s)

    def __restore_history(self):
        words_file = open(search_history)
        freq_file = open(search_freq)
        words = words_file.read()
        freq = freq_file.read()
        words_list = []
        freq_list = []
        if words:
            words_list = json.loads(words)
        if freq:
            freq_list = json.loads(freq)
        words_file.close()
        freq_file.close()
        for loop_var in range(len(words_list)):
            self.history[words_list[loop_var]] = freq_list[loop_var]

    def store_history(self):
        words_file = open(search_history, 'w')
        freq_file = open(search_freq, 'w')
        words_list = self.history.keys()
        freq_list = self.history.values()
        words_file.write(json.dumps(words_list))
        freq_file.write(json.dumps(freq_list))
        words_file.close()
        freq_file.close()
Beispiel #5
0
 def reset(cls):
     cls._shared_state.clear()
     cls._arg_trie = SortedStringTrie()
     cls._registries.clear()
Beispiel #6
0
class _Repository:
    """Copied from https://stackoverflow.com/questions/6255050/python-thinking-of-a-module-and-its-variables-as-a-singleton-clean-approach."""

    _shared_state = dict()
    _arg_trie = SortedStringTrie()
    _registries = dict()

    @classmethod
    def reset(cls):
        cls._shared_state.clear()
        cls._arg_trie = SortedStringTrie()
        cls._registries.clear()

    def __init__(self):
        self.__dict__ = self._shared_state

    def add_argument(self,
                     name,
                     *aliases,
                     scope=None,
                     dtype=str,
                     default=None,
                     nargs=1,
                     msg='',
                     choices=None):
        if scope is None:
            raise ArgumentScopeNotSupplied(
                'You have to explicitly set scope to a value.')

        arg = Argument(name,
                       *aliases,
                       scope=scope,
                       dtype=dtype,
                       default=default,
                       nargs=nargs,
                       msg=msg,
                       choices=choices)
        if arg.name in self.__dict__:
            raise DuplicateArgument(
                f'An argument named "{arg.name}" has been declared.')
        if arg.name in SUPPORTED_VIEW_ATTRS:
            raise ReservedNameError(
                f'Name "{arg.name}" is reserved for something else.')
        self.__dict__[arg.name] = arg
        self._arg_trie[
            arg.
            name] = arg  # NOTE(j_luo) This is class attribute, therefore not part of __dict__.
        if dtype == bool:
            self._arg_trie[f'no_{arg.name}'] = arg
        return arg

    def set_argument(self, name, value, *, _force=False):
        if not _force:
            raise MustForceSetArgument(
                f'You must explicitliy set _force to True in order to set an argument.'
            )
        arg = self._get_argument_by_string(name)
        arg.value = value

    def add_registry(self, registry, scope):
        try:
            arg = self.add_argument(registry.name, scope=scope, dtype=str)
        except DuplicateArgument:
            raise DuplicateRegistry(
                f'A registry named "{registry.name}" already exists.')
        self._registries[arg.name] = registry

    @property
    def configs(self) -> Dict[str, str]:
        ret = dict()
        for name, registry in self._registries.items():
            arg = self._get_argument_by_string(name)
            ret[name] = arg.value
        return ret

    def get_view(self):
        return _RepositoryView(self._shared_state)

    def _get_argument_by_string(self, name, source='CLI'):
        args = self._arg_trie.values(prefix=name)
        if len(args) > 1:
            found_names = [f'"{arg.name}"' for arg in args]
            raise MultipleMatches(
                f'Found more than one match for name "{name}": {", ".join(found_names)}.'
            )
        elif len(args) == 0:
            raise MatchNotFound(
                f'Found no argument named "{name}" from "{source}".')
        arg = args[0]
        return arg

    def parse_args(self, known_only=False):
        arg_groups = list()
        group = list()
        for seg in sys.argv[1:]:
            if seg.startswith('-'):
                if group:
                    arg_groups.append(group)
                group = seg.split('=')
            else:
                group.append(seg)
        if group:
            arg_groups.append(group)
        # Parse the CLI string first.
        parsed = list()
        for group in arg_groups:
            name, *values = group
            name = name.strip('-')
            # NOTE(j_luo) Help mode. Note that if known_only is True, then help is ignored.
            if name == 'h' or name == 'help':
                if not known_only:
                    _print_all_args(log_also=False, and_exit=True)
                continue  # NOTE(j_luo) Some other args might start with "h"!
            try:
                arg = self._get_argument_by_string(name, source='CLI')
                if arg.dtype == bool:
                    new_value = [not name.startswith('no_')] + values
                else:
                    new_value = values
                parsed.append((arg, new_value))
            except MatchNotFound as e:
                if not known_only:
                    raise e
        # Deal with config files and use their values to set the new default values.
        cfg_names = set()
        for arg, new_value in parsed:
            if arg.name in self._registries:
                arg.value = new_value
                reg = self._registries[arg.name]
                cfg_cls = reg[arg.value]
                cfg = vars(cfg_cls())
                for cfg_name, cfg_value in cfg.items():
                    cfg_arg = self._get_argument_by_string(
                        cfg_name, source=cfg_cls.__name__)
                    cfg_arg.value = cfg_value
                    if cfg_name in cfg_names:
                        raise OverlappingRegistries(
                            f'Argument named "{cfg_name}" has been found in multiple registries.'
                        )
                    cfg_names.add(cfg_name)
        # Set the remaning CLI arguments.
        for arg, new_value in parsed:
            if arg.name not in self._registries:
                arg.value = new_value
        return g