2
$\begingroup$

How might I make an association out of web-derived data imported with SemanticImport?

knownRatios = Association[Flatten[StringCases[Flatten[SemanticImport[ "http://www.huygens-fokker.org/docs/intervals.html",Automatic,"Rows",ExcludedLines->{Append[Range[10],Range[519,539]]}, HeaderLines->0]],StartOfLine|WordBoundary~~a:NumberString~~ "/"~~b:NumberString~~Whitespace|WordBoundary~~c:___~~EndOfLine|"\n" ->  a~~"/"~~b~~" "~~"->"~~" "~~"\""~~c~~"\""]]]

I have also attempted these, to no avail:

Association @@ knownRatios

Association @@@ knownRatios

I appreciate your help, and thank you all in advance...

F.

$\endgroup$
3
  • $\begingroup$ The problem is that each element is a string, not a rule. Take a look at the InputForm. "1/1 -> \"unison, perfect prime\"" $\endgroup$ Commented Jun 21, 2019 at 19:08
  • $\begingroup$ Awesome and thank you for responding! I added an 'InputForm before the Association[], and IT WORKED!!! $\endgroup$ Commented Jun 21, 2019 at 19:23
  • $\begingroup$ Make this an answer, and the prize is yours, my friend! THANK YOU! $\endgroup$ Commented Jun 21, 2019 at 19:23

2 Answers 2

3
$\begingroup$

Expanding on the comment.

ToExpression /@ 
  Flatten@StringCases[
    Flatten[SemanticImport[
      "http://www.huygens-fokker.org/docs/intervals.html", Automatic, 
      "Rows", ExcludedLines -> {Append[Range[10], Range[519, 539]]}, 
      HeaderLines -> 0]], 
    StartOfLine | WordBoundary ~~ a : NumberString ~~ "/" ~~ 
      b : NumberString ~~ Whitespace | WordBoundary ~~ c : ___ ~~ 
      EndOfLine | "\n" -> 
     a ~~ "/" ~~ b ~~ " " ~~ "->" ~~ " " ~~ "\"" ~~ c ~~ 
      "\""] // Association

<|1 -> "unison, perfect prime", 2 -> "octave", 3/2 -> "perfect fifth", ... |>

$\endgroup$
1
  • $\begingroup$ Your answer not only works, it is aesthetically pleasing to the eye and is highly readable. Thank you. $\endgroup$ Commented Jun 22, 2019 at 17:22
3
$\begingroup$

This might give you what you want.

ClearAll[ratioFile,knownRatios]
ratioFile=SemanticImport["http://www.huygens-fokker.org/docs/intervals.html"];
knownRatios=Association[Flatten[StringCases[#, a:NumberString ~~ "/" ~~ b:NumberString ~~ 
  Whitespace | WordBoundary ~~ c:___ ~~ EndOfString -> {a ~~ "/" ~~ b -> c}]& /@ Select[Flatten[Values[Normal[ratioFile]]], StringLength[#] > 1 && ContainsAny[{StringTake[#, 1]}, ToString[#] & /@ Range[9]] &]]];

There are probably ways of combining my select statement with the pattern (taken mostly from yours).

$\endgroup$
1
  • $\begingroup$ My friend, I thank you for your help! $\endgroup$ Commented Jun 22, 2019 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.