我有以下代码.我知道我可以使用apply_freq_filter
函数来过滤掉小于频率计数的搭配.但是,在我决定为过滤设置的频率之前,我不知道如何在文档中获取所有n-gram元组的频率(在我的情况下是bi-gram).如您所见,我正在使用nltk collocations类.
import nltk
from nltk.collocations import *
line = ""
open_file = open('a_text_file','r')
for val in open_file:
line += val
tokens = line.split()
bigram_measures = nltk.collocations.BigramAssocMeasures()
finder = BigramCollocationFinder.from_words(tokens)
finder.apply_freq_filter(3)
print finder.nbest(bigram_measures.pmi, 100)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用pandas将分类值转换为二进制值.我们的想法是将每个唯一的分类值视为一个特征(即一列),并根据特定对象(即行)是否分配给该类别而放置1或0.以下是代码:
data = pd.read_csv('somedata.csv')
converted_val = data.T.to_dict().values()
vectorizer = DV( sparse = False )
vec_x = vectorizer.fit_transform( converted_val )
numpy.savetxt('out.csv',vec_x,fmt='%10.0f',delimiter=',')
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何使用列名保存这些转换后的数据?在上面的代码中,我能够使用numpy.savetxt
函数保存数据,但这只是保存数组并且列名丢失.或者,是否有一种非常有效的方法来执行上述操作?
我试图不断监视一个基本上是Python程序的进程.如果程序停止,那么我必须再次启动程序.我正在使用另一个Python程序来执行此操作.
例如,假设我必须不断运行一个名为的进程run_constantly.py
.我最初手动运行该程序,将其进程ID写入文件"PID"(位置输出/ PROCESSID/PID).
现在我运行另一个程序,它具有以下代码来监视run_constantly.py
Linux环境中的程序:
def Monitor_Periodic_Process():
TIMER_RUNIN = 1800
foo = imp.load_source("Run_Module","run_constantly.py")
PROGRAM_TO_MONITOR = ['run_constantly.py','out/PROCESSID/PID']
while(1):
# call the function checkPID to see if the program is running or not
res = checkPID(PROGRAM_TO_MONITOR)
# if res is 0 then program is not running so schedule it
if (res == 0):
date_time = datetime.now()
scheduler.add_cron_job(foo.Run_Module, year=date_time.year, day=date_time.day, month=date_time.month, hour=date_time.hour, minute=date_time.minute+2)
scheduler.start()
scheduler.get_jobs()
time.sleep(TIMER_NOT_RUNIN)
continue
else:
#the process is running sleep and then monitor again
time.sleep(TIMER_RUNIN) …
Run Code Online (Sandbox Code Playgroud) 我有关于tweepy python模块的以下问题
1.我正在尝试检索特定位置的所有推文.我能够通过使用tweepy python模块(流API)来做到这一点,但我只得到那些地理位置已启用的推文,这意味着我会丢失其他未启用地理位置的推特的推文.在给定位置的情况下,是否有更好的方法来检索所有推文?
2.我使用Stream.Sample方法检索所有推文,有人可以告诉我样本方法中使用的参数吗?我看到count和async作为参数.现在我们应该在这里指定什么?
3. tweepy.Stream中的firehose方法做什么?
任何帮助深表感谢
是否有任何 python 模块(可能在 nltk python 中)来删除互联网俚语/聊天俚语,如“lol”、“brb”等。如果没有,有人可以向我提供一个包含如此庞大俚语列表的 CSV 文件吗?
网站http://www.netlingo.com/acronyms.php提供了首字母缩略词列表,但我找不到任何 CSV 文件以在我的程序中使用它们。
我试图在23万字的列表上计算重复的单词.我使用python字典这样做.代码如下:
for words in word_list:
if words in word_dict.keys():
word_dict[words] += 1
else:
word_dict[words] = 1
Run Code Online (Sandbox Code Playgroud)
上面的代码用了3分钟!我运行相同的代码超过150万字,它运行超过25分钟,我失去了耐心并终止.后来我发现,我可以使用从下面的代码在这里(如下所示).结果是如此令人惊讶,它在几秒钟内完成!所以我的问题是什么是更快的方式来做这个操作?我想字典创建过程必须花费O(N)时间.Counter方法如何能够在几秒钟内完成此过程,并创建一个精确的单词词典作为键和频率的值?
from collections import Counter
word_dict = Counter(word_list)
Run Code Online (Sandbox Code Playgroud) 我正在使用java正则表达式库来查找模式"String_OneOrMoreDigits".例如,"linenumber_1"或"linenumber_31"或"linenumber_456".我正在尝试以下模式,假设我将获得"linenumber_2"或"linenumber_44"类型的字符串.但是,我只是得到"linenumber_2"类型的字符串,它最后不匹配多个数字.如何匹配这些字符串?
Pattern pattern = Pattern.compile("(linenumber_[0-9])|(linenumber_[0-9][0-9])");
python ×6
acronym ×1
api ×1
cron-task ×1
daemon ×1
dictionary ×1
hashtable ×1
java ×1
n-gram ×1
nlp ×1
nltk ×1
numpy ×1
pandas ×1
performance ×1
regex ×1
scheduling ×1
string ×1
tweepy ×1
twitter ×1
word-count ×1