我有一套腌制的文本文档,我想用nltk来阻止它PorterStemmer
.由于我的项目特定的原因,我想在django应用程序视图中进行干预.
但是,当阻止django视图中的文档时,我收到字符串的IndexError: string index out of range
异常.因此,运行以下命令:PorterStemmer().stem()
'oed'
# xkcd_project/search/views.py
from nltk.stem.porter import PorterStemmer
def get_results(request):
s = PorterStemmer()
s.stem('oed')
return render(request, 'list.html')
Run Code Online (Sandbox Code Playgroud)
提出上述错误:
Traceback (most recent call last):
File "//anaconda/envs/xkcd/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "//anaconda/envs/xkcd/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "//anaconda/envs/xkcd/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/jkarimi91/Projects/xkcd_search/xkcd_project/search/views.py", line 15, in get_results
s.stem('oed')
File "//anaconda/envs/xkcd/lib/python2.7/site-packages/nltk/stem/porter.py", line 665, in stem
stem = self._step1b(stem) …
Run Code Online (Sandbox Code Playgroud) 在views.py
,我将时间序列数据存储在字典中,如下所示:
time_series = {"timestamp1": occurrences, "timestamp2": occurrences}
Run Code Online (Sandbox Code Playgroud)
其中每个timestamp
都在unix时间并且occurrences
是一个整数.
有没有办法在时间render
函数的上下文中将时间序列数据作为json对象传递?
为什么这样做:我在前端使用Cal-heatmap,要求数据采用json格式.Ajax请求现在工作得很好,但我理想的是希望尽可能使用这种render
方法.
我目前的bash ps1如下:
bldred='\e[1;31m' # Red
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
txtrst='\e[0m' # Text Reset - Useful for avoiding color bleed
export PS1="\n\[$bldred\]\u\[$txtrst\]@\[$bldwht\]\h\[$txtrst\]:\[$bldcyn\]\w\[$txtrst\]$ "
Run Code Online (Sandbox Code Playgroud)
但是,运行:
source activate <env-name-here>
Run Code Online (Sandbox Code Playgroud)
默认情况下,告诉conda
您env-name
将my 放在前面PS1
:
(<env-name-here>)
user@short-domain:fullpath$
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉我在换行符后conda
插入env-name
我PS1
的代码呢?
我有一个单击命令download
,它会在下载一系列文件之前提示用户输入用户名和密码:
$ python download.py
Username: jkarimi91
Password: 1234
Download complete!
Run Code Online (Sandbox Code Playgroud)
要测试此命令,我需要能够分别将 ausername
和 a传递password
给stdin
. 该CliRunner.invoke()
方法有一个input
参数,但它不接受列表。是否可以将多个输入传递给CliRunner.invoke()
?
我有一个 543 MB 的 txt 文件,其中包含一行空格分隔的 utf-8 标记:
aaa algeria americansamoa appliedethics accessiblecomputing ada anarchism ...
Run Code Online (Sandbox Code Playgroud)
但是,当我将此文本数据加载到 python 列表中时,它使用了 ~8 GB 的内存(~900 MB 用于列表,~8 GB 用于令牌):
with open('tokens.txt', 'r') as f:
tokens = f.read().decode('utf-8').split()
import sys
print sys.getsizeof(tokens)
# 917450944 bytes for the list
print sum(sys.getsizeof(t) for t in tokens)
# 7067732908 bytes for the actual tokens
Run Code Online (Sandbox Code Playgroud)
我预计内存使用量大约为文件大小 + 列表开销 = 1.5 GB。为什么令牌在加载到列表中时会消耗更多内存?
我读过列表不能是字典键,因为不能对可变对象进行散列。但是,自定义对象似乎也是可变的:
# custom object
class Vertex(object):
def __init__(self, key):
self.key = key
v = Vertex(1)
v.color = 'grey' # this line suggests the custom object is mutable
Run Code Online (Sandbox Code Playgroud)
但是,与列表不同,它们可以用作字典键;为什么是这样?在这两种情况下,我们不能简单地散列某种 id(例如对象在内存中的地址)吗?
python ×4
nlp ×2
python-2.7 ×2
bash ×1
cal-heatmap ×1
conda ×1
dictionary ×1
django ×1
django-1.7 ×1
javascript ×1
json ×1
list ×1
memory ×1
nltk ×1
ps1 ×1
python-click ×1
stemming ×1
tensorboard ×1
tensorflow ×1
utf-8 ×1
word2vec ×1