Wolfram 语言

外部服务

就赫伯特·乔治·威尔斯(H.G. Well)的小说《世界之战》中某语境中的名词创建图

连接到 OpenLibrary API 来获取小说《世界之战》的文字.

In[1]:=
Click for copyable input
ol = ServiceConnect["OpenLibrary"]; text = ol["BookText", "BibKeys" -> {{\[ThinSpace]"OLID", "OL7211749M"}\[ThinSpace]}];

我们可以创建一个 WordCloud 来可视化与火星入侵者相关的单词.

显示完整的 Wolfram 语言输入
In[2]:=
Click for copyable input
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]]
Out[2]=

并且就这段内容中的名词创建图.

显示完整的 Wolfram 语言输入
In[3]:=
Click for copyable input
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.]]
Out[3]=

相关范例

de en es fr pt-br ru