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$
  • $\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$ – Rohit Namjoshi Jun 21 at 19:08
  • $\begingroup$ Awesome and thank you for responding! I added an 'InputForm before the Association[], and IT WORKED!!! $\endgroup$ – Francis from ResponseBase Jun 21 at 19:23
  • $\begingroup$ Make this an answer, and the prize is yours, my friend! THANK YOU! $\endgroup$ – Francis from ResponseBase Jun 21 at 19:23
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$
  • $\begingroup$ Your answer not only works, it is aesthetically pleasing to the eye and is highly readable. Thank you. $\endgroup$ – Francis from ResponseBase Jun 22 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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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