text-segmentation
Replace a whole line where a particular word is found in a text file
One approach that you can use on smaller files that can fit into your memory twice: $data = file(‘myfile’); // reads an array of lines function replace_a_line($data) { if (stristr($data, ‘certain word’)) { return “replacement line!\n”; } return $data; } $data = array_map(‘replace_a_line’, $data); file_put_contents(‘myfile’, $data); A quick note, PHP > 5.3.0 supports lambda functions … Read more
Haskell file reading
Not a bad start! The only thing to remember is that pure function application should use let instead of the binding <-. import System.IO import Control.Monad main = do let list = [] handle <- openFile “test.txt” ReadMode contents <- hGetContents handle let singlewords = words contents list = f singlewords print list hClose handle … Read more
Python: Cut off the last word of a sentence?
Actually you don’t need to split all words. You can split your text by last space symbol into two parts using rsplit. Example: >>> text=”Python: Cut off the last word of a sentence?” >>> text.rsplit(‘ ‘, 1)[0] ‘Python: Cut off the last word of a’ rsplit is a shorthand for “reverse split”, and unlike regular … Read more
Android Word-Wrap EditText text
Besides finding the source of the issue, I found the solution. If android:inputType is used, then textMultiLine must be used to enable multi-line support. Also, using inputType supersedes the code android:singleLine=”false”. If using inputType, then, to reiterate, textMultiLine must be used or the EditText object will only consist of one line, without word-wrapping. Edit: Thank … Read more
Converting a String to a List of Words?
I think this is the simplest way for anyone else stumbling on this post given the late response: >>> string = ‘This is a string, with words!’ >>> string.split() [‘This’, ‘is’, ‘a’, ‘string,’, ‘with’, ‘words!’]
How to get the first word of a sentence in PHP?
There is a string function (strtok) which can be used to split a string into smaller strings (tokens) based on some separator(s). For the purposes of this thread, the first word (defined as anything before the first space character) of Test me more can be obtained by tokenizing the string on the space character. <?php … Read more
How do I split a string into a list of words?
Given a string sentence, this stores each word in a list called words: words = sentence.split()