How to create a Google Like Text Snippet from String in Python? -


I'm trying to create something similar to Google's text snippet. . For example:

For example: Google snippets highlighted keywords and "changes" text well analyzed a keyword in case it does not seem right at the beginning of the string

< Strong> keyword "Nike"

Harestek String "Laurem Epsom Doror Lorum Epsom Doror Laurum Android Darrel Laurem Ipsum Doror Lorum Izum Doror Lorum Darl Laurem Izham Doror Lorum Darl Laurem Ipsum Doror Lorum Epsom Dodger L'Lorem ipsum Dorlr Lorm ipsum Doror Lorm Android Doror Lorm Darrl Lorem Ijhm Doror Lorm Darrl Lorm Darol Lorm Darrl Lorm Darol Lorm Darrl Lorm Android Darol Lorm Darlr Lorm Android Darol Lorm Darrl Lorm ipsum Dorrl Lorem ipsum Doral Lorem ipsum Dorlr is no wonder that Nike is one of the largest brands in the world, lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dor lor lorem ipsum dorlor lorem ipsum is not this should be dorlor

piece:

... lorem ipsum dorlor strong that it is no wonder < > Nike is one of the biggest brands in the world not lorem ipsum dorlor lorem dorlor lorem ipsum dorlor loremdorlor lorem ipsum dorlor loremipsum dorlor is lorem ipsum dorlor lorem ...

this is what I What is an idea as far as:.

  keywordPosition = haystack.lower () Index (keyword.lower ()) snippetStart = keywordPosition - 100 snippetEnd = keywordPosition + stack 200 straw = "..." + haystack [snippetStart : SnippetEnd] "..."  

Is there a great way to dynamically adjust snippet start and snippet ad in Python? In many cases the above approach is clearly out of the haystrack piece index limit since an exception throws.

I created a small example with you comment here.

  straw stacks = "lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem Ipsum dorlor laser imer dorlor lamps ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor laser dipsor dorlor laser dorlor laser dorlor laser imer dorlor laser dome dorlor laser derm not that Nike is one of the biggest brands in the world lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor lorem ipsum dorlor "needle =" Nike342 "forward-looking = 7 # number of" Nike " By token The show tokens of = Hystak for. Split ( "") # represents # Split string found_index = -1 indicator of # tokens token list starts from 1 and assume that it does not exist. Compare the # loop through token and with each needle. We get the maximum number to find the needle, index rememeber and loop found_index = tokens.index (try to break out of the needle): found_index = tokens.index (needle) # the index show a word minus the needle, and Make a sub list of tokens from found_index = max (found_index - forerunner, 0) # found_index and end, then join the words together with one place back into those words. Piece = "" .join (token [found_index: lane (token)]) except valueError: piece = "#" No snippet or whatever error handling you print piece  
Going to

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -