小编duh*_*ime的帖子

查找由两个字符串共享的所有n字长子字符串的最大长度

我正在努力生成一个Python脚本,它可以找到由两个字符串共享的所有n字长子字符串的(尽可能长)长度,忽略尾随标点符号.鉴于两个字符串:

"这是一个示例字符串"

"这也是一个示例字符串"

我希望脚本能够识别这些字符串是否有一个共同的2个单词的序列("this is"),然后是一个共同的3个单词的序列("一个样本字符串").这是我目前的做法:

a = "this is a sample string"
b = "this is also a sample string"

aWords = a.split()
bWords = b.split()

#create counters to keep track of position in string
currentA = 0
currentB = 0

#create counter to keep track of longest sequence of matching words
matchStreak = 0

#create a list that contains all of the matchstreaks found
matchStreakList = []

#create binary switch to control the use of while loop
continueWhileLoop = …
Run Code Online (Sandbox Code Playgroud)

python string algorithm substring pattern-matching

6
推荐指数
1
解决办法
2166
查看次数

使用NLTK简化法国POS标签集

如何简化斯坦福法国POS标签器返回的部分语音标签?将英文句子读入NLTK相当容易,找到每个单词的词性,然后使用map_tag()来简化标签集:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
from nltk.tag.stanford import POSTagger
from nltk.tokenize import word_tokenize
from nltk.tag import map_tag

#set java_home path from within script. Run os.getenv("JAVA_HOME") to test java_home
os.environ["JAVA_HOME"] = "C:\\Program Files\\Java\\jdk1.7.0_25\\bin"

english = u"the whole earth swarms with living beings, every plant, every grain and leaf, supports the life of thousands."

path_to_english_model = "C:\\Text\\Professional\\Digital Humanities\\Packages and Tools\\Stanford Packages\\stanford-postagger-full-2014-08-27\\stanford-postagger-full-2014-08-27\\models\\english-bidirectional-distsim.tagger"
path_to_jar = "C:\\Text\\Professional\\Digital Humanities\\Packages and Tools\\Stanford Packages\\stanford-postagger-full-2014-08-27\\stanford-postagger-full-2014-08-27\\stanford-postagger.jar"

#define english and french taggers
english_tagger = POSTagger(path_to_english_model, path_to_jar, encoding="utf-8") …
Run Code Online (Sandbox Code Playgroud)

python syntax nlp nltk stanford-nlp

6
推荐指数
1
解决办法
2832
查看次数

Python:求和三层字典的值

给定一个包含三层键的字典,对值进行求和的最快方法是什么?这是我目前的做法:

from collections import defaultdict

dicts = [ {'a':{'b':{'c':1}}}, {'a':{'b':{'c':4, 'e':3}}} ]

def sum_three_deep_dict_values(dicts):
    '''Read in two dicts and return a dictionary that contains their outer-joined keys and value sums'''
    combined = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))
    for d in dicts:
        for w1, val_dict in d.iteritems():          
            for w2 in val_dict.iterkeys():              
                for w3 in val_dict[w2].iterkeys():
                    combined[w1][w2][w3] += d[w1][w2][w3]
    return combined

print sum_three_deep_dict_values(dicts)
Run Code Online (Sandbox Code Playgroud)

这里的预期输出是{'a': {'b': {'c': 5, 'e': 3}}}:目标是将两个字典具有相同键的值(例如d[a][b][c]此处)相加,并在输出字典中包含来自任一字典的剩余键值对.

有很多关于SO的问题似乎回答了这个问题:"如何总结嵌套词典的价值"?然而,昨晚读完它们,我发现的每一个都涉及一些奇怪的特殊情况或参数,例如"组合/忽略第n层键",或"在特殊地方应用if条件".因此,我想提出一个简单的问题:在Python中对双嵌套字典的值进行求和的最佳方法是什么?

python dictionary

6
推荐指数
1
解决办法
550
查看次数

删除图像链接的下划线

我的页面上有一个大图像,其中也有一个link元素,我想从图像链接中删除下划线。我正在使用Chrome,它会在图片链接下显示突出显示的内容,并声称css是由引入的user agent stylesheet,我知道这是Chrome的默认css。将user agent stylesheet带来以下样式到我的网页:

a:-webkit-any-link {
    color: -webkit-link;
    text-decoration: underline;
    cursor: auto;
}
Run Code Online (Sandbox Code Playgroud)

如何从该图像链接中删除下划线?我尝试将链接和图像的ID设置为img-link随后使用以下CSS定位该ID,但没有任何运气:

<a id="img-link" href="/images/post_images/mapping_early_english_books/provincial_printing.png" data-lightbox="provincial_printing" data-title="My caption">
  <img id="img-link" src="/images/post_images/mapping_early_english_books/provincial_printing.png" alt="Provincial Printing" style="width:100%" /></a></p>

#img-link {
    text-decoration: none;
}
Run Code Online (Sandbox Code Playgroud)

他人在消除此下划线方面可以提供的任何帮助将不胜感激!

html css html5 image jekyll

6
推荐指数
1
解决办法
5394
查看次数

针对特定点的 Three.js 光线投射

我正在使用 Three.js 进行网络可视化,但无法确定为什么我的光线投射实现没有识别正确的点(完整示例来源)。

更具体地说,我正在尝试将光线投射与点云一起使用,并尝试在用户悬停在该点上时将点的颜色更改为白色。现在,悬停点确实会改变点的颜色,但该事件似乎是在光标附近的点上触发的,而不是在光标正下方的点上。

这是我用来初始化光线投射器的代码:

// configure the raycaster
raycaster = new THREE.Raycaster();
raycaster.params.Points.threshold = 20;
Run Code Online (Sandbox Code Playgroud)

这是渲染函数:

function render() {
  renderer.render(scene, camera);
  controls.update();
  raycast();
}

// Color hovered points
function raycast() {
  raycaster.setFromCamera(mouse, camera); 
  var intersects = raycaster.intersectObject(particles);
  if (intersects.length) {

    var newColor = new THREE.Color();
    newColor.setRGB( 1, 1, 1 );

    var index = intersects[0].index;
    particles.geometry.colors[index] = newColor;
    particles.geometry.colorsNeedUpdate=true;
  }
}
Run Code Online (Sandbox Code Playgroud)

最后,mousemove在主体上触发的事件回调:

/**
* Mouse coordinates go from 0 to container width {0:1} 
* …
Run Code Online (Sandbox Code Playgroud)

javascript three.js raycasting

6
推荐指数
1
解决办法
3051
查看次数

Three.js索引BufferGeometry与InstancedBufferGeometry

我正在尝试更多地了解THREE.js中的高性能几何,并且已经了解索引的BufferGeometry和InstancedBufferGeometry是两种性能最高的几何类型.

到目前为止,我的理解是在索引的BufferGeometry中,在几何中重复使用的顶点仅被添加到几何体中一次,并且给定的重用顶点的每个实例都由它们在顶点数组中的索引位置引用.

我对InstancedBufferGeometry的理解是,这个几何允许人们创建一个对象的"蓝图",将该对象的顶点的一个副本发送到着色器,然后使用自定义属性来修改蓝图的位置,旋转,比例等的每个副本.[ 来源 ]

我想更好地理解:是否存在索引的BufferGeometry比InstancedBufferGeometry更高效的情况.

另外,在InstancedBufferGeometry中,是否有必须考虑的WebGL最大参数(例如每个网格的最大顶点)以避免网格太大?如何计算InstancedBufferGeometry中的顶点?

如果有人可以帮助澄清应该使用索引的BufferGeometry和InstancedBufferGeometry的情况,以及InstancedBufferGeometry的性能上限,我将非常感激.

three.js

6
推荐指数
1
解决办法
1200
查看次数

Matplotlib:设置 animation.rc 参数

在 Matplotlib 中绘制大型动画时,我收到了警告:

动画大小已经达到了 20997590 字节,超过了 20971520.0 的限制。如果您确定要嵌入更大的动画,请将 animation.embed_limit rc 参数设置为更大的值(以 MB 为单位)。此帧和更多帧将被丢弃。

有谁知道怎么设置这个参数?

matplotlib

6
推荐指数
1
解决办法
3942
查看次数

类型错误:(0,_schemaUtils.default)不是函数

我创建了一个简单的 React 应用程序,create-react-app它在本地主机上运行良好。我现在正在尝试 Dockerify 该应用程序。这是我的 package.json:

{
  "name": "yeet",
  "version": "0.1.0",
  "engines": {
    "node": "12.x"
  },
  "scripts": {
    "client": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "start": "node server.js",
    "production": "npm run build && npm run start"
  },
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "axios": "^0.21.0",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-session": "^1.17.1",
    "mongoose": "^5.10.13",
    "mysql": "^2.18.1",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.0",
    "web-vitals": "^0.2.4"
  },
  "devDependencies": {
    "babel-core": …
Run Code Online (Sandbox Code Playgroud)

node.js docker reactjs babeljs create-react-app

6
推荐指数
1
解决办法
1万
查看次数

Python Tkinter:AttributeError:Checkbutton 实例没有属性“get”

我是 Tkinter 的新手。我正在尝试创建一个具有两个旋转框和两个复选框的 GUI,当用户单击特定按钮(“开始”)时,将打印其值。到目前为止,这是我的代码:

from Tkinter import *
from tkFileDialog import askopenfilename
from PIL import Image

#create TK frame
root = Tk()
#identify the dimensions of the TK frame
root.geometry("360x150")
#title the TK frame
root.title("Literature Online API")

#create a function that will return the filepath for a file provided by the user
def selectfile():
    user_defined_filepath['filename'] = askopenfilename(filetypes=[("Text","*.txt")]) # user_defined_filepath['filename'] may now be accessed in the global scope.

#create a function that will allow the "start" button to begin further processes …
Run Code Online (Sandbox Code Playgroud)

python checkbox user-interface tkinter

5
推荐指数
1
解决办法
1万
查看次数

为什么Atob和Btoa不可逆

我正在尝试找到一种简单的方法来记录和暂时混淆我在Markdown中编写的“测验”问题的答案。(在演示过程中,我会告诉学生测验答案,因此,我不需要任何类型的安全加密。)

我以为我可以用atob('message I want to obfuscate')然后告诉学生,他们可以btoa()在开发人员工具面板中使用以逆转该过程。但是,以下内容不会返回“ one”:

btoa( atob('one') )
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么这个不回来'one'?JavaScript中是否还有其他方法可以使您轻松地对消息进行加密和解密?(我与绝对的初学者一起工作,这些初学者可能会对功能感到困惑,并且对将库添加到页面会感到非常困惑)。

javascript encoding

5
推荐指数
1
解决办法
270
查看次数