TextCases
更多信息和选项
- TextCases 被用于执行多种自然语言处理任务,如词性标注或已命名实体识别.
- 在 TextCases[text,…] 中,text 可以是一个字符串,一个由 File[…] 表示的纯文本文件,一个 ContentObject 表达式或这些文本对象的列表.
- TextCases[{text1,text2,…},…] 给出每个 texti 的情况.
- 识别类型 form 可以是:
-
"type" 任何文本内容类型(例如, "Noun"、"City" ) Entity[…,…] 文本内容类型的指定实体 form1|form2|… 匹配任何 formi 的格式 Containing[outer,inner] 包含类型 inner 的类型 outer 格式 Verbatim["string"] 精确匹配的指定字符串 pattern 匹配的字符串模式 - 属性 prop 的可能选择有:
-
"String" (默认的)识别文本的字符串 "Position" text 中字符串的开始和结束位置 "Probability" 估计情况正确的概率 "Interpretation" 识别的字符串的标准诠释 "Snippet" 识别的字符串周围的片段 "HighlightedSnippet" 识别的字符串突出显示的片段 f 把 f 应用于包含所有属性的关联 {prop1,prop2,…} 属性规范列表 - 可以给出以下选项:
-
AcceptanceThreshold Automatic 接受识别的最小概率 PerformanceGoal Automatic 带有特定优势的有利算法 TargetDevice "CPU" CPU 或 GPU 计算是否应该用于实体检测 VerifyInterpretation False 是否应该验证可诠释性 - TextCases 使用机器学习. 其方法、训练集和包含的偏差可能会在不同版本的 Wolfram 语言中发生变化并给出不同的结果.
- TextCases 可能会下载将保存在本地对象存储(位于 $LocalBase)中的资源,可用 LocalObjects[] 列出资源,用 ResourceRemove 将其删除.
范例
打开所有单元 关闭所有单元基本范例 (6)
TextCases["NYC, Los Angeles, and Chicago are the largest cities in the United States of America in 2018.", "City"]TextCases["The quick brown fox jumps over the lazy dog.", "Noun"]TextCases["The shirt cost $50 in America, but only 5€ in Italy.", "CurrencyAmount" -> "Interpretation"]TextCases["NYC, Los Angeles, and Chicago are the largest cities in the United States of America in 2018.", {"City", "Country", "Date"}]TextCases["NYC, Los Angeles, and Chicago are the largest cities in the USA in 2018.", {"City", "Country", "Date"} -> {"String", "Interpretation", "Probability"}]TextCases["NYC, Los Angeles, and Chicago are the largest cities in the USA in 2018.", "Location" -> (#String -> #Interpretation&)]TextCases["I love New York - I ❤ NYC", Entity["City", {"NewYork", "NewYork", "UnitedStates"}]]范围 (5)
单词和句子分割 (2)
找出 ContentObject 中和颜色有关的单词:
doc = TextSearch["ExampleData/Text", "dog"][1]TextCases[doc, "Color"]找到 File 中的量:
file = TextSearch["ExampleData/Text", "dog"][1, "Location"]TextCases[file, "Color"]Alternatives 和 Containing (2)
用 Alternatives 匹配多个类型:
TextCases["John and Mary went to the store.", "Noun" | "Verb"]TextCases["John and Mary went to the store.", "Noun" | "ProperNoun" | "Verb"]TextCases["I have a fairly clear idea of what I will buy at the store. I want shoes, a computer, and a jacket. The computer will be the most expensive, and will cost over $1000.", Containing["Sentence", "CurrencyAmount"]]TextCases["On vacation, I first went to France, then I went to Belgium. The food was amazing in both countries.", Containing["Sentence", "Country"]]结合 Alternatives 和 Containing 来形成高度结构化查询:
TextCases["I have a fairly clear idea of what I will buy at the store. I want shoes, a computer, and a jacket. The computer will be the most expensive, and will cost over $1000. John will like my computer.", Containing["Sentence", "CurrencyAmount" | "ProperNoun"]]返回类型 (1)
TextCases["He spent $50 in Boston on the 11th July. For a shirt that costs 5€ in Rome!", "CurrencyAmount" -> {"String", "Position", "Interpretation"}]TextCases["He spent $50 in Boston on the 11th July. For a shirt that costs 5€ in Rome!", {"CurrencyAmount", "City", "Date"} -> {"String", "Position", "Interpretation"}]显示 Association 中所有可用属性:
TextCases["He spent $50 in Boston on the 11th July. For a shirt that costs 5€ in Rome!", "Date" -> Identity]TextCases["He spent $50 in Boston on the 11th July. For a shirt that costs 5€ in Rome!", {"CurrencyAmount", "City", "Date"} -> Identity]//DatasetTextCases["She took a plane from Toulouse in France to Montreal in Canada", "Location" -> (#String -> #Interpretation&)]选项 (3)
AcceptanceThreshold (1)
TextCases[ExampleData[{"Text", "JFKInaugural"}], {"Country", "Date", "Person"}]通过设置高的 AcceptanceThreshold 可以获取极有可能正确的实体:
TextCases[ExampleData[{"Text", "JFKInaugural"}], {"Country", "Date", "Person"}, "AcceptanceThreshold" -> 0.9]PerformanceGoal (1)
使用 PerformanceGoal->"Speed" 可以帮助加速检测,但是低精度:
AbsoluteTiming@TextCases["My favourite cities are New York and Foix", "City"]AbsoluteTiming@TextCases["My favourite cities are New York and Foix", "City", PerformanceGoal -> "Speed"]VerifyInterpretation (1)
默认情况下,某些实体不能被诠释,可能是因为它们不正确或者还没有在知识库. 在这种情况下,会返回字符串而不是一个诠释:
TextCases["We visited Toulouse and Foix in France.", {"City", "Country"} -> "Interpretation"]使用 VerifyInterpretation 过滤不能被诠释的实体:
TextCases["We visited Toulouse and Foix in Midi-Pyrénées in France.", {"City", "Country"} -> "Interpretation", VerifyInterpretation -> True]应用 (6)
单词和句子分割 (2)
分词保留语法元素,如电子邮件地址,URL 和 Twitter 句柄:
TextCases["His email address is user@domain.com and Twitter handle is @username.", "Word"]TextCases["http://www.wolfram.com is a useful resource for Wolfram Language programmers.", "Word"]//TextElement所有非空白字符都由 "Word" 和 "Punctuation" 形式抓取:
TextCases["Washington D.C. is the capital of the United States. Mr. Anthony A. Williams was the mayor.", "Word" | "Punctuation"]//TextElementTextCases["Washington D.C. is the capital of the United States. Mr. Fox is the CEO of the company. She co-founded the company with Mrs. Smith.", "Sentence"]//TextElement词性 (2)
TextCases["John and Mary went to the new store.", "Noun"]TextCases["John and Mary went to the new store.", "Verb"]TextCases["John and Mary went to the new store.", "Preposition"]alice = ExampleData[{"Text", "AliceInWonderland"}];partsOfSpeech = TextCases[alice, {"Noun", "Verb", "Adjective", "Adverb"}];Grid @Partition[KeyValueMap[Labeled[WordCloud[#2, ImageSize -> 200], #1]&, partsOfSpeech], 2]实体和可诠释对象 (2)
TextCases["On vacation, I first went to France, then I went to Belgium.", "Country"]返回作为 Entity 对象的被诠释的字符串:
TextCases["On vacation, I first went to France, then I went to Belgium.", "Country" -> "Interpretation"]dollarstore = WikipediaData@First@WikipediaSearch["Variety Store"];currencies = TextCases[dollarstore, "CurrencyAmount" -> "Interpretation"]CurrencyConvert[currencies, "Yen"]属性和关系 (4)
TextCases 处理与 TextPosition 和 TextContents 相同的类型,并总是为给定类型标识这些函数的同样子字符串:
TextContents["Boston, Worcester, and Springfield are the largest cities in Massachusetts.", "City"]TextCases["Boston, Worcester, and Springfield are the largest cities in Massachusetts.", "City"]TextPosition["Boston, Worcester, and Springfield are the largest cities in Massachusetts.", "City"]TextCases 是 TextPosition 的广义性:
TextCases["Boston, Worcester, and Springfield are the largest cities in Massachusetts.", "City" -> "Position"]TextPosition["Boston, Worcester, and Springfield are the largest cities in Massachusetts.", {"City", "AdministrativeDivision"}]TextCases["Boston, Worcester, and Springfield are the largest cities in Massachusetts.", {"City", "AdministrativeDivision"} -> "Position"]类似于 TextContents 输出的数据集可以使用 TextCases 获取:
Dataset@TextCases["Boston, Worcester, and Springfield are the largest cities in Massachusetts.", "City" -> Identity]Dataset@TextCases["Boston, Worcester, and Springfield are the largest cities in Massachusetts.", "City" -> Function[<|"City" -> Last[#Interpretation][[1]], "State" -> Last[#Interpretation][[2]], "Country" -> Last[#Interpretation][[3]], "Position" -> #Position, "HighlightedSnippet" -> #HighlightedSnippet|>]]TextSentences 等价于 TextCases[…,"Sentence"]:
TextCases["Hi Michael J. Jordan. I am Michael I. Jordan.", "Sentence"]//TextElementTextSentences["Hi Michael J. Jordan. I am Michael I. Jordan."]//TextElementTextStructure 把文本分割成同样的句子:
TextStructure["Hi Michael J. Jordan. I am Michael I. Jordan.", "PartsOfSpeech"]TextWords 等同于 TextCases[…,"Word"]:
TextCases["As a matter-of-fact, my mother-in-law is in N.Y.C.", "Word"]//TextElementTextWords["As a matter-of-fact, my mother-in-law is in N.Y.C."]//TextElementTextStructure 把文本分割成与 TextCases[…,"Word"|"Punctuation"] 一样的单词和标点符号:
TextCases["As a matter-of-fact, my mother-in-law is in N.Y.C.", "Word" | "Punctuation"]//TextElementTextStructure["As a matter-of-fact, my mother-in-law is in New York.", "PartsOfSpeech"]巧妙范例 (2)
可在地图上标出许多实体(城市、国家等). TextCases 可一次性找出所有这样的实体.
text = WikipediaData["Rice"];Snippet[text, 4]locations = TextCases[text, "LocationEntity" -> "Interpretation", VerifyInterpretation -> True];RandomSample[locations, 10]GeoBubbleChart[Counts[locations]]ReverseSort@Counts[Cases[locations, Entity["GeographicRegion", _]]]ReverseSort@Counts[Cases[locations, Entity["Country", _]]]text = WikipediaData["World War"];Snippet[text, 4]查找所有包含日期的句子,并提取其对应的 DateObject 解释:
sentences = TextCases[text, "Date" -> {"HighlightedSnippet", "Interpretation"}, VerifyInterpretation -> True];RandomSample[sentences, 3]TimelinePlot[sentences[[All, 2]]]TimelinePlot[Association[Rule@@@sentences], Sequence[PlotRange -> {"1900", "1950"}, PlotLayout -> "Vertical", Background -> LightBlue]]文本
Wolfram Research (2015),TextCases,Wolfram 语言函数,https://reference.wolfram.com/language/ref/TextCases.html (更新于 2019 年).
CMS
Wolfram 语言. 2015. "TextCases." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2019. https://reference.wolfram.com/language/ref/TextCases.html.
APA
Wolfram 语言. (2015). TextCases. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/TextCases.html 年
BibTeX
@misc{reference.wolfram_2026_textcases, author="Wolfram Research", title="{TextCases}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/TextCases.html}", note=[Accessed: 15-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_textcases, organization={Wolfram Research}, title={TextCases}, year={2019}, url={https://reference.wolfram.com/language/ref/TextCases.html}, note=[Accessed: 15-November-2026]}