我正在寻找一种将文本分成n-gram的方法.通常我会做类似的事情:
import nltk
from nltk import bigrams
string = "I really like python, it's pretty awesome."
string_bigrams = bigrams(string)
print string_bigrams
Run Code Online (Sandbox Code Playgroud)
我知道nltk只提供bigrams和trigrams,但有没有办法将我的文本分成4克,5克甚至100克?
谢谢!
我需要比较存储在数据库中的文档,并得出0到1之间的相似性得分.
我需要使用的方法必须非常简单.实现n-gram的vanilla版本(可以定义要使用的克数),以及tf-idf和余弦相似度的简单实现.
有没有可以做到这一点的程序?或者我应该从头开始写这个?
我想要执行精确的单词匹配和部分单词/子串匹配.例如,如果我搜索"男士剃须刀",那么我应该能够在结果中找到"男士剃须刀".但是在我搜索"en的剃须刀"的情况下,我也应该能够在结果中找到"男士剃须刀".我使用以下设置和映射:
索引设置:
PUT /my_index
{
"settings": {
"number_of_shards": 1,
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
映射:
PUT /my_index/my_type/_mapping
{
"my_type": {
"properties": {
"name": {
"type": "string",
"index_analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
插入记录:
POST /my_index/my_type/_bulk
{ "index": { "_id": 1 }}
{ "name": "men's shaver" }
{ "index": { "_id": 2 …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将几个文件加载到内存中.这些文件具有以下3种格式之一:
实际上,它们是ngram静态文件,以防这有助于解决方案.例如:
i_love TAB 10
love_you TAB 12
Run Code Online (Sandbox Code Playgroud)
目前,我正在做的伪代码是
loadData(file):
data = {}
for line in file:
first, second = line.split('\t')
data[first] = int(second) #or float(second)
return data
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,虽然磁盘中文件的总大小约为21 MB,但当加载到内存中时,该过程需要120 - 180 MB的内存!(整个python应用程序不会将任何其他数据加载到内存中).
只有不到10个文件,大多数文件在大约50-80k行保持稳定,除了一个目前有数百万行的文件.
所以我想要一个技术/数据结构来减少内存消耗:
非常感谢你.我期待着你的建议.
如何生成一个n-gram的字符串,如:
String Input="This is my car."
Run Code Online (Sandbox Code Playgroud)
我想用这个输入生成n-gram:
Input Ngram size = 3
Run Code Online (Sandbox Code Playgroud)
输出应该是:
This
is
my
car
This is
is my
my car
This is my
is my car
Run Code Online (Sandbox Code Playgroud)
在Java中给出一些想法,如何实现它或者是否有可用的库.
我正在尝试使用这个NGramTokenizer,但它给出了n-gram的字符序列,我想要n-gram的单词序列.
我想使用ElasticSearch搜索文件名(而不是文件的内容).因此,我需要找到文件名的一部分(完全匹配,没有模糊搜索).
示例:
我有以下名称的文件:
My_first_file_created_at_2012.01.13.doc
My_second_file_created_at_2012.01.13.pdf
Another file.txt
And_again_another_file.docx
foo.bar.txt
Run Code Online (Sandbox Code Playgroud)
现在我想搜索2012.01.13
获取前两个文件.
搜索file
或ile
应返回除最后一个之外的所有文件名.
我如何使用ElasticSearch实现这一目标?
这是我测试过的,但它总是返回零结果:
curl -X DELETE localhost:9200/files
curl -X PUT localhost:9200/files -d '
{
"settings" : {
"index" : {
"analysis" : {
"analyzer" : {
"filename_analyzer" : {
"type" : "custom",
"tokenizer" : "lowercase",
"filter" : ["filename_stop", "filename_ngram"]
}
},
"filter" : {
"filename_stop" : {
"type" : "stop",
"stopwords" : ["doc", "pdf", "docx"]
},
"filename_ngram" : {
"type" : "nGram",
"min_gram" : …
Run Code Online (Sandbox Code Playgroud) 我对如何在Python中的scikit-learn库中使用ngrams感到有点困惑,具体来说,这个ngram_range
参数在CountVectorizer中是如何工作的.
运行此代码:
from sklearn.feature_extraction.text import CountVectorizer
vocabulary = ['hi ', 'bye', 'run away']
cv = CountVectorizer(vocabulary=vocabulary, ngram_range=(1, 2))
print cv.vocabulary_
Run Code Online (Sandbox Code Playgroud)
给我:
{'hi ': 0, 'bye': 1, 'run away': 2}
Run Code Online (Sandbox Code Playgroud)
在我明显错误的印象中,我会得到unigrams和bigrams,就像这样:
{'hi ': 0, 'bye': 1, 'run away': 2, 'run': 3, 'away': 4}
Run Code Online (Sandbox Code Playgroud)
我正在使用这里的文档:http: //scikit-learn.org/stable/modules/feature_extraction.html
显然,我对如何使用ngrams的理解存在严重错误.也许这个论点没有效果,或者我对一个真正的二元组有一些概念上的问题!我很难过.如果有人提出建议,我会感激不尽.
更新:
我意识到了我的方式的愚蠢.我的印象是ngram_range
会影响词汇,而不是语料库.
我有以下代码.我知道我可以使用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) 我需要为包含以下文本的文本文件计算Unigrams,BiGrams和Trigrams:
"囊性纤维化仅影响美国3万名儿童和青少年.吸入盐水雾可减少充满囊性纤维化患者呼吸道的脓液和感染,但副作用包括令人讨厌的咳嗽和严酷的味道.这就是结论在本周出版的"新英格兰医学杂志"上发表的两项研究."
我从Python开始并使用以下代码:
#!/usr/bin/env python
# File: n-gram.py
def N_Gram(N,text):
NList = [] # start with an empty list
if N> 1:
space = " " * (N-1) # add N - 1 spaces
text = space + text + space # add both in front and back
# append the slices [i:i+N] to NList
for i in range( len(text) - (N - 1) ):
NList.append(text[i:i+N])
return NList # return the list
# test code
for i in range(5):
print …
Run Code Online (Sandbox Code Playgroud) 我的问题在概念上类似于解决字谜,除了我不能只使用字典查找.我试图找到合理的词而不是真实的词.
我已经基于一堆文本中的字母创建了一个N-gram模型(现在,N = 2).现在,给定一个随机的字母序列,我想根据转移概率将它们置于最可能的序列中.我认为在开始时我需要维特比算法,但随着我看起来更深入,维特比算法根据观察到的输出优化了一系列隐藏的随机变量.我正在尝试优化输出序列.
有没有一个众所周知的算法,我可以阅读?或者我是否与Viterbi走在正确的轨道上,我只是没有看到如何应用它?
更新
我已经添加了一笔赏金来要求更深入地了解这个问题.(分析解释为什么不能采用有效的方法,除模拟退火之外的其他启发式/近似等)
algorithm machine-learning mathematical-optimization markov n-gram
n-gram ×10
python ×6
nltk ×3
lucene ×2
nlp ×2
algorithm ×1
autocomplete ×1
compression ×1
dictionary ×1
document ×1
java ×1
markov ×1
memory ×1
scikit-learn ×1
string ×1
substring ×1
tf-idf ×1
vsm ×1