本文整理汇总了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")
示例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)
示例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))
示例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")
示例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')
示例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
示例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()
示例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()
示例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()
示例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
示例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()
示例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
示例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)
示例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
示例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)']))