查找句子中的单词并创建指示变量



我有一个变量,里面有各种句子:

Cats are good pets, for they are clean and are not noisy.
Abstraction is often one floor above you.
She wrote a long letter to Charlie, but he didn't read it.
Where do random thoughts come from?
Mary plays the piano.
I want more detailed information.
I'd rather be a bird than a fish.
When I was little I had a car door slammed shut on my hand. I still remember it quite vividly.
Malls are great places to shop; John can find everything he needs under one roof.
My Mum tries to be cool by saying that she likes all the same things that I do.

如果找到名称,如何创建变量name == 1

如果句子中的任何单词与我选择的单词匹配(例如letter(,我也希望有变量name == 2

我尝试了以下方法:

gen name = regexm(sentence, "letter* & (Charlie | Mary | John)*")` 

然而,这是行不通的。我在所有的观测中只得到name == 0

正则表达式很棒,但第二十二条军规是你必须非常努力地学习语言;如果你精通了,你就会看到好处。

我将把它留给其他答案来给出智能regex解决方案。这里的目的是强调其他字符串函数也是可用的。在这里,我利用了这样一个事实:如果strpos()在另一个字符串中找到一个字符串,它将返回一个正结果,相当于true。此外,Stata将解析为单词,因此从第一原理来看,即使(例如(找到字符串当且仅当它是单词也不太困难。

clear 
input strL whatever 
"Cats are good pets, for they are clean and are not noisy."
"Abstraction is often one floor above you."
"She wrote a long letter to Charlie, but he didn't read it."
"Where do random thoughts come from?"
"Mary plays the piano."
"I want more detailed information."
"I'd rather be a bird than a fish."
"When I was little I had a car door slammed shut on my hand. I still remember it quite vividly."
"Malls are great places to shop; John can find everything he needs under one roof."
"My Mum tries to be cool by saying that she likes all the same things that I do."
end 
gen wanted1 = strpos(whatever, "Charlie") | strpos(whatever, "Mary") | strpos(whatever, "John") 
* cat or cats as a word 
gen wanted2 = 0 
gen wordcount = wordcount(whatever) 
su wordcount, meanonly 
local J = r(max) 
quietly foreach w in cat cats { 
forval j = 1/`J' { 
replace wanted2 = 1 if word(lower(whatever), `j') == "`w'" 
}
} 
gen what = substr(whatever, 1, 40) 
list wanted? what, sep(0) 
+--------------------------------------------------------------+
| wanted1   wanted2                                       what |
|--------------------------------------------------------------|
1. |       0         1   Cats are good pets, for they are clean a |
2. |       0         0   Abstraction is often one floor above you |
3. |       1         0   She wrote a long letter to Charlie, but  |
4. |       0         0        Where do random thoughts come from? |
5. |       1         0                      Mary plays the piano. |
6. |       0         0          I want more detailed information. |
7. |       0         0          I'd rather be a bird than a fish. |
8. |       0         0   When I was little I had a car door slamm |
9. |       1         0   Malls are great places to shop; John can |
10. |       0         0   My Mum tries to be cool by saying that s |
+--------------------------------------------------------------+

最新更新