我正在尝试使用Python NLTK使用Kneser-Ney平滑来平滑一组n-gram概率.不幸的是,整个文档相当稀疏.
我想要做的是:我将一个文本解析为三元组元组列表.从这个列表中我创建了一个FreqDist然后使用该FreqDist来计算KN平滑分布.
我很确定,结果是完全错误的.当我总结个人概率时,我得到的东西超越1.采取这个代码示例:
import nltk
ngrams = nltk.trigrams("What a piece of work is man! how noble in reason! how infinite in faculty! in \
form and moving how express and admirable! in action how like an angel! in apprehension how like a god! \
the beauty of the world, the paragon of animals!")
freq_dist = nltk.FreqDist(ngrams)
kneser_ney = nltk.KneserNeyProbDist(freq_dist)
prob_sum = 0
for i in kneser_ney.samples():
prob_sum += kneser_ney.prob(i)
print(prob_sum)
Run Code Online (Sandbox Code Playgroud)
输出为"41.51696428571428".根据语料库大小,此值会无限大.这使得任何prob()返回除了概率分布之外的任何东西.
看看NLTK代码我会说实现是有问题的.也许我只是不明白代码应该如何使用.在那种情况下,你能给我一个提示吗?在任何其他情况下:你知道任何有效的Python实现吗?我真的不想自己实现它.
我有以下情形:多个工作进程将有关其当前状态的事件发送到事件调度程序。然后,如果我们在主流程中,则此事件分发程序需要处理所有事件;如果在工作进程中,则该信号将需要主进程的事件分发程序发信号以处理这些事件。
这里的主要问题是事件处理也必须在主进程的主线程中,所以我不能只在线程中运行一会儿True循环并在那里等待工作进程的消息。
所以我有这个:
import asyncio
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import current_process, Process, Queue
from threading import current_thread
from time import sleep
def get_q(q):
print("Waiting for the queue ({} / {})\n".format(current_thread().name, current_process().name))
return q.get()
async def message_q(q):
while True:
f = loop.run_in_executor(None, get_q, q)
await f
if f.result() is None:
print("Done")
return;
print("Got the result ({} / {})".format(current_thread().name, current_process().name))
print("Result is: {}\n".format(f.result()))
async def something_else():
while True:
print("Something else\n")
await asyncio.sleep(2)
def other_process(q):
for i …
Run Code Online (Sandbox Code Playgroud) 我有一个Gradle项目,它取决于另一个Gradle项目.我的项目结构是这样的:
在project1/build.gradle
我想添加project2
为依赖:
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDirs = [ 'src' ]
}
}
}
include ':project2'
project(':project2').projectDir = new File("../project2")
dependencies {
compile project(':project2')
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我总是收到此错误消息:
错误:(21,0)无法在org.gradle.api.Project类型的根项目'project1'上找到参数[:project2]的方法include().
我正在使用Gradle 3.5,我在命令行(gradle build
)和IntelliJ中都收到此错误.我找到了一些关于同一问题的StackOverflow线程(这个和 这个),但它们没有帮助.
该摇篮多项目文档也没有提到,我可能会丢失,可能会导致错误的任何特殊要求.
当我离开include
呼叫时,我收到的消息是在根项目中找不到项目路径.
我也尝试将依赖项移动到子目录project1
,但没有成功.
我想知道我做错了什么,为什么显然没有其他人有同样的问题.我很感激提示. …