如何在spark scala中找到具有特定术语的文本行
how to find a line in a text with specific term in spark scala
我在 spark scala 中找不到对此的响应,
请看详细,
我有一个包含主题列表及其权重的输出文本,如下所示:(这是在文档上使用 lda 实现的)
TOPIC_0;connection;0.030922248292319265
TOPIC_0;pragmatic;0.02690878152282403
TOPIC_0;Originator;0.02443295327258558
TOPIC_0;check;0.022290036662386385
TOPIC_0;input;0.020578378303486064
TOPIC_0;character;0.019718375317755072
TOPIC_0;wide;0.017389396600966833
TOPIC_0;load;0.016898979702795396
TOPIC_0;Pretty;0.014923624938546124
TOPIC_0;soon;0.014731449663492822
我想浏览每个主题并在文件中找到与该主题相关的第一句话。
我尝试过类似的方法,但我无法下定决心过滤:
topic.foreach { case (term, weight) =>
val filePath = "data/20_news/sci.BusinessandFinance/14147"
val lines = sc.textFile(filePath)
val words = lines.flatMap(x => x.split(' '))
val sentence = words.filter(w => words.contains(term))
}
过滤的最后一行不正确,
例如:
我的文本文件是这样的:
input for the program should be checked. the connection between two part is pretty simple.
所以它应该提取主题的第一句话:"input
"
感谢任何帮助或想法
我认为您正在过滤单词列表,应该过滤行。
此代码:words.contains(term)
没有任何意义,因为如果该术语出现在任何单词中,它 return 为真。
这样写会更有意义:
w.contains(term)
这样至少您的过滤器只会 return 匹配该词的词。
然而,您真正想要的是查看 line
(即句子)是否包含该词。
topic.foreach { case (term, weight) =>
val filePath = "data/20_news/sci.BusinessandFinance/14147"
val lines = sc.textFile(filePath)
val sentence = lines.filter(line => line.contains(term))
}
虽然这些行可能需要额外的拆分(例如在句号上)才能得到句子。
您可以像这样添加此步骤:
topic.foreach { case (term, weight) =>
val filePath = "data/20_news/sci.BusinessandFinance/14147"
val lines = sc.textFile(filePath)
val morelines = lines.flatMap(l => l.split(". "))
val sentence = morelines.filter(line => line.contains(term))
}
val rddOnline = sc.textFile("/path/to/file")
val hasLine = rddOnline.map(行 => line.contains("whatever it is"))
它将 return 是或否
我在 spark scala 中找不到对此的响应,
请看详细,
我有一个包含主题列表及其权重的输出文本,如下所示:(这是在文档上使用 lda 实现的)
TOPIC_0;connection;0.030922248292319265
TOPIC_0;pragmatic;0.02690878152282403
TOPIC_0;Originator;0.02443295327258558
TOPIC_0;check;0.022290036662386385
TOPIC_0;input;0.020578378303486064
TOPIC_0;character;0.019718375317755072
TOPIC_0;wide;0.017389396600966833
TOPIC_0;load;0.016898979702795396
TOPIC_0;Pretty;0.014923624938546124
TOPIC_0;soon;0.014731449663492822
我想浏览每个主题并在文件中找到与该主题相关的第一句话。
我尝试过类似的方法,但我无法下定决心过滤:
topic.foreach { case (term, weight) =>
val filePath = "data/20_news/sci.BusinessandFinance/14147"
val lines = sc.textFile(filePath)
val words = lines.flatMap(x => x.split(' '))
val sentence = words.filter(w => words.contains(term))
}
过滤的最后一行不正确,
例如:
我的文本文件是这样的:
input for the program should be checked. the connection between two part is pretty simple.
所以它应该提取主题的第一句话:"input
"
感谢任何帮助或想法
我认为您正在过滤单词列表,应该过滤行。
此代码:words.contains(term)
没有任何意义,因为如果该术语出现在任何单词中,它 return 为真。
这样写会更有意义:
w.contains(term)
这样至少您的过滤器只会 return 匹配该词的词。
然而,您真正想要的是查看 line
(即句子)是否包含该词。
topic.foreach { case (term, weight) =>
val filePath = "data/20_news/sci.BusinessandFinance/14147"
val lines = sc.textFile(filePath)
val sentence = lines.filter(line => line.contains(term))
}
虽然这些行可能需要额外的拆分(例如在句号上)才能得到句子。 您可以像这样添加此步骤:
topic.foreach { case (term, weight) =>
val filePath = "data/20_news/sci.BusinessandFinance/14147"
val lines = sc.textFile(filePath)
val morelines = lines.flatMap(l => l.split(". "))
val sentence = morelines.filter(line => line.contains(term))
}
val rddOnline = sc.textFile("/path/to/file")
val hasLine = rddOnline.map(行 => line.contains("whatever it is"))
它将 return 是或否