Home Php C# Sql C C++ Javascript Python Java Go Android Git Linux Asp.net Django .net Node.js Ios Xcode Cocoa Iphone Mysql Tomcat Mongodb Bash Objective-c Scala Visual-studio Apache Elasticsearch Jar Eclipse Jquery Ruby-on-rails Ruby Rubygems Android-studio Spring Lua Sqlite Emacs Ubuntu Perl Docker Swift Amazon-web-services Svn Html Ajax Xml Java-ee Maven Intellij-idea Rvm Macos Unix Css Ipad Postgresql Css3 Json Windows-server Vue.js Typescript Oracle Hibernate Internet-explorer Github Tensorflow Laravel Symfony Redis Html5 Google-app-engine Nginx Firefox Sqlalchemy Lucene Erlang Flask Vim Solr Webview Facebook Zend-framework Virtualenv Nosql Ide Twitter Safari Flutter Bundle Phonegap Centos Sphinx Actionscript Tornado Register | Login | Edit Tags | New Questions | 繁体 | 简体


10 questions online user: 8

0
votes
answers
53 views
+10

Pocketsphinx在python回報關鍵字搜索隨機單詞

0

我從網站複製代碼聽特定字詞使用Python pocketsphinx.It雖然運行,但從來沒有輸出關鍵字作爲expected.This是我的代碼:Pocketsphinx在python回報關鍵字搜索隨機單詞

import sys, os 
from pocketsphinx.pocketsphinx import * 
from sphinxbase.sphinxbase import * 
import pyaudio 

# modeldir = "../../../model" 
# datadir = "../../../test/data" 

modeldir="C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//en-us" 
dictdir="C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//cmudict-en-us.dict" 
lmdir="C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//en-us.lm.bin" 
# Create a decoder with certain model 
config = Decoder.default_config() 
config.set_string('-hmm', modeldir) 
config.set_string('-lm', lmdir) 
config.set_string('-dict', dictdir) 
config.set_string('-keyphrase', 'forward') 
config.set_float('-kws_threshold', 1e+20) 

p = pyaudio.PyAudio() 
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024) 
stream.start_stream() 

# Process audio chunk by chunk. On keyword detected perform action and restart search 
decoder = Decoder(config) 
decoder.start_utt() 
while True: 
    buf = stream.read(1024) 
    if buf: 
     decoder.process_raw(buf, False, False) 
    else: 
     break 
    if decoder.hyp() != None: 
     #print(decoder.hyp().hypstr) 
     if decoder.hyp().hypstr == 'forward': 
     print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()]) 
     print ("Detected keyword, restarting search") 
     decoder.end_utt() 
     decoder.start_utt() 

此外,當我使用print(decoder.hyp().hypstr)

它只是輸出隨機單詞時,我如果我說一個字或行其輸出講anything.For例如:

the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the da 
the head 
the bed 
the bedding 
the heading of 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and well 
the bedding and well 
the bedding and well 
the bedding and butler 
the bedding and what lingus 
the bedding and what lingus 
the bedding and what lingus 
the bedding and what lingus ha 
the bedding and blessed are 
the bedding and blessed are 
the bedding and what lingus on 
the bedding and what lingus want 
the bedding and what lingus want 
the bedding and what lingus want 
the bedding and what lingus want 
the bedding and what lingus want or 
the bedding and what lingus want to talk 
the bedding and what lingus current top 
the bedding and what lingus want to talk 
the bedding and what lingus want to talk 
the bedding and what lingus want to talk 
the bedding and what lingus want to talk 
the bedding and what lingus want to talk to her 
the bedding and what lingus want to talk to her 
the bedding and what lingus want to talk to her 
the bedding and what lingus want to talk to her 

請幫助我通過它。我只是一個Python新手。

沙发
0
1

首先,我只是想澄清;你的Pocketsphinx 工作。

因此,根據我使用pocketsphinx的經驗,您幾乎可以使用most accurate語音識別工具,但可能是您離線解決方案的最佳選擇。 Pocketsphinx只能翻譯您的文字(音頻),最好像它的'model規定的那樣。這些模型似乎仍然是一項正在進行的工作,其中大部分需要改進。有幾件事你可以嘗試提高識別的準確性;如reducing noisetuning the recognition,但這不在此問題的直接範圍之內。

從我的理解你的代碼中,你正在尋找一個特定的關鍵字被說出來(用戶的聲音),並使用pocketshinx的後端識別它。這個關鍵詞似乎是「前進」的。你可以進一步閱讀如何正確完成"hot word listening"

你有正確的想法,但方法可以改進。這是我的「速戰速決」版本的代碼:

import os 
import pyaudio 
import pocketsphinx as ps 

modeldir = "C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//" 

# Create a decoder with certain model 
config = ps.Decoder.default_config() 
config.set_string('-hmm', os.path.join(modeldir, 'en-us')) 
config.set_string('-lm', os.path.join(modeldir, 'en-us.lm.bin')) 
config.set_string('-dict', os.path.join(modeldir, 'cmudict-en-us.dict')) 
config.set_string('-keyphrase', 'forward') 
config.set_float('-kws_threshold', 1e+20) 

p = pyaudio.PyAudio() 
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024) 
stream.start_stream() 

# Process audio chunk by chunk. On keyword detected perform action and restart search 
decoder = ps.Decoder(config) 
decoder.start_utt() 

while True: 
    buf = stream.read(1024) 
    if buf: 
     decoder.process_raw(buf, False, False) 
    else: 
     break 
    if decoder.hyp() is not None: 
     print(decoder.hyp().hypstr) 
     if 'forward' in decoder.hyp().hypstr: 
      print([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()]) 
      print("Detected keyword, restarting search") 
      decoder.end_utt() 
      decoder.start_utt() 

對於任何一個pocketsphinx.Decoder()「會話」(即調用.start_utt()方法而隨後調用.ent_utt()),該decoder.hyp().hypstr變量將繼續有效將單詞添加到自身一旦它檢測到輸入音頻流具有來自pocketsphinx解碼的「有效」翻譯/識別。

您已使用if decoder.hyp().hypstr == 'forward':。它所做的是強制整個字符串完全「向前」,以便代碼進入(我認爲,期望...是?)條件代碼塊。由於pocketshinx默認情況下不是很準確,因此通常需要嘗試大部分單詞才能實際註冊正確的單詞。由於這個原因,並且自從decoder.hyp().hypstr增加到自身(如前所述),我已經使用了線if 'forward' in decoder.hyp().hypstr:。這會在整個字符串中查找所需的關鍵字「forward」。這樣,直到找到關鍵字才允許識別錯誤。

我希望它有幫助!

+0

謝謝你的回答。但是這段代碼對兄弟來說太沒有幫助了。它永遠不會在語言中識別單詞「前進」,而只是在我對它說話時打印隨機單詞。是否有什麼我在模型中缺少的東西? – TechieBoy101

+0

所有這一切意味着pocketsphinx的「翻譯」對於您輸入的數據而言並不十分準確。因此,正如我指出的那樣,在pocketsphinx正確識別您的單詞之前,您將不得不嘗試幾次(多次)。我明白這是多麼令人不滿。然後,您需要查看**增加識別的準確性,**正確**執行「熱門詞彙收聽」。這些鏈接在我原來的答案中提供。 –

板凳
0
0

您需要刪除此行

config.set_string('-lm', lmdir) 

關鍵詞的搜索和LM搜索是互斥的。

+0

非常感謝,真的很有用。我需要問你是否有辦法在pocketsphinx中聽到1個以上的關鍵詞或句子。這可能嗎? – TechieBoy101

+0

是的,您可以使用關鍵詞列表,請參閱http://cmusphinx.github.io/wiki/tutoriallm –

0
votes
answers
57 views
+10

Python 3和Pocket Sphinx

3

我試圖爲我的祖父創建一個簡單的音樂播放器,通過編程音頻控制音樂播放器來解決按鈕問題。我正在用Python 3和Pocket Sphinx使用Raspberry Pi 3。因爲Pocket Sphinx不需要互聯網,所以我會使用它,因爲我的祖父無法訪問互聯網。Python 3和Pocket Sphinx

我的問題是如何採取「說」的值,例如:「播放按鈕」,並讓它播放波形文件「按鈕」?

這是我必須建立基本程序:

import speech_recognition as sr 
import pygame 
from pygame import mixer 
mixer.init() 

r = sr.Recognizer() 
m = sr.Microphone() 

Button = pygame.mixer.Sound('/home/pi/Downloads/button8.wav') 

try: 
    print("A moment of silence, please...") 
    with m as source: r.adjust_for_ambient_noise(source) 
    print("Set minimum energy threshold to {}".format(r.energy_threshold)) 
    while True: 
     print("Say something!") 
     with m as source: audio = r.listen(source) 
     print("Got it! Now to recognize it...") 
     try: 
      # recognize speech using Sphinx 
      value = r.recognize_sphinx(audio) 
      print("You said {}".format(value)) #uses unicode for strings and this is where I am stuck 
      pygame.mixer.Sound.play(Button) 
      pygame.mixer.music.stop() 
     except sr.UnknownValueError: 
      print("Oops! Didn't catch that") 
     except sr.RequestError as e: 
      print("Uh oh! Couldn't request results; {0}".format(e)) 
except KeyboardInterrupt: 
    pass 

非常感謝你的幫助,你可以提供。請善待我,因爲我是初學者。

+0

你什麼錯誤? –

+0

它會打印什麼,但我不知道如何採取什麼說播放聲音。例如,我希望能夠說「播放按鈕」並讓它播放Pi上的Wave文件(Button.wav)。感謝您的幫助。 –

沙发
0
1

嘗試比較它與 '播放鍵':

# recognize speech using Sphinx 
value = r.recognize_sphinx(audio) 
print("You said {}".format(value)) 

if value.lower() == 'play button': 
    pygame.mixer.Sound.play(Button) 
    pygame.mixer.music.stop() 
+1

你做到了Elis Byberi!非常感謝!!!謝謝謝謝!那麼,「if value.lower()」表示什麼?它看着python腳本的打印值嗎?非常感謝你的幫助! –

+0

value.lower()在字符串值中做小寫字母。比較'如果'播放按鈕'=='播放按鈕'是錯誤的。 'if'確實檢查字符串的值是否與'播放按鈕'相同。 @LauraJacob –

+1

非常感謝你的幫助!我衷心感謝! –

0
votes
answers
44 views
+10

獅身人面像在Anaconda下運行,但我的Django項目在Python 3.5.2下

0

我triyng用獅身人面像來記錄我的django項目,但我遇到了一些麻煩。獅身人面像在Anaconda下運行,但我的Django項目在Python 3.5.2下

當我運行make html命令我得到這個錯誤:

/home/santoryu/richiestaTesi/docs/models/models.rst:3: WARNING: autodoc: 
failed to import module u'myapp.models'; the following exception was 
raised: 
Traceback (most recent call last): 
    File "/home/santoryu/anaconda2/lib/python2.7/site-packages/Sphinx- 
1.5.6-py2.7.egg/sphinx/ext/autodoc.py", line 560, in import_object 
__import__(self.modname) 
    File "/home/santoryu/richiestaTesi/myapp/models.py", line 8 
SyntaxError: Non-ASCII character 'xc3' in file 
/home/santoryu/richiestaTesi/myapp/models.py on line 8, but no encoding 
declared; see http://python.org/dev/peps/pep-0263/ for details 

很明顯的錯誤發生,因爲蟒蛇非ASCII字符本身不支持,但在Python 3.5.2他們。所以我猜如何強制Python 3.5.2執行Sphinx?

+0

項目被記錄python 3特定? –

沙发
0
2

錯誤消息表明您已將Sphinx安裝到Python 2.7虛擬環境中。

File "/home/santoryu/anaconda2/lib/python2.7/site-packages/Sphinx- 
1.5.6-py2.7.egg/sphinx/ext/autodoc.py", line 560, in import_object 
__import__(self.modname) 

您也可以爲Python 2.7而不是3.6安裝Anaconda。

確保您下載並安裝了適用於Python 3.6的Anaconda並且您的use Anaconda to create a virtual environment using Python 3.6用於安裝Sphinx。

3
votes
answers
53 views
+10

由於searchd.pid缺席,因此sphinx on centos 7無法啟動

我在CentOS 7上安裝了sphinx-2.2.11 yum install -y postgresql-libs unixODBC wget http://sphinxsearch.com/files/sphinx-2.2.11-1.rhel7.x86_64.rpm yum install sphinx-2.2.11-1.rhel7.x86_64.rpm 安裝沒有任何錯誤,然後我創建了sphinx配置並安裝了php擴展(也都沒有錯誤) 我重新啟動apache(httpd),我嘗試啟動sphinx服務 systemctl start searchd 我收到了這條消息 searchd.service的作業失敗,因為超出了配置的資源限制。有關詳細信息,請參閱“systemctl status searchd.service”和“journalctl -xe”。 啟動命令後 - systemctl status searchd.service 5月2日20:28:57 kvmde43-10657.fornex.org systemd [1]:無法從文件/var/run/sphinx/searchd.pid中讀取PID:無效參數5月02日20:28:57 kvmde43-10657.fornex .org systemd [1]:無法啟動SphinxSearch搜索引擎。 事實上,我在系統的任何地方都沒有“searchd.pid”,雖然安裝很順利)我該如何解決? 提前致謝
沙发
+30

I have just resolved this issue.

I took a look at sphinx log /var/log/sphinx/searchd.log and noted that some data files under folder /var/log/sphinx/data/ are Permission denied;

I set chown sphinx:sphinx on /var/log/sphinx/data/ folder and It started to work as charm )

Thanks

6
votes
answers
43 views
+10

sphinx config || 配置/ sphinx.yml

我的獅身人面像配置是: ================================ config/sphinx.yml development: bin_path: "/usr/local/bin" searchd_binary_name: searchd indexer_binary_name: indexer 但每次我跑一個 rake ts:index Sphinx cannot be found on your system. You may need to configure the following settings in your config/sphinx.yml file: * bin_path * searchd_binary_name * indexer_binary_name For more information, read the documentation: For more information, read the documentation: http://freelancing-god.github.com/ts/en/advanced_config.html Generating Configuration to config/development.sphinx.conf Sphinx 2.0.1-beta (r2792) Copyright (c) 2001-2011, Andrew Aksyonoff Copyright (c) 2008-2011, Sphinx Technologies Inc (http://sphinxsearch.com) using config file 'config/development.sphinx.conf'... indexing index 'post_core'... collected 2 docs, 0.0 MB sorted 0.0 Mhits, 100.0% done total 2 docs, 675 bytes total 0.006 sec, 110510 bytes/sec, 327.43 docs/sec skipping non-plain index 'post'... total 6 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg total 12 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg rotating indices: succesfully sent SIGHUP to searchd (pid=19438). Generating Configuration to config/development.sphinx.conf Sphinx 2.0.1-beta (r2792) Copyright (c) 2001-2011, Andrew Aksyonoff Copyright (c) 2008-2011, Sphinx Technologies Inc (http://sphinxsearch.com) using config file 'config/development.sphinx.conf'... indexing index 'post_core'... collected 2 docs, 0.0 MB sorted 0.0 Mhits, 100.0% done total 2 docs, 675 bytes total 0.006 sec, 105567 bytes/sec, 312.79 docs/sec skipping non-plain index 'post'... total 6 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg total 12 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg rotating indices: succesfully sent SIGHUP to searchd (pid=19438). 所以有什麼問題?為什麼即使安裝它也無法找到它的rake輸出?
沙发
+30

The warning from Thinking Sphinx could definitely be clearer... the problem is very likely to be how old your version of Thinking Sphinx is. Older TS versions don't know about Sphinx 2.0.x - so I'd recommend updating to the latest version of Thinking Sphinx (either 1.4.6 for Rails 1.2 and 2.x, or 2.0.5 for Rails 3).

板凳
+20

There are two things that help to solve this problem. First, as Pat says, it is useful to update the Thinking Sphinx plugin or gem to the latest version (either 1.4.x for Rails 2, or 2.0.x for Rails 3). Second it helps sometimes to specify the version of Sphinx in the configuration file (you can find it out by calling "indexer"), especially if Sphinx is running on a remote server and Thinking Sphinx does not have access to Sphinx locally:

production:
  ..
  version: 2.0.4  # <------- Version of Sphinx on remote server 192.168.1.10
  port: 9312 
  address: 192.168.1.10
  ..
地板
+10

I was facing the same issue and looked everywhere for an answer without any resolution.

The trick that worked for me was to install older version of sphinx. v .9 instead of the latest beta.

Using the latest Thinking-Sphinx with this version of sphinx resolved the issue.

5
votes
answers
27 views
+10

如何在LINUX上安裝Sphinx(ubuntu)

這是Raja,我正在使用PHP進行Web開發。最近我應該用LINUX作為操作系統將“Sphinx”搜索安裝到我的應用程序中。 我遵循了本網站提供的指示 http://www.howtoforge.com/how-to-install-sphinx-on-ubuntu-10.10 在LINUX(Ubuntu)操作系統中安裝sphinx並且我在終端中測試了應用程序,我得到了這樣的結果 root@dev2:/# search "test" Sphinx 0.9.9-release (r2117) Copyright (c) 2001-2009, Andrew Aksyonoff using config file '/etc/sphinxsearch/sphinx.conf'... index 'test1': query 'test ': returned 3 matches of 3 total in 0.000 sec displaying matches: 1. document=1, weight=2, group_id=1, date_added=Thu Jan 5 18:18:55 2012 id=1 group_id=1 group_id2=5 date_added=2012-01-05 18:18:55 title=test one content=this is my test document number one. also checking search within phrases. 2. document=2, weight=2, group_id=1, date_added=Thu Jan 5 18:18:55 2012 id=2 group_id=1 group_id2=6 date_added=2012-01-05 18:18:55 title=test two content=this is my test document number two 3. document=4, weight=1, group_id=2, date_added=Thu Jan 5 18:18:55 2012 id=4 group_id=2 group_id2=8 date_added=2012-01-05 18:18:55 title=doc number four content=this is to test groups words: 1. 'test': 3 documents, 5 hits 問題是,我在“/ var / www / sphinx_search”位置創建了目錄,其中包含sphinx.conf,sphinxapi.php和test.php文件。 sphinx.conf背後的代碼是 source src1 { type = mysql sql_host = localhost sql_user = user sql_pass = pwd sql_db = test sql_port = 3306 # optional, default is 3306 sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents sql_attr_uint = group_id sql_attr_timestamp = date_added sql_query_info = SELECT * FROM documents WHERE id=$id } index test1 { source = src1 path = /var/lib/sphinxsearch/data/test1 docinfo = extern charset_type = sbcs } indexer { mem_limit = 32M } searchd { port = 9312 log = /var/log/sphinxsearch/searchd.log query_log = /var/log/sphinxsearch/query.log read_timeout = 5 max_children = 30 pid_file = /var/run/searchd.pid max_matches = 1000 seamless_rotate = 1 preopen_indexes = 0 unlink_old = 1 } 和test.php背後的代碼是 require_once('sphinxapi.php'); //require_once('api/sphinxapi.php'); $s = new SphinxClient; $s->setServer("123.123.123.123", 9312); // NOT "localhost" under Windows 7! $s->setMatchMode(SPH_MATCH_EXTENDED2); $result = $s->Query("group"); echo '
';print_r($result);


當我試圖在瀏覽器中運行此應用程序(http:// localhost / Raja / search_engine / sphinx /)時,會顯示此錯誤。

{"status":"failed","status_message":"connection to localhost:9312 failed (errno=10060, msg=A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)"}


As i'm new to Sphinx, i cant get it right away. Can anybody help me to proceed further.

Curiously looking for a solution earlier,

Thanks in Advance,
Raja.
    
        
沙发
+30

In your example with search you using search utility which don't require searchd daemon to be running. That's why search is executed successfully.

To use sphinx API you need to start searchd daemon, like:

/path/to/searchd --config /etc/sphinxsearch/sphinx.conf

If you installed sphinx using apt-get then try:

searchd --config /etc/sphinxsearch/sphinx.conf

After that you could try application in browser.

But, I don't understand this

$s->setServer("123.123.123.123", 9312); // NOT "localhost" under Windows 7!

You said you running sphinx under ubuntu, not windows 7. So, sphinx server IP should be IP of ubuntu host, not localhost.

BTW, ubuntu ship old version of Sphinx, I would recommend to download latest from sphinxsearch.com. Latest version is 2.0.3

非常感謝您的回复,請您詳細解釋一下LINUX上的sphinx安裝。我很困惑。因為在LINUX中,我在/ var / www /維護我的源代碼。我需要在哪條路上安裝sphinx。在這一部分,我感到困惑。如果我清楚地解釋如何安裝以及如何在瀏覽器中運行應用程序,那將會很棒。 - 拉賈1月18日在13:02

板凳
+20

change your setServer() to this one

$s->SetServer('localhost', 'mysql_user_name', 'mysql_password', 3312);

and set the port as 3312 in your sphinx.conf

port = 3312

I thing this might work for you. If not tell me what happened.

5
votes
answers
32 views
+10

什麼是從Sphinx(在PHP中)檢索信息的最佳方法?

我是獅身人面像的新手,我正在一個新的網站上發布它。它工作正常,當我在控制台中搜索搜索時,一切正常。 使用PHP api和搜索,也給我相同的結果。但是它只為我找到的行提供了ID和權重。例如,有沒有辦法讓一些文本字段與'匹配'哈希一起使用? 如果沒有辦法做到這一點,是否有人知道如何以sphinx權重排序順序從數據庫(sql)中檢索記錄(同時搜索所有這些記錄)?
沙发
+10
+50

You can use a mysql FIELD() function call in your ORDER BY to ensure everything is in the order sphinx specified.


$idlist = array();
foreach ( $sphinx_result["matches"] as $id => $idinfo ) {
    $idlist[] = "$id";
}
$ids = implode(", ", $idlist);

SELECT * FROM table WHERE id IN ($ids) ORDER BY FIELD(id, $ids)

當IN()列表很大時,這可能會非常慢,因為MySQL通常會將其轉換為範圍掃描。我建議創建一個臨時表並加入它。 - 將於2012年7月3日15:26

+40

Yeah, sphinx doesn't bring the results. But I found out a simple way to reorder the query using the IN() clause, to bring all together.

Quering something

SELECT * FROM table WHERE id IN(id_list... )

just indexing the result, with their id in the table:

while ($row = mysql_fetch_objects)
   $result[$row->id] = $row; 

and having the matching results from sphinx, its very easy to reorder:

$ordered_result = array();
foreach ($sphinxs_results['matches'] as $id => $content)
   $ordered_result[] = $result1[$id];

this shall work, if your $sphinxs_results are in the correct order.

its almost pat's answer, but with less one loop. Can make some diference in big results, I guess.

0

unfortually sphinx didn't returns matched fields, only its ids (sphinx index didn't contains data - only hash from data). Post about this issue you can find on the sphinxsearch.com forum.

0

As Alex says, Sphinx doesn't return that information. You will have to use the IDs to query the database yourself - just loop through each ID, get your relevant data out, keeping the results in weighting order. To do it all in one query, you could try something like the following (psuedo-code - PHP ain't my language of choice):

results = db.query("SELECT * FROM table WHERE id IN (%s)", matches.join(", "));
ordered_results = [];

for (match in matches) {
  for (result in results) {
    if (result["id"] == match) {
      ordered_results << result;
    }
  }
}

return ordered_results;
4
votes
answers
27 views
+10

在SphinxSE中轉義特殊字符

我使用sphinx存儲引擎實現在我的網站上搜索,效果相當不錯,但是當搜索包含諸如&和@之類的字符時,搜索失敗並出現以下錯誤: There was a problem processing the query on the foreign data source. Data source error: search query already specified 和PHP拋出此錯誤: Warning: mysql_query() [function.mysql-query]: Unable to save result set in /home/path/to/file.php on line 100 我用它來逃避用戶的輸入 mysql_real_escape_string 有趣的是,如果我複制查詢並直接在phpmyadmin中運行它,我沒有錯誤。 query = '@title("cheese & cake");limit=1000filter=type=1;ranker=sph04;mode=extended;sort=extended:@weight desc;'
沙发
+40

Character escaping in Sphinxql is a tricky subject... I'm not sure if it is fully officially resolved. mysql_real_escape_string won't handle the special Sphinx query characters.

They do provide an escape function in sphinxapi.php:

function EscapeString ( $string )
{
    $from = array ( '\', '(',')','|','-','!','@','~','"','&', '/', '^', '$', '=' );
    $to   = array ( '\\', '(',')','|','-','!','@','~','"', '&', '/', '^', '$', '=' );
    return str_replace ( $from, $to, $string );
 }

Note that this won't specifically handle the SQL escape characters (for example, no single quote replacement). Actually, I tested it, and it doesn't even work just for Sphinx characters.

You need this function:

function EscapeSphinxQL ( $string )
{
    $from = array ( '\', '(',')','|','-','!','@','~','"','&', '/', '^', '$', '=', "'", "x00", "
", "
", "x1a" );
    $to   = array ( '\\', '\(','\)','\|','\-','\!','\@','\~','\"', '\&', '\/', '\^', '\$', '\=', "\'", "\x00", "\n", "\r", "\x1a" );
    return str_replace ( $from, $to, $string );
}

Note the extra backslashes on the Sphinx-specific characters. I think what happens is that they put your whole query through an SQL parser, which removes escape backslashes 'extraneous' for SQL purposes (i.e. '&' -> '&'). Then, it puts the MATCH clause through the fulltext parser, and suddenly '&' is a special character. So, you need the extra backslashes in the beginning.

4
votes
answers
22 views
+10

使用Sphinx進行多對多搜索

我想使用Sphinx進行多對多匹配。我在StackOverflow中將問題和標籤用於說明。 對於標籤,問題可以與許多標籤相關聯,反之亦然。 所以在mysql中,我有3個表:question,tag,question_tag。 我想在“java”,“sphinx”,“mysql”,“hibernate”集中搜索包含盡可能多的標籤的問題。因此,結果可能會出現3場比賽,2場比賽或1場比賽的問題。 目前,我創建了一個字段,用空格連接所有這些標籤,並在該字段中進行sphinx搜索。但這聽起來很愚蠢,在添加和刪除標籤時會產生大量開銷。會有更聰明的方式,對吧?
沙发
+40
+50

Take a look at MVA - Multi Value Attributes, in the documentation MVA and sql_attr_multi.

I have used this in the past to search "interests" against a "person". So I may like rock music and watching rugby. Sphinx can index this in an array and you can search them using OR or AND matching.

0

Easiest should be to just join the tables and group by the question ID. Sphinx does the rest for you:

source src_questions{
  select question_id, question_subject, question_body, tag_value from question 
  JOIN question_tag on question.question_id = question_tag.question_id 
  JOIN tag on question_tag.tag_id = tag.tag_id
  GROUP BY question_id 
}

that assumes, that you've got the following columns in your tables

question table:

question_id INT
question_subject VARCHAR or TEXT
question_body VARCHAR or TEXT

tag table:

tag_id INT
tag_value VARCHAR or TEXT

question_tag_table:

question_id INT
tag_id INT

由於這是一個很多的案例,你將為同樣的問題創建許多記錄。另一個更嚴重的問題是,如果問題符合您搜索階段的多個關鍵字,則評分仍然很低。 - Yau Leung 2010年11月3日6:12

4
votes
answers
35 views
+10

Sphinx可以配置為對某些字段賦予更多權重嗎?

嘿,所以我想知道Sphinx是否有可能在結果中對某些文檔字段加權。舉例來說,如果我做了搜索the rock show,是有可能配置獅身人面像給更高的優先級來命名的歌曲the rock show在其中有一首歌the rock show在它的歌詞?
沙发
+20
+50

謝謝。我將一個字段的權重設置為4,000,000,將另一個字段設置為1,但它似乎對結果的順序沒有任何影響。唯一似乎有所作為的是我將MatchMode設置為,並且似乎Sphinx一直忽略了重量。 - 邁克2010年10月9日19:16

+20

This question is pretty old but here is a good article from the Sphinx blog on matching and weighting in Sphinx:

» How Sphinx relevance ranking works

感謝您的鏈接,欣賞它! - Agent47DarkSoul 2013年8月22日9:37