Class: Hearch

Inherits:
Object
  • Object
show all
Defined in:
lib/Hearch.rb

Instance Method Summary collapse

Constructor Details

#initializeHearch



12
13
14
# File 'lib/Hearch.rb', line 12

def initialize
    @index = nil #索引对象。
end

Instance Method Details

#addArticle(articleContentString, articleId) ⇒ Object

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)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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 #关键字相同。
                currentEntry.articleId << articleId #将文章编号加入到文章编号列表中。
                
                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) ⇒ Object

分词获取关键字列表。



26
27
28
29
30
31
32
33
34
# File 'lib/Hearch.rb', line 26

def getKeywordList(keywordsString)
    resultList=[] #结果列表。
    
    resultList=segment(keywordsString)
    
#         resultList << keywordsString #最简单实现,直接将整个字符串加入。
    
    resultList #返回结果。
end

#initializeIndexObject

Initialize an empty index.

Example:

>> hearch=Hearch.new
>> hearch.initializeIndex


21
22
23
# File 'lib/Hearch.rb', line 21

def initializeIndex
    @index=Com::Stupidbeauty::Hearch::HearchIndex.new #索引库对象。
end

#loadIndex(indexFileName) ⇒ Object

Load index.

Example:

>> hearch=Hearch.new
>> hearch.loadIndex('indexFile.pb')

Arguments:

indexFileName: (String)


43
44
45
46
47
48
# File 'lib/Hearch.rb', line 43

def loadIndex(indexFileName)
    fileContent=File.read(indexFileName) #读取文件内容。
    
    #解析protobuf:
    @index=Com::Stupidbeauty::Hearch::HearchIndex.decode(fileContent) #protobuf解码。
end

#saveIndex(indexFileName) ⇒ Object

Save index.

Example:

>> hearch=Hearch.new
>> hearch.saveIndex('indexFile.pb')

Arguments:

indexFileName: (String)


57
58
59
60
61
62
63
64
# File 'lib/Hearch.rb', line 57

def saveIndex(indexFileName)
    manufacturerListEncoded="" #初始化要存储的缓冲区。
    manufacturerListEncoded=Com::Stupidbeauty::Hearch::HearchIndex.encode(@index) #重新编码。
  
    manufacturerListFile=File.open(indexFileName, 'w') #打开本地记录文件。
    manufacturerListFile.write(manufacturerListEncoded) #写入到文件中。
    manufacturerListFile.close #关闭文件。
end

#search(keywordsString) ⇒ Object

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)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/Hearch.rb', line 75

def search(keywordsString)
    resultList=[] #结果列表。
    keywordList=getKeywordList(keywordsString) #分词获取关键字列表。
    
    @index.entry.each do |currentEntry| #一个个条目地比较。
        keywordList.each do |currentKeyword| #一个个关键字地比较。
#                 puts "Current keyword: ", currentKeyword, ", current entry keyword: ", currentEntry.keyword #Debug.
            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