Evenly distribute wrapped text between lines in SwiftUI - swiftui

How can I tell SwiftUI that when text wraps, I'd like all the lines to be as close to equal length as possible?
For example, I don't want this:
The quick brown fox jumps over the
lazy dog
Even if there is enough horizontal space to fit everything except "lazy dog" on the first line, I want this instead (or whatever gives the most equal line lengths for the font in use):
The quick brown fox
jumps over the lazy dog

Related

Force SkParagraph layout to account for ghost whitespace

SkParagraph automatically compensates for "ghostwhitespace" when shaping a paragraph. I'd like to disable this behaviour and allow the line to pushed out when whitespace is introduced.
Center alignment with current behaviour:
The quick brown fox
๐ŸฆŠ ate a zesty
hamburgerfons ๐Ÿ”.
The ๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง laughed.
Now adding loads of spaces after zesty: (desired behaviour)
The quick brown fox
๐ŸฆŠ ate a zesty
hamburgerfons ๐Ÿ”.
The ๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง laughed.
Notice second line pushed to the left due to all the extra whitespace.
I've modified this CanvasKit fiddle to illustrate. See line 40.
I've also found this flutter issue that illustrates the issue.
I've gone through the Skia / SkParagraph source code many times over and can't find a way to introduce the behaviour I need.

Replace list of value in a text file

I have the following problem:
I have a huge file and i have to replace some value (more than one).
Ad example, I have to replace:
DOG with RED
CAT with BLUE
FISH with GREEN
...
...
n with N
Do you know some software that is able (putting in input a list value) to replace all the value of the list in one hit in the text?
EDIT:
My text file is something really big as a book or similar.
In this book i have many words that i have to replace with other words
You can use sed to substitute matching expressions, for example
sed -e 's/DOG/RED/g;s/CAT/BLUE/g' < inputFile > outputFile
You haven't specified if you wan this changed in place or not. You could clearly delete the old version afterwards if you were happy with the results.
.....
If you are on Windows some other answers will give you an equivalent or suggest tools such as cygwin: e.g. here

Generating sentences with NLTK and Python

I'm having trouble using NLTK to generate random sentences from a custom corpus.
Before I start, I'd like to mention that I'm using NLTK version 2x, so the "generate" function is still existent.
Here is my current code:
file = open('corpus/romeo and juliet.txt','r')
words = file.read()
tokens = nltk.word_tokenize(words)
text = nltk.Text(tokens)
print text.generate(length=10)
This runs, but does not create random sentences (I'm going for a horse_ebooks vibe). Instead, it returns me the first 10 words of my corpus source every time.
However, if I use NLTK's brown corpus, I get the desired random effect.
text = nltk.Text(nltk.corpus.brown.words())
print text.generate(length=10)
Going into the Brown corpus files, it seems as though every word is separated and tagged with verbs, adjectives, etc - something I thought would be completed with the word_tokenize function of my first block of code.
Is there a way to generate a corpus like the Brown example - even if it means converting my txt documents into that fashion instead of reading them directly?
Any help would be appreciated - any documents on this are either horribly outdated or just say to use Markov Chains (which I have, but I want to figure this out!) I understand generate() was removed as of NLTK 3.0 because of bugs.

Regular Expression - Matching and extracting complicated conditions

I'm trying to write a regular expression that will match these conditions:
Maximum of 8000 characters (any characters, including "\r\n")
Maximum of 10 lines (separated by \r\n).
to extract from the matched text only the first 4 lines.
Can't find a good way do it...:/
Thanks!!
Regular expressions are not what you need. They are used to match a certain pattern, not a certain length. If you are holding the data in a string, myString.length <= 8000 is all you need for the character count (using the correct syntax for your language, of course). For the number of lines, you will have to count the number of \r\n sequences in your string (can be done iteratively). To get the first four lines, just find the 4th \r\n and get everything before that with a substring method.
Description
This expression does the following:
validates the input string is between zero and 8,000 characters
validates there are at most 10 line of new line delimited text
then captures the first 4 new line delimited lines of text
\A(?=.{0,8000}\Z)(?=(?:^.*?(?:\r|\n|\Z)){0,10}\Z)(?:^.*?[\r\n\Z]+){0,4} This requires options: m multiline, and s dot matches all characters
Expanded
\A anchor to the begining of the string, this anchor allows the use of the s option which allows the . to match new line and line feed characters
(?=.{0,8000}\Z) look ahead and validate there are between zero and 8000 characters
(?=(?:^.*?(?:\r|\n|\Z)){0,10}\Z) look ahead and validate there are no more then 10 new line delimited lines
(?:^.*?[\r\n\Z]+){0,4} match the first 4 lines of text
PHP Code Example:
You didn't specify a language so I'm including this PHP example to show how it works and the sample output.
Input Text
This input test is 8 lines of new line delimited strings. There are only 1779 characters here.
Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small
river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about
the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were
thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didnโ€™t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of
the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then
she continued her way. On her way she met a copy. The copy warned the Little Blind Text, that where it came from it would have been rewritten a thousand times and everything that was left from its origin would be the word "and"
and the Little Blind Text should turn around and return to its own, safe country. But nothing the copy said could convince her and so it didnโ€™t take long until a few insidious Copy Writers ambushed her, made her drunk with Longe
and Parole and dragged her into their agency, where they abused her for their projects again and again. And if she hasnโ€™t been rewritten, then they are still using her.
Code
<?php
$sourcestring="your source string";
preg_match('/\A(?=.{0,8000}\Z)(?=(?:^.*?(?:\r|\n|\Z)){0,10}\Z)(?:^.*?[\r|\n\Z]+){0,4}/ims',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
Matches
$matches Array:
(
[0] => Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small
river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about
the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were
thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didnโ€™t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of
)

What is a permuted index?

I am reading Accelerated C++. I don't understand Exercise 5-1:
Design and implement a program to produce a permuted index from the following input. A permuted index is one in which each phrase is indexed by every word in the phrase.
The quick brown fox
jumped over the fence
The quick brown fox
jumped over the fence
jumped over the fence
The quick brown fox
jumped over the fence
The quick brown fox
That explanation isn't clear to me. What exactly is a permuted index?
The term permuted index is another name for a KWIC index, referring to the fact that it indexes all cyclic permutations of the headings. Books composed of many short sections with their own descriptive headings, most notably collections of manual pages, often ended with a permuted index section, allowing the reader to easily find a section by any word from its heading. This practice is no longer common.
From: http://en.wikipedia.org/wiki/Key_Word_in_Context
ps: you can access wikipedia via http://www.proxify.com
You can find a 'live' example of a permuted index in the 7th Edition UNIXโ„ข Programmer's Reference Manual, Vol 1 (dating back to 1979). A fragment of it (from the PDF files) is:
If you look for 'account', you can find a number of related entries together. You probably wouldn't think to look for sa(1) as well as ac(1), not to mention acct(2) or acct(5) unless they were grouped together. This is the benefit of a permuted index; you can look up the key word and see it in a bigger context.
You could also look at the man page entry for the ptx(1) command in the same 7th Edition manual.
Permuted index is an alphabetic list of index surrounded by its context. In the output, observe the bold words. They are alphabetically sorted and are surrounded by its context. This makes it easy for us to search a word and directly infer its usage from the surrounding context i.e. words in your case.
The quick brown fox
jumped over the fence
The quick brown fox
jumped over the fence
jumped over the fence
The quick brown fox
jumped over the fence
The quick brown fox