当前位置: 首页>>代码示例>>Python>>正文


Python Search.search_terms方法代码示例

本文整理汇总了Python中search.Search.search_terms方法的典型用法代码示例。如果您正苦于以下问题:Python Search.search_terms方法的具体用法?Python Search.search_terms怎么用?Python Search.search_terms使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在search.Search的用法示例。


在下文中一共展示了Search.search_terms方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: load

# 需要导入模块: from search import Search [as 别名]
# 或者: from search.Search import search_terms [as 别名]
	def load(self, filename):
		# Load the anki deck and get all the sentences
		deck = anki.DeckStorage.Deck(filename)
		
		# Grab the sentences based on which field in each model is the
		# sentence.
		
		# Cycle through models of the deck
		for model in deck.models:
			
			# First check if we know which field to use in this model
			if model.name in self.sentence_field:
				# Grab all facts ids
				fact_ids = deck.s.all("select id from facts where modelId = " + str(model.id))
				
				# Get facts from the id's
				facts = []
				for fact_id in fact_ids:
					facts.append(deck.s.query(Fact).get(fact_id[0]))
				
				# Get the sentences from the facts, and media.
				sentences = []
				parser = ankiParse()
				for fact in facts:
					# The new sentence
					newSentence = Sentence(sentence=fact[self.sentence_field[model.name]])
					media = [] # Temperory media without full path
					full_media = [] # Media with full path
					
					# Loop through fields in fact searching for media
					for field in model.fieldModels:
						parser.feed(fact[field.name])
						media.extend(parser.media)
						parser.close()
					
					# If this anki deck has a media directory add that to media path
					if deck.mediaDir() != None:					
						# Add full path to media
						del full_media[:]
						for m in media:
							#print os.path.join(deck.mediaDir(), m)
							if deck.mediaDir():
								path = deck.mediaDir()
							else:
								path = ""
							
							full_media.append(os.path.join(path, m))
							
						newSentence.media = full_media
					
					# Sentence Language
					newSentence.language = self.language
					
					# Remove tags that we don't want (anki specific tags, etc)
					tags = fact.tags
					for tag in self.tag_strip:
						tags = tags.strip(tag)
					
					newSentence.tags = tags.split()

					# Append the sentence	
					sentences.append(newSentence)
					
				
				# Ok, now if there are search terms search, search the
				# sentences.
				if len(self.search_terms) > 0:
					# Make a searcher
					searcher = Search()
					searcher.search_terms = self.search_terms
				
					# Put the sentences into the searcher
					for sentence in sentences:
						searcher.raw_sentences.append(sentence.sentence)
										
					# Search
					searcher.search()
					
					# Now update sentences to contain the results
					for sentence in sentences:
						if sentence.sentence not in searcher.sentences:
							sentences.remove(sentence)
					
				#get rid of all sentences that don't have these tags
				if len(self.search_tags) > 0:
					for sentence in sentences[:]:
						removed = False
						for tag in self.search_tags:
							if tag not in sentence.tags and not removed:
								sentences.remove(sentence)
								removed = True
					
				# Before we add these sentences, we need to strip any
				# xml / html from them
				i = 0
				for sentence in sentences:
					print sentence.media
					sentences[i].sentence = strip_tags(sentence.sentence)
					i += 1
					
#.........这里部分代码省略.........
开发者ID:bombpersons,项目名称:ankiResourceImport,代码行数:103,代码来源:ankiUpload.py


注:本文中的search.Search.search_terms方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。