Finding the three longest substrings in a string using SPARQL on the Wikidata Query Service, and ranking them across strings - substring

I'm trying to identify the longest three substrings from a string using SPARQL and the Wikidata Query Service and then rank
the substrings within a string by length
the strings by the lengths of any of those longest substrings .
I managed to identify the first and second substring from a string and could of course just create similar additional lines to tackle the problem, but this seems ugly and inefficient, so I am wondering if anyone here knows of a better way to get there.
This is a simplified version of the code, though I have left some auxiliary variables in that I am using for tracking progress on the way. You can try it here.
Clarification in response to this comment: if it is necessary to treat this query as a subquery and to feed it with results from another subquery, that's fine with me. To get an idea of the kinds of use I have in mind, see this demo.
SELECT * WHERE {
{
VALUES (?title) {
("What are the longest three words in this string?")
("A really complicated title")
("OneWordTitleInCamelCase")
("Thanks for your help!")
}
}
BIND(STRLEN(REPLACE(?title, " ", "")) AS ?titlelength)
BIND(STRBEFORE(?title, " ") AS ?substring1)
BIND(STRLEN(REPLACE(?substring1, " ", "")) AS ?substring1length)
BIND(STRAFTER(?title, " ") AS ?postfix)
BIND(STRLEN(REPLACE(?postfix, " ", "")) AS ?postfixlength)
BIND(STRBEFORE(?postfix, " ") AS ?substring2)
BIND(STRLEN(REPLACE(?substring2, " ", "")) AS ?substring2length)
}
ORDER BY DESC(?substring1length)
Expected results:
longsubstring substringlength
OneWordTitleInCamelCase 23
complicated 11
longest 7
really 6
string 6
Thanks 6
title 5
three 5
your 4
help 4
Actual results:
title titlelength substring1 substring1length postfix postfixlength substring2 substring2length
Thanks for your help! 18 Thanks 6 for your help! 12 for 3
What are the longest three words in this string? 40 What 4 are the longest three words in this string? 36 are 3
A really complicated title 23 A 1 really complicated title 22 really 6
OneWordTitleInCamelCase 23 0 0 0

Related

To extract numbers after a particular string

I want to extract a number that follows a specific string ':' and write a code that adds that number. I think.. can split it by space and extract it from it... Well, it doesn't work.
1.(12321 6,80.0:3 210.1:3!!!73 540.2:1++ 96.3:3!<<<<%% 689.4:3 24.5:4)
I want to extract the number 3 3 1 3 3 3 4 followed by ":" from this string and find out that the sum is 17.
import re
var1 = '1.(12321 6,80.0:3 210.1:3!!!73 540.2:1++ 96.3:3!<<<<%% 689.4:3 24.5:4)'
item = var1.split(" ")
sum([int(i) for i in re.findall('(?<=:)\\d+',var1)])
17

Why are my printed outputs different?

I've been slowly building up skillsets in Swift. Drawing with loops is a great way I find, to understand the subtleties of the language.
Here is an interesting puzzle I can't quite figure out:
I've been trying to generate a truncated pyramid like this for a little while.
I finally got a rough one produced using a for loop. screenshot here BUT, as you can see, one of my earlier attempts generated a half truncated pyramid.
The only difference between the two is that on lines 19 and 33, the variable "negativeSpaceThree" is diminished by 2 and 1 respectively.
Can anyone explain why the outputs are so different? I'd really like to understand these nuances. It might simply be my math, but I'm wondering if its a bug.
Many thanks for any input offered.
Code added below:
let space = " "
var negativeSpaceTwo = 22
var xTwo = 3
for circumTwo in 1...11{
xTwo += 2
negativeSpaceTwo -= 2
print(String(repeating: "-", count: negativeSpaceTwo) , String(repeating:"*", count: xTwo ))
}
print(space)
print(space)
var negativeSpaceThree = 11
var xThree = 3
for circumTwo in 1...11{
xThree += 2
negativeSpaceThree -= 1
print(String(repeating: "-", count: negativeSpaceThree) , String(repeating:"*", count: xThree ))
}
It's because of the difference in how many * characters you are printing on each line. If your total line length is total = dashes + stars and you subtract 2 from dashes each time while adding 2 to stars each time, the total line length will remain the same.
In the second pyramid, you reduce the dashes by one, but add 2 to stars, so the total length increases by 1 each line, giving the pyramid effect on the right-hand side of the text.

How to get the number of real words in a text in Swift [duplicate]

This question already has answers here:
Number of words in a Swift String for word count calculation
(7 answers)
Closed 5 years ago.
Edit: there is already a question similar to this one but it's for numbers separated by a specific character (Get no. Of words in swift for average calculator). Instead this question is about to get the number of real words in a text, separated in various ways: a line break, some line breaks, a space, more than a space etc.
I would like to get the number of words in a string with Swift 3.
I'm using this code but I get imprecise result because the number is get counting the spaces and new lines instead of the effective number of words.
let str = "Architects and city planners,are \ndesigning buildings to create a better quality of life in our urban areas."
// 18 words, 21 spaces, 2 lines
let components = str.components(separatedBy: .whitespacesAndNewlines)
let a = components.count
print(a)
// 23 instead of 18
Consecutive spaces and newlines aren't coalesced into one generic whitespace region, so you're simply getting a bunch of empty "words" between successive whitespace characters. Get rid of this by filtering out empty strings:
let components = str.components(separatedBy: .whitespacesAndNewlines)
let words = components.filter { !$0.isEmpty }
print(words.count) // 17
The above will print 17 because you haven't included , as a separation character, so the string "planners,are" is treated as one word.
You can break that string up as well by adding punctuation characters to the set of separators like so:
let chararacterSet = CharacterSet.whitespacesAndNewlines.union(.punctuationCharacters)
let components = str.components(separatedBy: chararacterSet)
let words = components.filter { !$0.isEmpty }
print(words.count) // 18
Now you'll see a count of 18 like you expect.

How can I take a user input that may contain spaces and convert the spaces to a hyphen in Swift? [duplicate]

This question already has answers here:
Any way to replace characters on Swift String?
(23 answers)
Closed 7 years ago.
I'm trying to create a simple iOS app that takes user input ( a city ) and searches a website for that city, and then will display the forecasts for that city.
What I'm currently stuck on and unable to find much documentation that isn't overwhelming is how I can be sure that the user input will translate well to a URL if there are more then one words in the name of the city.
aka if a user inputs Salt Lake City into my text field, how can I write an if else statement that determines the amount of spaces, and if the amount of spaces is greater than 0 will convert those spaces to "-".
So far I've tried creating an array out of the string, but still can't figure out how I can append a - to each element in the array. I don't think it's possible.
Does anyone know how I can do what I'm trying to do? Or am I approaching it the incorrect way?
Here's a poor first attempt. I know this doesn't work, but hopefully it explains it a bit more of what I'm trying to accomplish than my text above.
var cityText = "Salt Lake City"
let cityArray = cityText.componentsSeparatedByString(" ")
let combineDashUrl = cityArray[0] + "-" + cityArray[1] + "-" + cityArray[2]
print(combineDashUrl)
Assuming there are never multiple spaces in a row you should be able to use stringByReplacingOccurrencesOfString.
let cityText = "Salt Lake City"
let newCityText = cityText.stringByReplacingOccurrencesOfString(
" ",
withString: "-")
Replacing variable numbers of spaces with a dash would be more complicated. I'd probably use regular expressions for that.
You can use map over the array of characters to transform spaces into hyphens.
let city = "Salt Lake City"
let hyphenatedCity = String(city.characters.map{$0 == " " ? "-" : $0})

Applescript: return specific index positions from a date string

I have already used the text delimiters and item numbers to extract a date from a file name, so I'm clear about how to use these. Unfortunately the date on these particular files are formatted as "yyyyMMdd" and I need to covert the date into format "yyyy-MM-dd". I have been trying to use the offset function to get particular index positions, and I have found several examples of how you would return the offset of particular digits in the string, example:
set theposition to offset of 10 in theString -- this works
(which could return 5 or 7) but I have not found examples of how to call the digits at a specific index:
set _day to offset 7 of file_date_raw -- error
"Finder got an error: Some parameter is missing for offset." number -1701
How would you do this, or is there a totally better way I'm unaware of?
To "call the digits at a specific index", you use:
text 1 thru 4 of myString
If you know that each string has 8 characters in the yyyymmdd format, then you don't need to use 'offset' or any parsing, just add in the -'s, using text x thru y to dissect the string.
set d to "20011018"
set newString to (text 1 thru 4 of d) & "-" & (text 5 thru 6 of d) & "-" & (text 7 thru 8 of d)