当前位置: 首页>>代码示例>>Python>>正文


Python dictionary.Dictionary类代码示例

本文整理汇总了Python中dictionary.Dictionary的典型用法代码示例。如果您正苦于以下问题:Python Dictionary类的具体用法?Python Dictionary怎么用?Python Dictionary使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Dictionary类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

def main():
  graph = sys.argv[1]
  wordsList = sys.argv[2]

  with open(graph, 'r') as f:
    n = int(f.readline().strip())
    data = []
    for i in range(0,n):
      l = list(f.readline().strip())
      data.append(l)

  g = HoneyGraph()
  g.setup(n, data)

  words = []
  with open(wordsList, 'r') as f:
    words = [line.strip() for line in f.readlines()]

  d = Dictionary()
  d.setup(words)
  bound = max(words, key=len)

  out = set()
  for key in d.tree.keys():
    for n in g.comb[key]:
      recurseSearch(n, d.tree, '', [], out)
  out = sorted(out)

  with open('output.txt', 'w') as f:
    for i in out:
      f.write(i + "\n")
开发者ID:nikhilpi,项目名称:honeycombWordsearch,代码行数:31,代码来源:searcher.py

示例2: Anagram

class Anagram(object):
    
    def __init__(self):
        self.possible_words = set()
        self.output = set()
        self.dict = Dictionary()
        fich = open('Unabr.dict', 'r')
        self.dict.get_dict(fich)
        fich.close()
        
    def set_input(self, string):
        self.string = string
        
    def get_output(self):
        self.process('', list(self.string))
        for word in self.possible_words:
            if self.dict.is_in_dict(word):
                self.output.add(word)
        return self.output
    
    def process(self, string, l):
        if len(l) == 0:
            self.possible_words.add(string)
            return
        for index in range(len(l)):
            new_list = l[:]
            elem = new_list.pop(index)
            self.process(string + elem, new_list)            
开发者ID:antonioRubio,项目名称:anagram,代码行数:28,代码来源:anagram.py

示例3: checkFile

def checkFile(file_name, dictionary_file="words.dat"):
    # Set up dictionary based on words.dat
    d = Dictionary(file_name=dictionary_file)

    file_in = open(file_name, 'r')
    file_out = open("{}.out".format(file_name), 'w')

    current_word = ""

    while True:
        # Read one character at a time from the input file
        next_char = file_in.read(1)
        # Exit the loop when there's nothing else to read
        if not next_char:
            break

        if next_char in d.ALLOWED_LETTERS:
            current_word += next_char
        else:
            # Verify the current_word with the dictionary
            resp, current_word = d.verify(current_word)
            if not resp:  # Word was not found in dictionary
                resp, new_word = getUserResponse(current_word)
                d.update(resp, current_word, new_word)
                current_word = new_word
            file_out.write(current_word)
            current_word = ""
            file_out.write(next_char)

    file_in.close()
    file_out.close()
    print("Spellchecked file written to {}.out.".format(file_name))
开发者ID:tomhogans,项目名称:cs113,代码行数:32,代码来源:spellCheck.py

示例4: main

def main():
    files = sys.argv[1:]
    d = Dictionary()
    for f in files:
        for word in parseWords(f):
            d.add_word(word)
    d.save("words.dat")
开发者ID:tomhogans,项目名称:cs113,代码行数:7,代码来源:buildDict.py

示例5: setup_module

def setup_module(module):
    global DICTIONARIES
    global cluster
    global node

    dict_configs_path = os.path.join(SCRIPT_DIR, 'configs/dictionaries')
    for f in os.listdir(dict_configs_path):
        os.remove(os.path.join(dict_configs_path, f))

    for layout in LAYOUTS:
        for source in SOURCES:
            if source.compatible_with_layout(layout):
                structure = DictionaryStructure(layout, FIELDS[layout.layout_type])
                dict_name = source.name + "_" + layout.name
                dict_path = os.path.join(dict_configs_path, dict_name + '.xml')
                dictionary = Dictionary(dict_name, structure, source, dict_path, "table_" + dict_name)
                dictionary.generate_config()
                DICTIONARIES.append(dictionary)
            else:
                print "Source", source.name, "incompatible with layout", layout.name

    main_configs = []
    for fname in os.listdir(dict_configs_path):
        main_configs.append(os.path.join(dict_configs_path, fname))
    cluster = ClickHouseCluster(__file__, base_configs_dir=os.path.join(SCRIPT_DIR, 'configs'))
    node = cluster.add_instance('node', main_configs=main_configs, with_mysql=True, with_mongo=True)
    cluster.add_instance('clickhouse1')
开发者ID:greck2908,项目名称:ClickHouse,代码行数:27,代码来源:test.py

示例6: TextCorpus

class TextCorpus(interfaces.CorpusABC):
    """
    Helper class to simplify the pipeline of getting bag-of-words vectors (= a
    gensim corpus) from plain text.

    This is an abstract base class: override the `get_texts()` method to match
    your particular input.

    Given a filename (or a file-like object) in constructor, the corpus object
    will be automatically initialized with a dictionary in `self.dictionary` and
    will support the `iter` corpus method. You must only provide a correct `get_texts`
    implementation.

    """
    def __init__(self, input=None):
        super(TextCorpus, self).__init__()
        self.input = input
        self.dictionary = Dictionary()
        if input is not None:
            self.dictionary.add_documents(self.get_texts())
        else:
            logger.warning("No input document stream provided; assuming "
                           "dictionary will be initialized some other way.")


    def __iter__(self):
        """
        The function that defines a corpus.

        Iterating over the corpus must yield sparse vectors, one for each document.
        """
        for text in self.get_texts():
            yield self.dictionary.doc2bow(text, allow_update=False)


    def getstream(self):
        return getstream(self.input)


    def get_texts(self):
        """
        Iterate over the collection, yielding one document at a time. A document
        is a sequence of words (strings) that can be fed into `Dictionary.doc2bow`.

        Override this function to match your input (parse input files, do any
        text preprocessing, lowercasing, tokenizing etc.). There will be no further
        preprocessing of the words coming out of this function.
        """
        # Instead of raising NotImplementedError, let's provide a sample implementation:
        # assume documents are lines in a single file (one document per line).
        # Yield each document as a list of lowercase tokens, via `utils.tokenize`.
        length = 0
        for lineno, line in enumerate(getstream(self.input)):
            length += 1
            yield utils.tokenize(line, lowercase=True)
        self.length = length


    def __len__(self):
        return self.length # will throw if corpus not initialized
开发者ID:Alienfeel,项目名称:gensim,代码行数:60,代码来源:textcorpus.py

示例7: __init__

class Solver:

    problem = None
    dictionary = None

    def __init__(self, problem):
        self.problem = problem
        self.dictionary = Dictionary(problem)

    def solve(self):
        # pivot until solution found
        while self.dictionary.canPivot():
            self.dictionary.pivot()

        # unbounded problem
        if self.dictionary.unbounded:
            raise SolverError("problem is unbounded")

        return self.__getSolution()

    # returns solution only in primal form
    def __getSolution(self):
        dic = self.dictionary

        # convert dual solutions
        if self.problem.dual:
            dual = dic.toProblem()
            primal = dual.getDual()
            dic = Dictionary(primal)
            
        return dic.getSolution()
开发者ID:StevenLOL,项目名称:ilp-solver,代码行数:31,代码来源:solver.py

示例8: Initialize

def Initialize(credentials=None, opt_url=None):
  """Initialize the EE library.

  If this hasn't been called by the time any object constructor is used,
  it will be called then.  If this is called a second time with a different
  URL, this doesn't do an un-initialization of e.g.: the previously loaded
  Algorithms, but will overwrite them and let point at alternate servers.

  Args:
    credentials: OAuth2 credentials.
    opt_url: The base url for the EarthEngine REST API to connect to.
  """
  data.initialize(credentials, (opt_url + '/api' if opt_url else None), opt_url)
  # Initialize the dynamically loaded functions on the objects that want them.
  ApiFunction.initialize()
  Element.initialize()
  Image.initialize()
  Feature.initialize()
  Collection.initialize()
  ImageCollection.initialize()
  FeatureCollection.initialize()
  Filter.initialize()
  Geometry.initialize()
  List.initialize()
  Number.initialize()
  String.initialize()
  Date.initialize()
  Dictionary.initialize()
  _InitializeGeneratedClasses()
  _InitializeUnboundMethods()
开发者ID:Arable,项目名称:ee-python,代码行数:30,代码来源:__init__.py

示例9: Initialize

def Initialize(credentials="persistent", opt_url=None):
    """Initialize the EE library.

  If this hasn't been called by the time any object constructor is used,
  it will be called then.  If this is called a second time with a different
  URL, this doesn't do an un-initialization of e.g.: the previously loaded
  Algorithms, but will overwrite them and let point at alternate servers.

  Args:
    credentials: OAuth2 credentials.  'persistent' (default) means use
        credentials already stored in the filesystem, or raise an explanatory
        exception guiding the user to create those credentials.
    opt_url: The base url for the EarthEngine REST API to connect to.
  """
    if credentials == "persistent":
        credentials = _GetPersistentCredentials()
    data.initialize(credentials, (opt_url + "/api" if opt_url else None), opt_url)
    # Initialize the dynamically loaded functions on the objects that want them.
    ApiFunction.initialize()
    Element.initialize()
    Image.initialize()
    Feature.initialize()
    Collection.initialize()
    ImageCollection.initialize()
    FeatureCollection.initialize()
    Filter.initialize()
    Geometry.initialize()
    List.initialize()
    Number.initialize()
    String.initialize()
    Date.initialize()
    Dictionary.initialize()
    Terrain.initialize()
    _InitializeGeneratedClasses()
    _InitializeUnboundMethods()
开发者ID:bevingtona,项目名称:earthengine-api,代码行数:35,代码来源:__init__.py

示例10: __init__

class Wordplay:
    def __init__(self):
        self.dico = Dictionary()

    def __del__(self):
        self.dico.close()


    def open(self, dictionary_path):
        self.dico.open(dictionary_path)


    def close(self):
        self.dico.close()


    def search_words(self, pattern):
        for word in self.dico.search_words(pattern):
            yield word


    def letters_for_three_words(self, word1_begin, word2_begin, word3_begin, word_end_len):
        word1_suffixes = set(self._search_suffixes(word1_begin, word_end_len))
        word2_suffixes = set(self._search_suffixes(word2_begin, word_end_len))
        word3_suffixes = set(self._search_suffixes(word3_begin, word_end_len))

        common_suffixes = word1_suffixes & word2_suffixes & word3_suffixes
        for common_suffix in sorted(common_suffixes):
            yield common_suffix


    def quatro(self, prefix1, suffix1, prefix2, suffix2, middleLength):
        word1_middle = set(self._search_middle(prefix1, suffix1, middleLength))
        word2_middle = set(self._search_middle(prefix2, suffix2, middleLength))

        common_middles = word1_middle & word2_middle
        for common_middle in sorted(common_middles):
            yield common_middle


    def _search_middle(self, prefix, suffix, middleLength):
        for word in self.dico.search_words(prefix + "_" * middleLength + suffix):
            middle = word[len(prefix):-len(suffix)]

            yield middle


    def _search_suffixes(self, word_begin, word_end_len):
        for word in self.dico.search_words(word_begin + "_" * word_end_len):
            suffix = word[-word_end_len:]

            yield suffix

    def search_anagrams(self, word):
        for anagram in self.dico.search_anagrams(word):
            yield anagram
开发者ID:killruana,项目名称:wordplay,代码行数:56,代码来源:wordplay.py

示例11: __getSolution

    def __getSolution(self):
        dic = self.dictionary

        # convert dual solutions
        if self.problem.dual:
            dual = dic.toProblem()
            primal = dual.getDual()
            dic = Dictionary(primal)
            
        return dic.getSolution()
开发者ID:StevenLOL,项目名称:ilp-solver,代码行数:10,代码来源:solver.py

示例12: test2

def test2():
    dictionary = Dictionary()
    dictionary.set_words(["KISSED"])
    board = Board()
    rack = "KISSEDQ"
    solutions = board.generate_solutions(rack, dictionary)
    solution = board.find_best_solution(solutions, dictionary)
    if solution:
        print "Winner: %s" % solution
        board.add_solution(solution)
    print board
    assert solution and solution.score == 32
开发者ID:flibustenet,项目名称:scrabble,代码行数:12,代码来源:test2.py

示例13: test_cross_with_blank

    def test_cross_with_blank(self):
        dic = Dictionary()
        dic.set_words(["SA","JETS"])

        board = Board()
        board.add_word('JET', 5, 4, VERTICAL)
        sol= Solution(8, 4, HORIZONTAL, 'SA', []) 
        sol.determine_score(board, dic)
        self.assertEqual(sol.score, 13)
        sol= Solution(8, 4, HORIZONTAL, 'SA', [0]) 
        sol.determine_score(board, dic)
        self.assertEqual(sol.score, 11)
        board.add_solution(sol)
开发者ID:flibustenet,项目名称:scrabble,代码行数:13,代码来源:test_score.py

示例14: file_to_dict

def file_to_dict(path):
    word_file = open(path, 'r')
    dictionary = Dictionary()

    counter = 0
    for line in word_file:
        if re.match('^[a-z]+$',line) is not None:
            dictionary.add_word(line.strip())
        if counter % 25000 == 0:
            print "Loading Dictionary..."
        counter += 1
    dictionary.update_word_count()
    word_file.close()
    return dictionary
开发者ID:darylsew,项目名称:contact-analysis,代码行数:14,代码来源:loader.py

示例15: test_blanks_with_same_letter

    def test_blanks_with_same_letter(self):

        dic = Dictionary()
        dic.set_words(["ABA"])
        board = Board()
        solutions = []
        board.generate_solutions_in_line('?BA', dic, 7, HORIZONTAL, solutions)
        words = set([(str(s)) for s in solutions])
        self.assertEqual(words, set(['ABa (7,7,H)', 
            'aBA (7,7,H)', 
            'ABa (7,5,H)', 
            'aBA (7,5,H)', 
            'ABa (7,6,H)', 
            'aBA (7,6,H)']))
开发者ID:flibustenet,项目名称:scrabble,代码行数:14,代码来源:test_score.py


注:本文中的dictionary.Dictionary类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。