我有一个包含 3 个文档的索引。
{
"firstname": "Anne",
"lastname": "Borg",
}
{
"firstname": "Leanne",
"lastname": "Ray"
},
{
"firstname": "Anne",
"middlename": "M",
"lastname": "Stone"
}
Run Code Online (Sandbox Code Playgroud)
当我搜索“Anne”时,我希望弹性返回所有 3 个文档(因为它们都在一定程度上与术语“Anne”匹配)。但是,我希望 Leanne Ray 具有较低的分数(相关性排名),因为搜索词“Anne”在此文档中出现的位置比该词在其他两个文档中出现的位置晚。
最初,我使用的是 ngram 分词器。我的索引映射中还有一个名为“full_name”的生成字段,其中包含名字、中间名和姓氏字符串。当我搜索“Anne”时,所有 3 个文档都在结果集中。然而,安妮·M·斯通(Anne M Stone)的分数与莉安·雷(Leanne Ray)相同。Anne M Stone 的分数应该比 Leanne 更高。
为了解决这个问题,我将 ngram 分词器更改为 edge_ngram 分词器。这具有将 Leanne Ray 完全排除在结果集中的效果。我们希望将此结果保留在结果集中 - 因为它仍然包含查询字符串 - 但其分数低于其他两个更好的匹配项。
我在某处读到,可以在同一索引中将边缘 ngram 过滤器与 ngram 过滤器一起使用。如果是这样,我应该如何重新创建索引来执行此操作?有更好的解决方案吗?
这是初始索引设置。
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"filter": [
"lowercase"
],
"type": "custom",
"tokenizer": "my_tokenizer"
}
},
"tokenizer": …
Run Code Online (Sandbox Code Playgroud) 我正在回顾一些关于 n 元语法的笔记,并且遇到了一些有趣的函数。首先是生成二元组:
def bigrams(word):
return sorted(list(set(''.join(bigram)
for bigram in zip(word,word[1:]))))
def bigram_print(word):
print("The bigrams of", word, "are:")
print(bigrams(word))
bigram_print("ababa")
bigram_print("babab")
Run Code Online (Sandbox Code Playgroud)
在自己阅读并使用 Python 后,我明白了为什么这是有效的。然而,当我看到这个函数时,我对这里的使用感到非常困惑zip(*word[i:])
。我知道这*
是一个解包运算符(如此处所解释的),但我真的被它如何与列表理解结合使用而绊倒了。谁能解释一下吗?
def ngrams(word, n):
return sorted(list(set(''.join(ngram)
for ngram in zip(*[word[i:]
for i in range(n)]))))
def ngram_print(word, n):
print("The {}-grams of {} are:".format(n, word))
print(ngrams(word, n))
for n in [2, 3, 4]:
ngram_print("ababa", n)
ngram_print("babab", n)
print()
Run Code Online (Sandbox Code Playgroud) 我很好奇,如果有人理解,知道或可以指向我关于谷歌如何创建他们的流行通道块功能的综合文献或源代码.但是,如果您知道任何其他可以执行相同操作的应用程序,请发布您的答案.
如果您不知道我在写什么,这里有一个热门通道示例的链接.当您查看" 信息技术应用程序的法律决策过程建模 "一书的概述时......通过Georgios N. Yannopoulos,您可以看到如下内容:
热门段落
......方向,不确定.我们还没有解决,因为我们没有预料到,这个问题会在未经审理的案件发生时提出; 公园的某种程度的和平是否应该牺牲或捍卫那些使用这些东西的乐趣或兴趣的孩子.当出现未经审查的案件时,我们就会面临利害攸关的问题,然后通过在最能满足我们的方式中选择竞争利益来解决问题.在做什么...... 第86页
这将是适合"机械"法理学的世界.显然,这个世界不是我们的世界; 人类立法者无法了解未来可能带来的所有可能的情况组合.这种无法预测会带来相对不确定性的目标.当我们大胆地进行框架的一些通用规则(例如,没有车辆可能是考虑到公园的规则),在这种情况下所使用的语言修复任何事物必须满足必要的条件... 第86页
它必须是密集的模式匹配过程.我只能想到n-gram模型,文本语料库,自动plagisrism检测.但是,有时n-gram是用于预测序列中的下一个项目的概率模型,并且手动创建文本语料库(据我所知).而且,在这个特殊情况下,流行的段落,可能会有很多单词.
我真的迷路了.如果我想创建这样的功能,我应该如何或在哪里开始?另外,在你的回复中包含哪些编程语言最适合这些东西:F#或任何其他功能性语言,PERL,Python,Java ......(我自己也成为了F#粉丝)
PS:有人可以包含标签自动抄袭检测,因为我不能
text-processing pattern-recognition corpus n-gram plagiarism-detection
作为更好地理解我目前正在学习的F#的一部分,我编写了将给定字符串拆分为n-gram的函数.
1)我想收到有关我的功能的反馈:这可以更简单或更有效地编写吗?
2)我的总体目标是编写基于n-gram相似性返回字符串相似度(在0.0 ... 1.0范围内)的函数; 这种方法是否适用于短字符串比较,或者这种方法可以可靠地用于比较大字符串(例如文章).
3)我知道n-gram比较忽略了两个字符串的上下文.你建议用什么方法来实现我的目标?
//s:string - target string to split into n-grams
//n:int - n-gram size to split string into
let ngram_split (s:string, n:int) =
let ngram_count = s.Length - (s.Length % n)
let ngram_list = List.init ngram_count (fun i ->
if( i + n >= s.Length ) then
s.Substring(i,s.Length - i) + String.init ((i + n) - s.Length)
(fun i -> "#")
else
s.Substring(i,n)
)
let ngram_array_unique = ngram_list
|> Seq.ofList
|> Seq.distinct
|> Array.ofSeq
//produce …
Run Code Online (Sandbox Code Playgroud) 我正在使用来自nltk的NgramModel来计算在句子中找到某个单词的概率.我的问题是每个单词每次都给出完全相同的概率,无论上下文如何!这是一些演示我的问题的示例代码.
from nltk.corpus import brown
from nltk.probability import LidstoneProbDist, WittenBellProbDist
from nltk.model import NgramModel
estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)
lm = NgramModel(3, brown.words(categories='news'), estimator=estimator)
>>> print lm.prob("word", ["This is a context which generates a word"])
0.00493261081006
>>> print lm.prob("word", ["This is a context of a word"])
0.00493261081006
>>> print lm.prob("word", ["This word"])
0.00493261081006
>>> print lm.prob("word", ["word"])
0.00493261081006
>>> print lm.prob("word", ["adnga"])
0.00493261081006
Run Code Online (Sandbox Code Playgroud) 我的Hibernate Search分析器配置存在一些问题.我的一个索引实体("Hospital")有一个String字段("name"),它可以包含长度为1-40的值.我希望能够通过搜索一个字符找到一个实体(因为有可能,医院有单个字符名称).
@Indexed(index = "HospitalIndex")
@AnalyzerDef(name = "ngram",
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = StandardFilterFactory.class),
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = NGramFilterFactory.class,
params = {
@Parameter(name = "minGramSize", value = "1"),
@Parameter(name = "maxGramSize", value = "40")})
})
public class Hospital {
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO, analyzer = @Analyzer(definition = "ngram"))
private String name = "";
}
Run Code Online (Sandbox Code Playgroud)
如果我添加名为"My Test Hospital"的医院,Lucene索引如下所示:
1 name al
1 name e
1 name es
1 name est …
Run Code Online (Sandbox Code Playgroud) 下面是我拥有的输入数据框。
id description
1 **must watch avoid** **good acting**
2 average movie bad acting
3 good movie **acting good**
4 pathetic avoid
5 **avoid watch must**
Run Code Online (Sandbox Code Playgroud)
我想从短语中常用的单词中提取 ngrams,即 bigram、trigram 和 4 wordgram。让我们将短语标记为单词,那么即使常用单词的顺序不同,我们也可以找到 ngrams ie(如果我们经常使用单词“good movie”和在第二个短语我们经常使用的词是“电影好”,我们可以将二元词提取为“好电影”)。我期望的示例如下所示:
ngram frequency
must watch 2
acting good 2
must watch avoid 2
average 1
Run Code Online (Sandbox Code Playgroud)
正如我们在第一句中看到的常用词是“must watch”,在最后一句中,我们有“watch must”,即频繁出现的词的顺序发生了变化。因此,它以 2 的频率提取必须观看的二元组。
我需要从短语中的常用单词中提取 ngrams/bigrams。
如何使用 Python 数据框实现这一点?任何帮助是极大的赞赏。
谢谢!
# Input
n <- 2
"abcd"
# Output
c("ab", "bc", "cd")
Run Code Online (Sandbox Code Playgroud)
I don't want to use a for
loop or sapply
我有一个输入文件,由带有数字和单词序列的行组成,结构如下:
\1-grams:
number w1 number
number w2 number
\2-grams:
number w1 w2 number
number w1 w3 number
number w2 w3 number
\end\
Run Code Online (Sandbox Code Playgroud)
我希望以这样的方式存储单词序列(所谓的n-gram),以便我可以轻松地为每个唯一的n-gram检索两个数字.我现在做的是以下内容:
all = {}
ngrams = {}
for line in open(file):
m = re.search('\\\([1-9])-grams:',line.strip()) # find nr of words in sequence
if m != None:
n = int(m.group(1))
ngrams = {} # reinitialize dict for new n
else:
m = re.search('(-[0-9]+?[\.]?[0-9]+)\t([^\t]+)\t?(-[0-9]+\.[0-9]+)?',line.strip()) #find numbers and word sequence
if m != None:
ngrams[m.group(2)] = '{0}|{1}'.format(m.group(1), m.group(3))
elif "\end\\" == line.strip(): …
Run Code Online (Sandbox Code Playgroud) 移动设备和平板电脑的现代键盘中使用的下一字预测或短语预测引擎,如swift key&XT9,它基于n-gram预测用户将基于某些预定义或动态语料库键入的下一个单词(最后输入的2-3个字加上当前字的最大频率)基于语言模型(马尔可夫模型).
我认为这些引擎/算法是AI/NLP的一部分.但我不确定的是他们所属的AI/NLP的具体分支.机器学习吗?是数据科学吗?是大数据吗?是计算智能吗?是决策吗?是数据挖掘吗?或统计模式识别/预测分析/监督学习/无监督学习?或者全部/许多这些或其他什么?
nlp artificial-intelligence machine-learning prediction n-gram
所以我有一个像这样创建的弹性搜索索引:
curl -XPUT 'http://localhost:9200/person' -d '{
"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)
在查询名为“ian”的人时,我得到两个结果
curl -XGET http://localhost:9200/person/_search -d '{
"query": {
"match": {
"_all": "ian"
}
}
}’
Run Code Online (Sandbox Code Playgroud)
但是在仅查询字母时ia
,我应该得到尽可能多或更多的结果,但我没有得到任何结果:
curl -XGET http://localhost:9200/person/_search -d '{
"query": {
"match": {
"_all": "ia"
}
}
}’
Run Code Online (Sandbox Code Playgroud)
我的edge_ngram
过滤器设置有问题吗?我该如何解决这个问题?
编辑:澄清一下,我希望我的插入语句看起来像这样
curl -XPOST …
Run Code Online (Sandbox Code Playgroud) 我有一个 PHP 数组:
$excerpts = array(
'I love cheap red apples',
'Cheap red apples are what I love',
'Do you sell cheap red apples?',
'I want red apples',
'Give me my red apples',
'OK now where are my apples?'
);
Run Code Online (Sandbox Code Playgroud)
我想找到这些行中的所有 n-gram 以获得如下结果:
我尝试内爆数组,然后解析它,但这很愚蠢,因为可以找到新的 n 元语法,因为字符串之间没有任何可看的串联。
你将如何进行?