2013-05-17 77 views
0

用户指南第6.1.5章单词块单词是由空格,制表符或返回字符或用双引号括起来的字符串。是否可以有额外的单词分隔符?如何设置单词分隔符?

我从“用户指南”一章6.5.1'何时使用数组'中获取以下代码片段, 184

on mouseUp 

    --cycle through each word adding each instance to an array 
    repeat for each word tWord in field "sample text" 
     add 1 to tWordCount[tWord] 
    end repeat 

    -- combine the array into text 
    combine tWordCount using return and comma 
    answer tWordCount 

end mouseUp 

它计数字段“示例文本”中每个单词形式的出现次数。

我意识到完全停止后单词被计为单词的一部分与默认设置。

如何更改句点(和,或逗号)被认为是单词边界的设置?

回答

1

或者,您可以在处理之前简单地删除违规字符。 这您就可以利用更换功能或“REPLACETEXT功能来完成。 的REPLACETEXT功能可以使用正则表达式的MatchString但比REPLACE函数慢。所以在这里我使用替换功能。

on mouseUp 
    put field "sample" into twords 
    --remove all trailing puncuation and quotes 
    replace "." with "" in twords 
    replace "," with "" in twords 
    replace "?" with "" in twords 
    replace ";" with "" in twords 
    replace ":" with "" in twords 
    replace quote with "" in twords 
    --hyphenated words need to be seperated? 
    replace "-" with " " in twords 

    repeat for each word tword in twords 
     add 1 to twordcount[tword] 
    end repeat 
    combine twordcount using return and comma 
    answer twordcount 
end mouseUp 
+0

这也是一个有趣的解决方案。我不知道哪一个更快。 –

0

根据用户指南在章节6.1.5中说的内容单词块单词是由空格,制表符或返回字符或用双引号括起来的字符串。

itemDelimiter但不是wordDelimiter

因此标点符号在将词添加到词计数数组之前首先被移除。

这可以用功能effectiveWord完成。

function effectiveWord aWord 
    put last char of aWord into it 
    if it is "." then delete last char of aWord 
    if it is "," then delete last char of aWord 
    if it is ":" then delete last char of aWord 
    if it is ";" then delete last char of aWord 
    return aWord 
end effectiveWord 



on mouseUp 

    --cycle through each word adding each instance to an array 
    repeat for each word tWord in field "Sample text" 
     add 1 to tWordCount[effectiveWord(tWord)] 
    end repeat 

    -- combine the array into text 
    combine tWordCount using return and comma 
    answer tWordCount 

end mouseUp 
+0

您可以更有效地使用正则表达式做到这一点:replaceText(myVar的, “[^ A-ZA-Z0-9]”,为空)。 – Mark

+0

是的,但这对Unicode不起作用。 –

+1

您似乎需要的文本类型可以很容易地转换为ASCII文本。 – Mark

1

我想你问一个关于分隔符的问题有些分隔符内置:

空间的话,

逗号的项目,

回报( CR)为线路。

能够创建自己的自定义分隔符属性(itemDelimiter)是该语言的一个强大功能,属于“项目”。你可以将其设置为任何单个字符:

设置itemDelimiter为“C”

答案--call这串“theText”中的“XXCXXCXX”的项目数

其结果将是“ 3"

正如其他人指出的那样,替代一个字符串为另一个的方法允许对文本的定制解析强大的控制:

替换‘C’与空间theText

产量 “XX XX XX”

克雷格·纽曼

+0

是的,对于项目我可以设置分隔符,这是非常有用的。但对于单词而言,它被设置为空格,制表符或返回。这意味着立即在一个词后面的句号被认为是该词的一部分。我的问题是关于获得“有效”单词的最简单方法,即包含没有标点符号的字形。 –

相关问题