我正在寻找一个Python的接口来加载ARPA文件(回退语言模型),并利用它们来评估一些文字,如得到它的日志概率,困惑等.
我不需要在Python中生成ARPA文件,只是用它来进行查询.
有人推荐套餐吗?我已经看过kenlm和swig-srilm,但是第一个在Windows中很难设置,第二个似乎不再维护了.
我正在尝试使用mrjob在EMR上运行hadoop,并且无法弄清楚如何设置日志记录(用户生成的日志在map/reduce步骤中),因此我可以在集群终止后访问它们.
我已经使用了试图建立日志logging
模块,print
并且sys.stderr.write()
到目前为止,但没有运气.对我有用的唯一选择是将日志写入文件然后SSH机器并读取它,但它很麻烦.我希望我的日志转到stderr/stdout/syslog并自动收集到S3,因此我可以在群集终止后查看它们.
这是带有日志记录的word_freq示例:
"""The classic MapReduce job: count the frequency of words.
"""
from mrjob.job import MRJob
import re
import logging
import logging.handlers
import sys
WORD_RE = re.compile(r"[\w']+")
class MRWordFreqCount(MRJob):
def mapper_init(self):
self.logger = logging.getLogger()
self.logger.setLevel(logging.INFO)
self.logger.addHandler(logging.FileHandler("/tmp/mr.log"))
self.logger.addHandler(logging.StreamHandler())
self.logger.addHandler(logging.StreamHandler(sys.stdout))
self.logger.addHandler(logging.handlers.SysLogHandler())
def mapper(self, _, line):
self.logger.info("Test logging: %s", line)
sys.stderr.write("Test stderr: %s\n" % line)
print "Test print: %s" % line
for word in WORD_RE.findall(line):
yield (word.lower(), 1)
def combiner(self, word, counts):
yield (word, sum(counts))
def …
Run Code Online (Sandbox Code Playgroud) 更新:尝试检查/填充另一个函数中的值
我正在尝试在我的项目中使用 mypy,但是我使用的许多实例属性仅在 之后初始化__init__
,而不是在其中初始化。但是,我确实希望保持在 处声明所有实例属性的良好做法__init__
,因此我需要一些复杂的解决方案来完成这项工作。
我希望它如何表现的一个例子(目前 mypy 正在抱怨):
from typing import Optional
class Foo:
def __init__(self, x: int):
self.x = x
self.y: int = None # will initialize later, but I know it will be an int
def fill_values(self):
self.y = x**2
def do(self) -> int:
return self.x + self.y
Run Code Online (Sandbox Code Playgroud)
目前 mypy 抱怨 的分配self.y
,并希望它是Optional
or None
。
如果我同意并将该行更改为self.y: Optional[int] = None
,则 mypy 会抱怨 的返回值do
,因为self.y
可能是None
. …