class Hearch
Public Class Methods
new()
click to toggle source
# File lib/Hearch.rb, line 13 def initialize @index = nil #索引对象。 end
Public Instance Methods
addArticle(articleContentString, articleId)
click to toggle source
Add an article into the index.
Example:
>> hearch=Hearch.new >> hearch.loadIndex('indexFile.pb') >> articleContentString='something to search' >> hearch.addArticle(articleContentString, 1)
Arguments:
articleContentString: (String) articleId: (int)
# File lib/Hearch.rb, line 103 def addArticle(articleContentString, articleId) keywordList=getKeywordList(articleContentString) #分词获取关键字列表。 #遍历关键字列表: keywordList.each do |currentKeyword| #一个个关键字地添加。 existEntry=false #是否命中了已有条目。 @index.entry.each do |currentEntry| #一个个条目地比较。 if currentEntry.keyword==currentKeyword #关键字相同。 articleIdSet=Set.new #文章编号集合。 articleIdSet=currentEntry.articleId.to_set #将文章编号列表转换成文章编号集合。 articleIdSet << articleId #加入文章编号。 currentEntry.articleId=Google::Protobuf::RepeatedField.new( :int32 , []) #初始化文章编号列表。 currentEntry.articleId+=articleIdSet.to_a #重新转换成文章编号列表。 existEntry=true #是命中了已有条目。 break #跳出。 end #if currentEntry.keyword==currentKeyword #关键字相同。 end #@index.entry.each do |currentEntry| #一个个条目地比较。 if (existEntry) #命中了已有条目。 else #未命中已有条目。 currentEntry=Com::Stupidbeauty::Hearch::HearchIndexEntry.new #索引库条目对象。 currentEntry.keyword=currentKeyword #设置关键字。 currentEntry.articleId << articleId #将文章编号加入到文章编号列表中。 @index.entry << currentEntry #加入条目列表中。 end #if (existEntry) #命中了已有条目。 end #keywordList.each do |currentKeyword| #一个个关键字地比较。 end
getKeywordList(keywordsString)
click to toggle source
分词获取关键字列表。
# File lib/Hearch.rb, line 27 def getKeywordList(keywordsString) resultList=[] #结果列表。 resultList=segment(keywordsString) # resultList << keywordsString #最简单实现,直接将整个字符串加入。 resultList #返回结果。 end
initializeIndex()
click to toggle source
Initialize an empty index.
Example:
>> hearch=Hearch.new >> hearch.initializeIndex
# File lib/Hearch.rb, line 22 def initializeIndex @index=Com::Stupidbeauty::Hearch::HearchIndex.new #索引库对象。 end
loadIndex(indexFileName)
click to toggle source
Load index.
Example:
>> hearch=Hearch.new >> hearch.loadIndex('indexFile.pb')
Arguments:
indexFileName: (String)
# File lib/Hearch.rb, line 44 def loadIndex(indexFileName) fileContent=File.read(indexFileName) #读取文件内容。 #解析protobuf: @index=Com::Stupidbeauty::Hearch::HearchIndex.decode(fileContent) #protobuf解码。 end
saveIndex(indexFileName)
click to toggle source
Save index.
Example:
>> hearch=Hearch.new >> hearch.saveIndex('indexFile.pb')
Arguments:
indexFileName: (String)
# File lib/Hearch.rb, line 58 def saveIndex(indexFileName) manufacturerListEncoded="" #初始化要存储的缓冲区。 manufacturerListEncoded=Com::Stupidbeauty::Hearch::HearchIndex.encode(@index) #重新编码。 manufacturerListFile=File.open(indexFileName, 'w') #打开本地记录文件。 manufacturerListFile.write(manufacturerListEncoded) #写入到文件中。 manufacturerListFile.close #关闭文件。 end
search(keywordsString)
click to toggle source
Search for articles in the index.
Example:
>> hearch=Hearch.new >> hearch.loadIndex('indexFile.pb') >> keywordString='something to search' >> resultList=hearch.search(keywordsString)
Arguments:
keywordsString: (String)
# File lib/Hearch.rb, line 76 def search(keywordsString) resultList=[] #结果列表。 keywordList=getKeywordList(keywordsString) #分词获取关键字列表。 @index.entry.each do |currentEntry| #一个个条目地比较。 keywordList.each do |currentKeyword| #一个个关键字地比较。 if currentEntry.keyword==currentKeyword #关键字相同。 resultList << currentEntry.articleId #将文章编号加入到结果列表中。 break #跳出。 end #if currentEntry.keyword==currentKeyword #关键字相同。 end #keywordList.each do |currentKeyword| #一个个关键字地比较。 end #@index.entry.each do |currentEntry| #一个个条目地比较。 resultList #返回结果列表。 end