Google python mimic.py (python 2.7)

本文介绍了一个使用Python编写的文本模仿生成器。该程序通过读取指定文件中的文本,建立一个模仿字典来映射每个出现的单词到其后可能跟随的所有单词列表,然后根据这个字典生成200个随机但风格类似原文的单词。文中提供了两种构造模仿字典的方法,并附带了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/python -tt

Read in the file specified on the command line.
Do a simple split() on whitespace to obtain all the words in the file.
Rather than read the file line by line, it's easier to read
it into one giant string and split it once.

Build a "mimic" dict that maps each word that appears in the file
to a list of all the words that immediately follow that word in the file.
The list of words can be be in any order and should include
duplicates. So for example the key "and" might have the list
["then", "best", "then", "after", ...] listing
all the words which came after "and" in the text.
We'll say that the empty string is what comes before
the first word in the file.

With the mimic dict, it's fairly easy to emit random
text that mimics the original. Print a word, then look
up what words might come next and pick one at random as
the next work.
Use the empty string as the first word to prime things.
If we ever get stuck with a word that is not in the dict,
go back to the empty string to keep things moving.

Note: the standard python module 'random' includes a
random.choice(list) method which picks a random element
from a non-empty list.

For fun, feed your program to itself as input.
Could work on getting it to put in linebreaks around 70
columns, so the output looks better.

"""

import random
import sys


def mimic_dict(filename):
"""Returns mimic dict mapping each word to list of words which follow it."""
[color=red] f=open(filename,'r')
words=f.read()
f.close()
dicts={}
words=words.split()
#solution1:
if len(words)>0:
dicts['']=[words[0]]
for word in words:
if words.index(word)<len(words)-1:
if word in dicts.keys() and words[words.index(word)+1] not in dicts.values():
dicts.get(word).append(words[words.index(word)+1])
else:
dicts[word]=[words[words.index(word)+1]]
return dicts[/color]
#solution 2:
[color=blue] temp=''
for word in words:
if temp not in dicts:
dicts[temp]=[word]
else:
dicts[temp].append(word)
temp=word
return dicts[/color]

def print_mimic(mimic_dict, word):
"""Given mimic dict and start word, prints 200 random words."""

[color=red] for i in range(0,200):
print word,
if len(mimic_dict[word])<1:
word=''
word=random.choice(mimic_dict.get(word))
[/color]

# Provided main(), calls mimic_dict() and mimic()
def main():
if len(sys.argv) != 2:
print 'usage: ./mimic.py file-to-read'
sys.exit(1)

dict = mimic_dict(sys.argv[1])
print_mimic(dict, '')


if __name__ == '__main__':
main()
### ROS 中创建 `turtle_mimic.launch` 的方法 在 ROS 2 中,`.launch` 文件通常用于启动多个节点并配置它们的参数。以下是关于如何创建一个名为 `turtle_mimic.launch` 的 Launch 文件的具体说明。 #### 创建 Launch 文件的过程 为了实现这一目标,可以按照以下结构编写 Python 脚本作为 `.py` 后缀的 Launch 文件,并将其命名为 `turtlesim_mimic_launch.py`[^1]: ```python from launch import LaunchDescription from launch_ros.actions import Node def generate_launch_description(): # 定义两个 turtlesim_node 实例以及 mimic 节点 turtlesim1 = Node( package='turtlesim', executable='turtlesim_node', name='turtlesim1' ) turtlesim2 = Node( package='turtlesim', executable='turtlesim_node', name='turtlesim2' ) mimic_node = Node( package='turtlesim', executable='mimic', name='mimic', remappings=[ ('/input/pose', '/turtlesim1/turtle1/pose'), ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel') ] ) # 返回描述符对象 return LaunchDescription([ turtlesim1, turtlesim2, mimic_node ]) ``` 上述脚本定义了一个 Launch 描述器,其中包含了三个主要部分:两个 Turtlesim 节点实例 (`turtlesim1`, `turtlesim2`) 和一个模仿节点 (`mimic`)。通过重映射功能,实现了将第一个海龟的位置数据传递给第二个海龟的速度控制输入。 #### 如何运行此 Launch 文件 完成文件编写之后,需确保该文件位于某个已安装或源码编译的工作空间中的适当包目录下(通常是 `<your_package>/launch/`)。接着可以通过如下命令来执行它: ```bash ros2 launch <package_name> turtlesim_mimic_launch.py ``` 这里 `<package_name>` 应替换为你实际放置此脚本所在的 ROS 包名。 #### 注意事项 - 需要确认所使用的环境已经正确设置了 ROS 2 并初始化了相关依赖项。 - 如果遇到权限错误或者找不到模块等问题,请检查工作区是否已被成功构建(`colcon build`)并且激活了正确的 overlay shell (source install/local_setup.bash)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值