‹›外部服务就赫伯特·乔治·威尔斯(H.G. Well)的小说《世界之战》中某语境中的名词创建图
连接到 OpenLibrary API 来获取小说《世界之战》的文字.
ol = ServiceConnect["OpenLibrary"];
text = ol["BookText",
"BibKeys" -> {{\[ThinSpace]"OLID", "OL7211749M"}\[ThinSpace]}];
我们可以创建一个 WordCloud 来可视化与火星入侵者相关的单词.
显示完整的 Wolfram 语言输入
sentences = TextSentences[text[1]];
preresult =
DeleteCases[
Function[s,
Module[{sentence = DeleteStopwords[TextWords[ToLowerCase[s]]]},
If[MemberQ[sentence, "martian" | "martians"], sentence]
]] /@ sentences, Null];
WordCloud[Tally[preresult]]
并且就这段内容中的名词创建图.
显示完整的 Wolfram 语言输入
edges = Function[u,
Module[{words =
Pick[u, MemberQ[#,
Entity["GrammaticalUnit", "Noun"]] & /@ (PartOfSpeech /@
u), True], basicedges, martianedges},
basicedges = UndirectedEdge @@@ Partition[words, 2, 1];
basicedges
]] /@ preresult;
Graph[Flatten[edges], VertexStyle -> RGBColor[0.6, 0.12, 0.],
EdgeStyle -> RGBColor[0.84, 0.04, 0.]]