我是Python新手,我看到了一个使用NLTK的代码示例,如下所示(在空闲中):
>>> letters = nltk.FreqDist('ageqwst')
>>> words = nltk.corpus.words.words()
>>> [word for word in words if nltk.FreqDist(word) <= letters]
所生成的输出显示了可以用letters中的字符生成的单词。
我想知道nltk.FreqDist(word) <= letters部件是如何工作的。
我抬头看
我正在尝试使用Python获取一组文档的频率分布。由于某些原因,我的代码无法工作,并产生以下错误:
Traceback (most recent call last):
File "C:\Documents and Settings\aschein\Desktop\freqdist", line 32, in <module>
fd = FreqDist(corpus_text)
File "C:\Python26\lib\site-packages\nltk\probability.py", line 104, in __init_
我是python的新手,所以我正在阅读nltk这本书。我也在努力熟悉图形和绘图的操作。我绘制了一个条件频率分布,我想从删除顶部和左侧脊椎开始。这就是我所拥有的:
import nltk
import sys
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import show
from nltk.corpus import state_union
#cfdist1
cfd = nltk.ConditionalFreqDist(
(word, fileid[:4])
for f
我正在尝试创建一个函数来计算NLTK中Brown语料库中特定部分的比格数。我创建了这个函数
def category_bigram_count(bigrams,category):
category_text=nltk.corpus.brown.words(categories=category)
return sum(1 for bg in category_text if bg==bigrams)
但是很明显,当我尝试使用这个函数时
category_bigram_count("The","Office","government
我有以下代码。我知道我可以使用apply_freq_filter函数过滤出少于一个频率计数的搭配。但是,在决定设置过滤频率之前,我不知道如何获得文档中所有n元文法元组(在我的例子中是二元文法)的频率。如您所见,我使用的是nltk配置类。
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
下面的代码是做什么的?也就是说,只有最后两行。我不明白Text()做什么,what ()做什么?
import nltk
def words(content):
tokens = nltk.tokenize.word_tokenize(content) #Make the string into a list of words
tokens = [w for w in tokens if not w in stop_words] # remove the stop words
tokens = [wordnet_lemmatizer.lemmatize(w)
下面是我拥有的输入pandas数据帧。
我想找出单字和双字的频率。下面显示了我所期望的示例
如何使用nltk或scikit learn来做到这一点?
我写了下面的代码,它接受一个字符串作为输入。如何将其扩展到序列/数据帧?
from nltk.collocations import *
desc='john is a guy person you him guy person you him'
tokens = nltk.word_tokenize(desc)
bigram_measures = nltk.collocations.BigramAssocMeas
我正在使用Python和NLTK构建一个语言模型,如下所示:
from nltk.corpus import brown
from nltk.probability import LidstoneProbDist, WittenBellProbDist
estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)
lm = NgramModel(3, brown.words(categories='news'), estimator)
# Thanks to miku, I fixed this problem
pri
我正在使用FreqDist来获取一个单词在语料库中出现的次数,因为它非常快。问题是ftable不会以整数的形式返回答案,我需要对其进行一些基本操作。
words = brown.words()
content = [w for w in words if w.lower()]
ftable = nltk.FreqDist(content)
例如:
percent = ftable[sing]/ftable[s])*100
我尝试过像ftable.Nsing这样的东西,但是没有成功。
谢谢!
编辑:也在评论中。w.lower()用于将语料库中的单词小写,这样当我对它们运行for循环时,我只比较小
我正在努力解决SPOJ的下一个回文问题。在下面的Java代码中,我的时间限制超过了错误。
如果从左到右、从右到左读取时,正整数在十进制系统中的表示形式相同,则称为回文。对于给定的不超过1000000位的正整数K,请将大于K的最小回文的值写入输出。数字总是显示而不带前导零。
import java.math.BigInteger;
import java.util.Scanner;
public class Nextpalindrome {
public static void main(String[] args) {
// TODO Auto-generated m
我研究了lib噪音的来源,并找到了ValuNoise3D函数:
double noise::ValueNoise3D (int x, int y, int z, int seed)
{
return 1.0 - ((double)IntValueNoise3D (x, y, z, seed) / 1073741824.0);
}
int noise::IntValueNoise3D (int x, int y, int z, int seed)
{
// All constants are primes and must remain prime in order for t
我有一个小程序,它使用NLTK来获得一个相当大的数据集的频率分布。问题是,在几百万个字之后,我开始吃掉我系统上的所有RAM。下面是我认为的相关代码行:
freq_distribution = nltk.FreqDist(filtered_words) # get the frequency distribution of all the words
top_words = freq_distribution.keys()[:10] # get the top used words
bottom_words = freq_d
我试图识别和分类基于IOB/序列标签的实体类型。
我能够使用nltk.ne_chunk(),它已经被训练过使用它们的训练集来识别命名的实体。
我想知道是否有任何方法可以使用my on训练集和使用训练数据的标签来训练算法,如
(
send O
sms B-TASK
to
8714349616 B-MOB
how B-MSG
are I-MSG
you I-MSG
)
(
sms B-TASK
how B-MSG
are I-MSG
yo
我正在使用python和nltk来研究一些文本,我想要比较不同文本中词性的频率分布。
我可以通过一条文本来实现:
from nltk import *
X_tagged = pos_tag(word_tokenize(open('/Users/X.txt').read()))
X_fd = FreqDist([tag for word, tag in X_tagged])
X_fd.plot(cumulative=True, title='Part of Speech Distribution in Corpus X')
我试着添加另一个,但没有太多的运气。
我知道Knuth用于生成随机泊松分布数的算法(下面用Java语言),但是我如何将其转换为随时间随机调用方法generateEvent()?
int poissonRandomNumber(int lambda) {
double L = Math.exp(-lambda);
int k = 0;
double p = 1;
do {
k = k + 1;
double u = Math.random();
p = p * u;
} while (p > L);
return k - 1;
}
我正在尝试使用nltk.ConditionalFreqDist生成的表,但似乎找不到任何关于将表写入csv文件或导出为其他格式的文档。我喜欢在pandas dataframe对象中使用它,这也很容易写到csv中。我能找到的唯一一个推荐的线程并没有真正解决我的问题。
我编写了以下函数将nltk.ConditionalFreqDist对象转换为pd.DataFrame:
def nltk_cfd_to_pd_dataframe(cfd):
""" Converts an nltk.ConditionalFreqDist object into a pandas Da